Coverage Report

Created: 2025-10-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/xmlsave.c
Line
Count
Source
1
/*
2
 * xmlsave.c: Implementation of the document serializer
3
 *
4
 * See Copyright for the status of this software.
5
 *
6
 * Author: Daniel Veillard
7
 */
8
9
#define IN_LIBXML
10
#include "libxml.h"
11
12
#include <limits.h>
13
#include <stdlib.h>
14
#include <string.h>
15
#include <libxml/xmlmemory.h>
16
#include <libxml/parserInternals.h>
17
#include <libxml/tree.h>
18
#include <libxml/xmlsave.h>
19
20
226k
#define MAX_INDENT 60
21
22
#include <libxml/HTMLtree.h>
23
24
#include "private/buf.h"
25
#include "private/enc.h"
26
#include "private/error.h"
27
#include "private/html.h"
28
#include "private/io.h"
29
#include "private/save.h"
30
31
#ifdef LIBXML_OUTPUT_ENABLED
32
33
3.57k
#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
34
35
struct _xmlSaveCtxt {
36
    const xmlChar *encoding;
37
    xmlCharEncodingHandlerPtr handler;
38
    xmlOutputBufferPtr buf;
39
    int options;
40
    int level;
41
    int format;
42
    char indent[MAX_INDENT + 1];  /* array for indenting output */
43
    int indent_nr;
44
    int indent_size;
45
    xmlCharEncodingOutputFunc escape; /* used for element content */
46
};
47
48
/************************************************************************
49
 *                  *
50
 *      Output error handlers       *
51
 *                  *
52
 ************************************************************************/
53
/**
54
 * Handle an out of memory condition
55
 *
56
 * @param out  an output buffer
57
 */
58
static void
59
xmlSaveErrMemory(xmlOutputBufferPtr out)
60
138
{
61
138
    if (out != NULL)
62
25
        out->error = XML_ERR_NO_MEMORY;
63
138
    xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_OUTPUT, NULL);
64
138
}
65
66
/**
67
 * Handle an out of memory condition
68
 *
69
 * @param out  an output buffer
70
 * @param code  the error number
71
 * @param node  the location of the error.
72
 * @param extra  extra information
73
 */
74
static void
75
xmlSaveErr(xmlOutputBufferPtr out, int code, xmlNodePtr node,
76
           const char *extra)
77
1.74k
{
78
1.74k
    const char *msg = NULL;
79
1.74k
    int res;
80
81
    /* Don't overwrite catastrophic errors */
82
1.74k
    if ((out != NULL) &&
83
1.66k
        (out->error != XML_ERR_OK) &&
84
0
        (xmlIsCatastrophicError(XML_ERR_FATAL, out->error)))
85
0
        return;
86
87
1.74k
    if (code == XML_ERR_NO_MEMORY) {
88
52
        xmlSaveErrMemory(out);
89
52
        return;
90
52
    }
91
92
1.69k
    if (out != NULL)
93
1.64k
        out->error = code;
94
95
1.69k
    if (code == XML_ERR_UNSUPPORTED_ENCODING) {
96
1.69k
        msg = "Unsupported encoding: %s";
97
1.69k
    } else {
98
0
        msg = xmlErrString(code);
99
0
        extra = NULL;
100
0
    }
101
102
1.69k
    res = xmlRaiseError(NULL, NULL, NULL, NULL, node,
103
1.69k
                        XML_FROM_OUTPUT, code, XML_ERR_ERROR, NULL, 0,
104
1.69k
                        extra, NULL, NULL, 0, 0,
105
1.69k
                        msg, extra);
106
1.69k
    if (res < 0)
107
3
        xmlSaveErrMemory(out);
108
1.69k
}
109
110
/************************************************************************
111
 *                  *
112
 *      Allocation and deallocation     *
113
 *                  *
114
 ************************************************************************/
115
116
/**
117
 * Sets the indent string.
118
 *
119
 * @since 2.14.0
120
 *
121
 * @param ctxt  save context
122
 * @param indent  indent string
123
 * @returns 0 on success, -1 if the string is NULL, empty or too long.
124
 */
125
int
126
113k
xmlSaveSetIndentString(xmlSaveCtxt *ctxt, const char *indent) {
127
113k
    size_t len;
128
113k
    int i;
129
130
113k
    if ((ctxt == NULL) || (indent == NULL))
131
0
        return(-1);
132
133
113k
    len = strlen(indent);
134
113k
    if ((len <= 0) || (len > MAX_INDENT))
135
0
        return(-1);
136
137
113k
    ctxt->indent_size = len;
138
113k
    ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
139
3.50M
    for (i = 0; i < ctxt->indent_nr; i++)
140
3.39M
        memcpy(&ctxt->indent[i * ctxt->indent_size], indent, len);
141
142
113k
    return(0);
143
113k
}
144
145
/**
146
 * Initialize a saving context
147
 *
148
 * @param ctxt  the saving context
149
 * @param options  save options
150
 */
151
static void
152
xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt, int options)
153
113k
{
154
113k
    if (ctxt == NULL) return;
155
156
113k
    xmlSaveSetIndentString(ctxt, xmlTreeIndentString);
157
158
113k
    if (options & XML_SAVE_FORMAT)
159
25.6k
        ctxt->format = 1;
160
87.5k
    else if (options & XML_SAVE_WSNONSIG)
161
6
        ctxt->format = 2;
162
163
113k
    if (((options & XML_SAVE_EMPTY) == 0) &&
164
113k
        (xmlSaveNoEmptyTags))
165
0
  options |= XML_SAVE_NO_EMPTY;
166
167
113k
    ctxt->options = options;
168
113k
}
169
170
/**
171
 * Free a saving context, destroying the output in any remaining buffer
172
 */
173
static void
174
xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
175
17.0k
{
176
17.0k
    if (ctxt == NULL) return;
177
17.0k
    if (ctxt->encoding != NULL)
178
1
        xmlFree((char *) ctxt->encoding);
179
17.0k
    if (ctxt->buf != NULL)
180
39
        xmlOutputBufferClose(ctxt->buf);
181
17.0k
    xmlFree(ctxt);
182
17.0k
}
183
184
/**
185
 * Create a new saving context
186
 *
187
 * @returns the new structure or NULL in case of error
188
 */
189
static xmlSaveCtxtPtr
190
xmlNewSaveCtxt(const char *encoding, int options)
191
17.1k
{
192
17.1k
    xmlSaveCtxtPtr ret;
193
194
17.1k
    ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
195
17.1k
    if (ret == NULL) {
196
67
  xmlSaveErrMemory(NULL);
197
67
  return ( NULL );
198
67
    }
199
17.0k
    memset(ret, 0, sizeof(xmlSaveCtxt));
200
201
17.0k
    if (encoding != NULL) {
202
85
        xmlParserErrors res;
203
204
85
        res = xmlOpenCharEncodingHandler(encoding, /* output */ 1,
205
85
                                         &ret->handler);
206
85
  if (res != XML_ERR_OK) {
207
84
      xmlSaveErr(NULL, res, NULL, encoding);
208
84
            xmlFreeSaveCtxt(ret);
209
84
      return(NULL);
210
84
  }
211
1
        ret->encoding = xmlStrdup((const xmlChar *)encoding);
212
1
    }
213
214
16.9k
    xmlSaveCtxtInit(ret, options);
215
216
16.9k
    return(ret);
217
17.0k
}
218
219
/************************************************************************
220
 *                  *
221
 *    Dumping XML tree content to a simple buffer   *
222
 *                  *
223
 ************************************************************************/
224
225
static void
226
1.04M
xmlSaveWriteText(xmlSaveCtxt *ctxt, const xmlChar *text, unsigned flags) {
227
1.04M
    if (ctxt->encoding == NULL)
228
358k
        flags |= XML_ESCAPE_NON_ASCII;
229
230
1.04M
    xmlSerializeText(ctxt->buf, text, SIZE_MAX, flags);
231
1.04M
}
232
233
/**
234
 * Serialize the attribute in the buffer
235
 *
236
 * @param ctxt  save context
237
 * @param attr  the attribute pointer
238
 */
239
static void
240
xmlSaveWriteAttrContent(xmlSaveCtxt *ctxt, xmlAttrPtr attr)
241
415k
{
242
415k
    xmlNodePtr children;
243
415k
    xmlOutputBufferPtr buf = ctxt->buf;
244
245
415k
    children = attr->children;
246
963k
    while (children != NULL) {
247
547k
        switch (children->type) {
248
404k
            case XML_TEXT_NODE:
249
404k
          xmlSaveWriteText(ctxt, children->content, XML_ESCAPE_ATTR);
250
404k
    break;
251
142k
            case XML_ENTITY_REF_NODE:
252
142k
                xmlOutputBufferWrite(buf, 1, "&");
253
142k
                xmlOutputBufferWriteString(buf, (const char *) children->name);
254
142k
                xmlOutputBufferWrite(buf, 1, ";");
255
142k
                break;
256
0
            default:
257
                /* should not happen unless we have a badly built tree */
258
0
                break;
259
547k
        }
260
547k
        children = children->next;
261
547k
    }
262
415k
}
263
264
/**
265
 * This will dump the content the notation declaration as an XML DTD definition
266
 *
267
 * @param buf  the XML buffer output
268
 * @param nota  A notation declaration
269
 */
270
static void
271
6.37k
xmlBufDumpNotationDecl(xmlOutputBufferPtr buf, xmlNotationPtr nota) {
272
6.37k
    xmlOutputBufferWrite(buf, 11, "<!NOTATION ");
273
6.37k
    xmlOutputBufferWriteString(buf, (const char *) nota->name);
274
275
6.37k
    if (nota->PublicID != NULL) {
276
5.30k
  xmlOutputBufferWrite(buf, 8, " PUBLIC ");
277
5.30k
  xmlOutputBufferWriteQuotedString(buf, nota->PublicID);
278
5.30k
  if (nota->SystemID != NULL) {
279
3.09k
      xmlOutputBufferWrite(buf, 1, " ");
280
3.09k
      xmlOutputBufferWriteQuotedString(buf, nota->SystemID);
281
3.09k
  }
282
5.30k
    } else {
283
1.06k
  xmlOutputBufferWrite(buf, 8, " SYSTEM ");
284
1.06k
  xmlOutputBufferWriteQuotedString(buf, nota->SystemID);
285
1.06k
    }
286
287
6.37k
    xmlOutputBufferWrite(buf, 3, " >\n");
288
6.37k
}
289
290
/**
291
 * This is called with the hash scan function, and just reverses args
292
 *
293
 * @param nota  A notation declaration
294
 * @param buf  the XML buffer output
295
 * @param name  unused
296
 */
297
static void
298
xmlBufDumpNotationDeclScan(void *nota, void *buf,
299
6.37k
                           const xmlChar *name ATTRIBUTE_UNUSED) {
300
6.37k
    xmlBufDumpNotationDecl((xmlOutputBufferPtr) buf, (xmlNotationPtr) nota);
301
6.37k
}
302
303
/**
304
 * This will dump the content of the notation table as an XML DTD definition
305
 *
306
 * @param buf  an xmlBuf output
307
 * @param table  A notation table
308
 */
309
static void
310
3.33k
xmlBufDumpNotationTable(xmlOutputBufferPtr buf, xmlNotationTablePtr table) {
311
3.33k
    xmlHashScan(table, xmlBufDumpNotationDeclScan, buf);
312
3.33k
}
313
314
/**
315
 * Dump the occurrence operator of an element.
316
 *
317
 * @param buf  output buffer
318
 * @param cur  element table
319
 */
320
static void
321
275k
xmlBufDumpElementOccur(xmlOutputBufferPtr buf, xmlElementContentPtr cur) {
322
275k
    switch (cur->ocur) {
323
216k
        case XML_ELEMENT_CONTENT_ONCE:
324
216k
            break;
325
25.7k
        case XML_ELEMENT_CONTENT_OPT:
326
25.7k
            xmlOutputBufferWrite(buf, 1, "?");
327
25.7k
            break;
328
23.8k
        case XML_ELEMENT_CONTENT_MULT:
329
23.8k
            xmlOutputBufferWrite(buf, 1, "*");
330
23.8k
            break;
331
8.81k
        case XML_ELEMENT_CONTENT_PLUS:
332
8.81k
            xmlOutputBufferWrite(buf, 1, "+");
333
8.81k
            break;
334
275k
    }
335
275k
}
336
337
/**
338
 * This will dump the content of the element table as an XML DTD definition
339
 *
340
 * @param buf  output buffer
341
 * @param content  element table
342
 */
343
static void
344
xmlBufDumpElementContent(xmlOutputBufferPtr buf,
345
9.17k
                         xmlElementContentPtr content) {
346
9.17k
    xmlElementContentPtr cur;
347
348
9.17k
    if (content == NULL) return;
349
350
9.17k
    xmlOutputBufferWrite(buf, 1, "(");
351
9.17k
    cur = content;
352
353
275k
    do {
354
275k
        if (cur == NULL) return;
355
356
275k
        switch (cur->type) {
357
2.74k
            case XML_ELEMENT_CONTENT_PCDATA:
358
2.74k
                xmlOutputBufferWrite(buf, 7, "#PCDATA");
359
2.74k
                break;
360
139k
            case XML_ELEMENT_CONTENT_ELEMENT:
361
139k
                if (cur->prefix != NULL) {
362
9.80k
                    xmlOutputBufferWriteString(buf,
363
9.80k
                            (const char *) cur->prefix);
364
9.80k
                    xmlOutputBufferWrite(buf, 1, ":");
365
9.80k
                }
366
139k
                xmlOutputBufferWriteString(buf, (const char *) cur->name);
367
139k
                break;
368
57.5k
            case XML_ELEMENT_CONTENT_SEQ:
369
132k
            case XML_ELEMENT_CONTENT_OR:
370
132k
                if ((cur != content) &&
371
127k
                    (cur->parent != NULL) &&
372
127k
                    ((cur->type != cur->parent->type) ||
373
102k
                     (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
374
33.4k
                    xmlOutputBufferWrite(buf, 1, "(");
375
132k
                cur = cur->c1;
376
132k
                continue;
377
275k
        }
378
379
275k
        while (cur != content) {
380
265k
            xmlElementContentPtr parent = cur->parent;
381
382
265k
            if (parent == NULL) return;
383
384
265k
            if (((cur->type == XML_ELEMENT_CONTENT_OR) ||
385
192k
                 (cur->type == XML_ELEMENT_CONTENT_SEQ)) &&
386
127k
                ((cur->type != parent->type) ||
387
102k
                 (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
388
33.4k
                xmlOutputBufferWrite(buf, 1, ")");
389
265k
            xmlBufDumpElementOccur(buf, cur);
390
391
265k
            if (cur == parent->c1) {
392
132k
                if (parent->type == XML_ELEMENT_CONTENT_SEQ)
393
57.5k
                    xmlOutputBufferWrite(buf, 3, " , ");
394
75.4k
                else if (parent->type == XML_ELEMENT_CONTENT_OR)
395
75.4k
                    xmlOutputBufferWrite(buf, 3, " | ");
396
397
132k
                cur = parent->c2;
398
132k
                break;
399
132k
            }
400
401
132k
            cur = parent;
402
132k
        }
403
275k
    } while (cur != content);
404
405
9.17k
    xmlOutputBufferWrite(buf, 1, ")");
406
9.17k
    xmlBufDumpElementOccur(buf, content);
407
9.17k
}
408
409
/**
410
 * This will dump the content of the element declaration as an XML
411
 * DTD definition
412
 *
413
 * @param buf  an xmlBuf output
414
 * @param elem  An element table
415
 */
416
static void
417
15.9k
xmlBufDumpElementDecl(xmlOutputBufferPtr buf, xmlElementPtr elem) {
418
15.9k
    xmlOutputBufferWrite(buf, 10, "<!ELEMENT ");
419
15.9k
    if (elem->prefix != NULL) {
420
2.64k
        xmlOutputBufferWriteString(buf, (const char *) elem->prefix);
421
2.64k
        xmlOutputBufferWrite(buf, 1, ":");
422
2.64k
    }
423
15.9k
    xmlOutputBufferWriteString(buf, (const char *) elem->name);
424
15.9k
    xmlOutputBufferWrite(buf, 1, " ");
425
426
15.9k
    switch (elem->etype) {
427
4.54k
  case XML_ELEMENT_TYPE_EMPTY:
428
4.54k
      xmlOutputBufferWrite(buf, 5, "EMPTY");
429
4.54k
      break;
430
1.94k
  case XML_ELEMENT_TYPE_ANY:
431
1.94k
      xmlOutputBufferWrite(buf, 3, "ANY");
432
1.94k
      break;
433
2.74k
  case XML_ELEMENT_TYPE_MIXED:
434
9.17k
  case XML_ELEMENT_TYPE_ELEMENT:
435
9.17k
      xmlBufDumpElementContent(buf, elem->content);
436
9.17k
      break;
437
329
        default:
438
            /* assert(0); */
439
329
            break;
440
15.9k
    }
441
442
15.9k
    xmlOutputBufferWrite(buf, 2, ">\n");
443
15.9k
}
444
445
/**
446
 * This will dump the content of the enumeration
447
 *
448
 * @param buf  output buffer
449
 * @param cur  an enumeration
450
 */
451
static void
452
4.75k
xmlBufDumpEnumeration(xmlOutputBufferPtr buf, xmlEnumerationPtr cur) {
453
12.1k
    while (cur != NULL) {
454
7.44k
        xmlOutputBufferWriteString(buf, (const char *) cur->name);
455
7.44k
        if (cur->next != NULL)
456
3.20k
            xmlOutputBufferWrite(buf, 3, " | ");
457
458
7.44k
        cur = cur->next;
459
7.44k
    }
460
461
4.75k
    xmlOutputBufferWrite(buf, 1, ")");
462
4.75k
}
463
/**
464
 * This will dump the content of the attribute declaration as an XML
465
 * DTD definition
466
 *
467
 * @param ctxt  save context
468
 * @param attr  an attribute declaration
469
 */
470
static void
471
29.1k
xmlSaveWriteAttributeDecl(xmlSaveCtxtPtr ctxt, xmlAttributePtr attr) {
472
29.1k
    xmlOutputBufferPtr buf = ctxt->buf;
473
474
29.1k
    xmlOutputBufferWrite(buf, 10, "<!ATTLIST ");
475
29.1k
    xmlOutputBufferWriteString(buf, (const char *) attr->elem);
476
29.1k
    xmlOutputBufferWrite(buf, 1, " ");
477
29.1k
    if (attr->prefix != NULL) {
478
10.6k
  xmlOutputBufferWriteString(buf, (const char *) attr->prefix);
479
10.6k
  xmlOutputBufferWrite(buf, 1, ":");
480
10.6k
    }
481
29.1k
    xmlOutputBufferWriteString(buf, (const char *) attr->name);
482
483
29.1k
    switch (attr->atype) {
484
8.33k
  case XML_ATTRIBUTE_CDATA:
485
8.33k
      xmlOutputBufferWrite(buf, 6, " CDATA");
486
8.33k
      break;
487
5.73k
  case XML_ATTRIBUTE_ID:
488
5.73k
      xmlOutputBufferWrite(buf, 3, " ID");
489
5.73k
      break;
490
1.44k
  case XML_ATTRIBUTE_IDREF:
491
1.44k
      xmlOutputBufferWrite(buf, 6, " IDREF");
492
1.44k
      break;
493
821
  case XML_ATTRIBUTE_IDREFS:
494
821
      xmlOutputBufferWrite(buf, 7, " IDREFS");
495
821
      break;
496
1.17k
  case XML_ATTRIBUTE_ENTITY:
497
1.17k
      xmlOutputBufferWrite(buf, 7, " ENTITY");
498
1.17k
      break;
499
1.09k
  case XML_ATTRIBUTE_ENTITIES:
500
1.09k
      xmlOutputBufferWrite(buf, 9, " ENTITIES");
501
1.09k
      break;
502
1.91k
  case XML_ATTRIBUTE_NMTOKEN:
503
1.91k
      xmlOutputBufferWrite(buf, 8, " NMTOKEN");
504
1.91k
      break;
505
1.30k
  case XML_ATTRIBUTE_NMTOKENS:
506
1.30k
      xmlOutputBufferWrite(buf, 9, " NMTOKENS");
507
1.30k
      break;
508
4.33k
  case XML_ATTRIBUTE_ENUMERATION:
509
4.33k
      xmlOutputBufferWrite(buf, 2, " (");
510
4.33k
      xmlBufDumpEnumeration(buf, attr->tree);
511
4.33k
      break;
512
419
  case XML_ATTRIBUTE_NOTATION:
513
419
      xmlOutputBufferWrite(buf, 11, " NOTATION (");
514
419
      xmlBufDumpEnumeration(buf, attr->tree);
515
419
      break;
516
2.54k
  default:
517
            /* assert(0); */
518
2.54k
            break;
519
29.1k
    }
520
521
29.1k
    switch (attr->def) {
522
7.56k
  case XML_ATTRIBUTE_NONE:
523
7.56k
      break;
524
1.74k
  case XML_ATTRIBUTE_REQUIRED:
525
1.74k
      xmlOutputBufferWrite(buf, 10, " #REQUIRED");
526
1.74k
      break;
527
9.99k
  case XML_ATTRIBUTE_IMPLIED:
528
9.99k
      xmlOutputBufferWrite(buf, 9, " #IMPLIED");
529
9.99k
      break;
530
1.35k
  case XML_ATTRIBUTE_FIXED:
531
1.35k
      xmlOutputBufferWrite(buf, 7, " #FIXED");
532
1.35k
      break;
533
8.46k
  default:
534
            /* assert(0); */
535
8.46k
            break;
536
29.1k
    }
537
538
29.1k
    if (attr->defaultValue != NULL) {
539
13.3k
        xmlOutputBufferWrite(buf, 2, " \"");
540
13.3k
        xmlSaveWriteText(ctxt, attr->defaultValue, XML_ESCAPE_ATTR);
541
13.3k
        xmlOutputBufferWrite(buf, 1, "\"");
542
13.3k
    }
543
544
29.1k
    xmlOutputBufferWrite(buf, 2, ">\n");
545
29.1k
}
546
547
/**
548
 * This will dump the quoted string value, taking care of the special
549
 * treatment required by %
550
 *
551
 * @param buf  output buffer
552
 * @param content  entity content.
553
 */
554
static void
555
3.61k
xmlBufDumpEntityContent(xmlOutputBufferPtr buf, const xmlChar *content) {
556
3.61k
    const char * base, *cur;
557
558
3.61k
    if (content == NULL)
559
1.39k
        return;
560
561
2.21k
    xmlOutputBufferWrite(buf, 1, "\"");
562
2.21k
    base = cur = (const char *) content;
563
215k
    while (*cur != 0) {
564
213k
        if (*cur == '"') {
565
10.0k
            if (base != cur)
566
8.98k
                xmlOutputBufferWrite(buf, cur - base, base);
567
10.0k
            xmlOutputBufferWrite(buf, 6, "&quot;");
568
10.0k
            cur++;
569
10.0k
            base = cur;
570
203k
        } else if (*cur == '%') {
571
808
            if (base != cur)
572
549
                xmlOutputBufferWrite(buf, cur - base, base);
573
808
            xmlOutputBufferWrite(buf, 6, "&#x25;");
574
808
            cur++;
575
808
            base = cur;
576
202k
        } else {
577
202k
            cur++;
578
202k
        }
579
213k
    }
580
2.21k
    if (base != cur)
581
1.70k
        xmlOutputBufferWrite(buf, cur - base, base);
582
2.21k
    xmlOutputBufferWrite(buf, 1, "\"");
583
2.21k
}
584
585
/**
586
 * This will dump the content of the entity table as an XML DTD definition
587
 *
588
 * @param buf  an xmlBuf output
589
 * @param ent  An entity table
590
 */
591
static void
592
20.5k
xmlBufDumpEntityDecl(xmlOutputBufferPtr buf, xmlEntityPtr ent) {
593
20.5k
    if ((ent->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
594
18.6k
        (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY))
595
4.81k
        xmlOutputBufferWrite(buf, 11, "<!ENTITY % ");
596
15.7k
    else
597
15.7k
        xmlOutputBufferWrite(buf, 9, "<!ENTITY ");
598
20.5k
    xmlOutputBufferWriteString(buf, (const char *) ent->name);
599
20.5k
    xmlOutputBufferWrite(buf, 1, " ");
600
601
20.5k
    if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) ||
602
17.7k
        (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) ||
603
16.3k
        (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
604
7.19k
        if (ent->ExternalID != NULL) {
605
2.66k
             xmlOutputBufferWrite(buf, 7, "PUBLIC ");
606
2.66k
             xmlOutputBufferWriteQuotedString(buf, ent->ExternalID);
607
2.66k
             xmlOutputBufferWrite(buf, 1, " ");
608
4.52k
        } else {
609
4.52k
             xmlOutputBufferWrite(buf, 7, "SYSTEM ");
610
4.52k
        }
611
7.19k
        xmlOutputBufferWriteQuotedString(buf, ent->SystemID);
612
7.19k
    }
613
614
20.5k
    if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
615
1.45k
        if (ent->content != NULL) { /* Should be true ! */
616
707
            xmlOutputBufferWrite(buf, 7, " NDATA ");
617
707
            if (ent->orig != NULL)
618
76
                xmlOutputBufferWriteString(buf, (const char *) ent->orig);
619
631
            else
620
631
                xmlOutputBufferWriteString(buf, (const char *) ent->content);
621
707
        }
622
1.45k
    }
623
624
20.5k
    if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
625
13.3k
        (ent->etype == XML_INTERNAL_PARAMETER_ENTITY)) {
626
        /*
627
         * We could save the original quote character and avoid
628
         * calling xmlOutputBufferWriteQuotedString here.
629
         */
630
13.3k
        if (ent->orig != NULL)
631
9.73k
            xmlOutputBufferWriteQuotedString(buf, ent->orig);
632
3.61k
        else
633
3.61k
            xmlBufDumpEntityContent(buf, ent->content);
634
13.3k
    }
635
636
20.5k
    xmlOutputBufferWrite(buf, 2, ">\n");
637
20.5k
}
638
639
/************************************************************************
640
 *                  *
641
 *    Dumping XML tree content to an I/O output buffer  *
642
 *                  *
643
 ************************************************************************/
644
645
static int
646
11.8k
xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
647
11.8k
    xmlOutputBufferPtr buf = ctxt->buf;
648
11.8k
    xmlCharEncodingHandler *handler;
649
11.8k
    xmlParserErrors res;
650
651
    /* shouldn't happen */
652
11.8k
    if ((buf->encoder != NULL) || (buf->conv != NULL))
653
0
        return(-1);
654
655
11.8k
    res = xmlOpenCharEncodingHandler(encoding, /* output */ 1, &handler);
656
11.8k
    if (res != XML_ERR_OK) {
657
1.66k
        xmlSaveErr(buf, res, NULL, encoding);
658
1.66k
        return(-1);
659
1.66k
    }
660
661
10.1k
    if (handler != NULL) {
662
9.88k
        xmlBufPtr newbuf;
663
664
9.88k
        newbuf = xmlBufCreate(4000 /* MINLEN */);
665
9.88k
        if (newbuf == NULL) {
666
8
            xmlCharEncCloseFunc(handler);
667
8
            xmlSaveErrMemory(buf);
668
8
            return(-1);
669
8
        }
670
671
9.87k
        buf->conv = buf->buffer;
672
9.87k
        buf->buffer = newbuf;
673
9.87k
        buf->encoder = handler;
674
9.87k
    }
675
676
10.1k
    ctxt->encoding = (const xmlChar *) encoding;
677
678
    /*
679
     * initialize the state, e.g. if outputting a BOM
680
     */
681
10.1k
    xmlCharEncOutput(buf, 1);
682
683
10.1k
    return(0);
684
10.1k
}
685
686
static int
687
10.1k
xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) {
688
10.1k
    xmlOutputBufferPtr buf = ctxt->buf;
689
690
10.1k
    xmlOutputBufferFlush(buf);
691
692
10.1k
    if (buf->encoder != NULL) {
693
9.87k
        xmlCharEncCloseFunc(buf->encoder);
694
9.87k
        buf->encoder = NULL;
695
9.87k
        xmlBufFree(buf->buffer);
696
9.87k
        buf->buffer = buf->conv;
697
9.87k
        buf->conv = NULL;
698
9.87k
    }
699
700
10.1k
    ctxt->encoding = NULL;
701
702
10.1k
    return(0);
703
10.1k
}
704
705
#ifdef LIBXML_HTML_ENABLED
706
static void
707
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
708
#endif
709
static void
710
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
711
static int
712
xmlSaveDocInternal(xmlSaveCtxtPtr ctxt, xmlDocPtr cur,
713
                   const xmlChar *encoding);
714
715
static void
716
xmlSaveWriteIndent(xmlSaveCtxtPtr ctxt, int extra)
717
28.3k
{
718
28.3k
    int level;
719
720
28.3k
    if ((ctxt->options & XML_SAVE_NO_INDENT) ||
721
28.3k
        (((ctxt->options & XML_SAVE_INDENT) == 0) &&
722
28.3k
         (xmlIndentTreeOutput == 0)))
723
0
        return;
724
725
28.3k
    level = ctxt->level + extra;
726
28.3k
    if (level > ctxt->indent_nr)
727
7.66k
        level = ctxt->indent_nr;
728
28.3k
    xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size * level, ctxt->indent);
729
28.3k
}
730
731
/**
732
 * Write out formatting for non-significant whitespace output.
733
 *
734
 * @param ctxt  The save context
735
 * @param extra  Number of extra indents to apply to ctxt->level
736
 */
737
static void
738
xmlOutputBufferWriteWSNonSig(xmlSaveCtxtPtr ctxt, int extra)
739
12
{
740
12
    int i;
741
12
    if ((ctxt == NULL) || (ctxt->buf == NULL))
742
0
        return;
743
12
    xmlOutputBufferWrite(ctxt->buf, 1, "\n");
744
18
    for (i = 0; i < (ctxt->level + extra); i += ctxt->indent_nr) {
745
6
        xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size *
746
6
                ((ctxt->level + extra - i) > ctxt->indent_nr ?
747
6
                 ctxt->indent_nr : (ctxt->level + extra - i)),
748
6
                ctxt->indent);
749
6
    }
750
12
}
751
752
/**
753
 * Dump a local Namespace definition.
754
 * Should be called in the context of attributes dumps.
755
 * If `ctxt` is supplied, `buf` should be its buffer.
756
 *
757
 * @param buf  the XML buffer output
758
 * @param cur  a namespace
759
 * @param ctxt  the output save context. Optional.
760
 */
761
static void
762
1.77M
xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
763
1.77M
    unsigned escapeFlags = XML_ESCAPE_ATTR;
764
765
1.77M
    if ((cur == NULL) || (buf == NULL)) return;
766
767
1.77M
    if ((ctxt == NULL) || (ctxt->encoding == NULL))
768
1.30M
        escapeFlags |= XML_ESCAPE_NON_ASCII;
769
770
1.77M
    if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
771
1.76M
  if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
772
7.69k
      return;
773
774
1.75M
  if (ctxt != NULL && ctxt->format == 2)
775
0
      xmlOutputBufferWriteWSNonSig(ctxt, 2);
776
1.75M
  else
777
1.75M
      xmlOutputBufferWrite(buf, 1, " ");
778
779
        /* Within the context of an element attributes */
780
1.75M
  if (cur->prefix != NULL) {
781
1.72M
      xmlOutputBufferWrite(buf, 6, "xmlns:");
782
1.72M
      xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
783
1.72M
  } else
784
27.7k
      xmlOutputBufferWrite(buf, 5, "xmlns");
785
1.75M
        xmlOutputBufferWrite(buf, 2, "=\"");
786
1.75M
        xmlSerializeText(buf, cur->href, SIZE_MAX, escapeFlags);
787
1.75M
        xmlOutputBufferWrite(buf, 1, "\"");
788
1.75M
    }
789
1.77M
}
790
791
/**
792
 * Dump a list of local namespace definitions to a save context.
793
 * Should be called in the context of attribute dumps.
794
 *
795
 * @param ctxt  the save context
796
 * @param cur  the first namespace
797
 */
798
static void
799
459k
xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
800
1.08M
    while (cur != NULL) {
801
629k
        xmlNsDumpOutput(ctxt->buf, cur, ctxt);
802
629k
  cur = cur->next;
803
629k
    }
804
459k
}
805
806
/**
807
 * Serialize a list of namespace definitions.
808
 *
809
 * @param buf  the XML buffer output
810
 * @param cur  the first namespace
811
 */
812
void
813
42.8k
xmlNsListDumpOutput(xmlOutputBuffer *buf, xmlNs *cur) {
814
1.18M
    while (cur != NULL) {
815
1.14M
        xmlNsDumpOutput(buf, cur, NULL);
816
1.14M
  cur = cur->next;
817
1.14M
    }
818
42.8k
}
819
820
/**
821
 * Dump the XML document DTD, if any.
822
 *
823
 * @param ctxt  the save context
824
 * @param dtd  the pointer to the DTD
825
 */
826
static void
827
46.9k
xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
828
46.9k
    xmlOutputBufferPtr buf;
829
46.9k
    xmlNodePtr cur;
830
46.9k
    int format, level;
831
832
46.9k
    if (dtd == NULL) return;
833
46.9k
    if ((ctxt == NULL) || (ctxt->buf == NULL))
834
0
        return;
835
46.9k
    buf = ctxt->buf;
836
46.9k
    xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
837
46.9k
    xmlOutputBufferWriteString(buf, (const char *)dtd->name);
838
46.9k
    if (dtd->ExternalID != NULL) {
839
19.5k
  xmlOutputBufferWrite(buf, 8, " PUBLIC ");
840
19.5k
  xmlOutputBufferWriteQuotedString(buf, dtd->ExternalID);
841
19.5k
  xmlOutputBufferWrite(buf, 1, " ");
842
19.5k
  xmlOutputBufferWriteQuotedString(buf, dtd->SystemID);
843
27.3k
    }  else if (dtd->SystemID != NULL) {
844
15.3k
  xmlOutputBufferWrite(buf, 8, " SYSTEM ");
845
15.3k
  xmlOutputBufferWriteQuotedString(buf, dtd->SystemID);
846
15.3k
    }
847
46.9k
    if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
848
26.1k
        (dtd->attributes == NULL) && (dtd->notations == NULL) &&
849
23.6k
  (dtd->pentities == NULL)) {
850
22.1k
  xmlOutputBufferWrite(buf, 1, ">");
851
22.1k
  return;
852
22.1k
    }
853
24.7k
    xmlOutputBufferWrite(buf, 3, " [\n");
854
    /*
855
     * Dump the notations first they are not in the DTD children list
856
     * Do this only on a standalone DTD or on the internal subset though.
857
     */
858
24.7k
    if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
859
2.83k
        (dtd->doc->intSubset == dtd))) {
860
2.83k
        xmlBufDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
861
2.83k
    }
862
24.7k
    format = ctxt->format;
863
24.7k
    level = ctxt->level;
864
24.7k
    ctxt->format = 0;
865
24.7k
    ctxt->level = -1;
866
97.7k
    for (cur = dtd->children; cur != NULL; cur = cur->next) {
867
72.9k
        xmlNodeDumpOutputInternal(ctxt, cur);
868
72.9k
    }
869
24.7k
    ctxt->format = format;
870
24.7k
    ctxt->level = level;
871
24.7k
    xmlOutputBufferWrite(buf, 2, "]>");
872
24.7k
}
873
874
/**
875
 * Dump an XML attribute
876
 *
877
 * @param ctxt  the save context
878
 * @param cur  the attribute pointer
879
 */
880
static void
881
406k
xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
882
406k
    xmlOutputBufferPtr buf;
883
884
406k
    if (cur == NULL) return;
885
406k
    buf = ctxt->buf;
886
406k
    if (buf == NULL) return;
887
406k
    if (ctxt->format == 2)
888
0
        xmlOutputBufferWriteWSNonSig(ctxt, 2);
889
406k
    else
890
406k
        xmlOutputBufferWrite(buf, 1, " ");
891
406k
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
892
136k
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
893
136k
  xmlOutputBufferWrite(buf, 1, ":");
894
136k
    }
895
406k
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
896
406k
    xmlOutputBufferWrite(buf, 2, "=\"");
897
406k
#ifdef LIBXML_HTML_ENABLED
898
406k
    if ((ctxt->options & XML_SAVE_XHTML) &&
899
62.4k
        (cur->ns == NULL) &&
900
51.3k
        ((cur->children == NULL) ||
901
35.4k
         (cur->children->content == NULL) ||
902
35.2k
         (cur->children->content[0] == 0)) &&
903
36.4k
        (htmlIsBooleanAttr(cur->name))) {
904
623
        xmlOutputBufferWriteString(buf, (const char *) cur->name);
905
623
    } else
906
406k
#endif
907
406k
    {
908
406k
        xmlSaveWriteAttrContent(ctxt, cur);
909
406k
    }
910
406k
    xmlOutputBufferWrite(buf, 1, "\"");
911
406k
}
912
913
#ifdef LIBXML_HTML_ENABLED
914
/**
915
 * Dump an HTML node, recursive behaviour, children are printed too.
916
 *
917
 * @param ctxt  the save context
918
 * @param cur  the current node
919
 */
920
static int
921
2.07k
htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
922
2.07k
    int switched_encoding = 0;
923
2.07k
    int format = 0;
924
925
2.07k
    xmlInitParser();
926
927
2.07k
    if (ctxt->encoding == NULL) {
928
2.07k
        const char *encoding = NULL;
929
930
2.07k
        if (cur->doc != NULL)
931
2.07k
            encoding = (char *) cur->doc->encoding;
932
933
2.07k
        if (encoding == NULL)
934
2.07k
            encoding = "HTML";
935
936
2.07k
  if (xmlSaveSwitchEncoding(ctxt, encoding) < 0)
937
5
      return(-1);
938
2.07k
  switched_encoding = 1;
939
2.07k
    }
940
941
2.07k
    if (ctxt->options & XML_SAVE_FORMAT)
942
0
        format = 1;
943
944
2.07k
    htmlNodeDumpInternal(ctxt->buf, cur, (char *) ctxt->encoding, format);
945
946
2.07k
    if (switched_encoding) {
947
2.07k
  xmlSaveClearEncoding(ctxt);
948
2.07k
    }
949
950
2.07k
    return(0);
951
2.07k
}
952
#endif
953
954
/**
955
 * Dump an XML node, recursive behaviour, children are printed too.
956
 *
957
 * @param ctxt  the save context
958
 * @param cur  the current node
959
 */
960
static void
961
181k
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
962
181k
    int format = ctxt->format;
963
181k
    xmlNodePtr tmp, root, unformattedNode = NULL, parent;
964
181k
    xmlAttrPtr attr;
965
181k
    xmlChar *start, *end;
966
181k
    xmlOutputBufferPtr buf;
967
968
181k
    if (cur == NULL) return;
969
181k
    buf = ctxt->buf;
970
971
181k
    root = cur;
972
181k
    parent = cur->parent;
973
2.20M
    while (1) {
974
2.20M
        switch (cur->type) {
975
7.20k
        case XML_DOCUMENT_NODE:
976
11.8k
        case XML_HTML_DOCUMENT_NODE:
977
11.8k
      xmlSaveDocInternal(ctxt, (xmlDocPtr) cur, ctxt->encoding);
978
11.8k
      break;
979
980
39.3k
        case XML_DTD_NODE:
981
39.3k
            xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
982
39.3k
            break;
983
984
448
        case XML_DOCUMENT_FRAG_NODE:
985
            /* Always validate cur->parent when descending. */
986
448
            if ((cur->parent == parent) && (cur->children != NULL)) {
987
221
                parent = cur;
988
221
                cur = cur->children;
989
221
                continue;
990
221
            }
991
227
      break;
992
993
15.8k
        case XML_ELEMENT_DECL:
994
15.8k
            xmlBufDumpElementDecl(buf, (xmlElementPtr) cur);
995
15.8k
            break;
996
997
28.9k
        case XML_ATTRIBUTE_DECL:
998
28.9k
            xmlSaveWriteAttributeDecl(ctxt, (xmlAttributePtr) cur);
999
28.9k
            break;
1000
1001
20.3k
        case XML_ENTITY_DECL:
1002
20.3k
            xmlBufDumpEntityDecl(buf, (xmlEntityPtr) cur);
1003
20.3k
            break;
1004
1005
1.05M
        case XML_ELEMENT_NODE:
1006
1.05M
      if ((cur != root) && (ctxt->format == 1))
1007
11.4k
                xmlSaveWriteIndent(ctxt, 0);
1008
1009
            /*
1010
             * Some users like lxml are known to pass nodes with a corrupted
1011
             * tree structure. Fall back to a recursive call to handle this
1012
             * case.
1013
             */
1014
1.05M
            if ((cur->parent != parent) && (cur->children != NULL)) {
1015
0
                xmlNodeDumpOutputInternal(ctxt, cur);
1016
0
                break;
1017
0
            }
1018
1019
1.05M
            xmlOutputBufferWrite(buf, 1, "<");
1020
1.05M
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1021
43.2k
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1022
43.2k
                xmlOutputBufferWrite(buf, 1, ":");
1023
43.2k
            }
1024
1.05M
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1025
1.05M
            if (cur->nsDef)
1026
450k
                xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
1027
1.40M
            for (attr = cur->properties; attr != NULL; attr = attr->next)
1028
343k
                xmlAttrDumpOutput(ctxt, attr);
1029
1030
1.05M
            if (cur->children == NULL) {
1031
171k
                if ((ctxt->options & XML_SAVE_NO_EMPTY) == 0) {
1032
171k
                    if (ctxt->format == 2)
1033
0
                        xmlOutputBufferWriteWSNonSig(ctxt, 0);
1034
171k
                    xmlOutputBufferWrite(buf, 2, "/>");
1035
171k
                } else {
1036
0
                    if (ctxt->format == 2)
1037
0
                        xmlOutputBufferWriteWSNonSig(ctxt, 1);
1038
0
                    xmlOutputBufferWrite(buf, 3, "></");
1039
0
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1040
0
                        xmlOutputBufferWriteString(buf,
1041
0
                                (const char *)cur->ns->prefix);
1042
0
                        xmlOutputBufferWrite(buf, 1, ":");
1043
0
                    }
1044
0
                    xmlOutputBufferWriteString(buf, (const char *)cur->name);
1045
0
                    if (ctxt->format == 2)
1046
0
                        xmlOutputBufferWriteWSNonSig(ctxt, 0);
1047
0
                    xmlOutputBufferWrite(buf, 1, ">");
1048
0
                }
1049
887k
            } else {
1050
887k
                if (ctxt->format == 1) {
1051
14.1k
                    tmp = cur->children;
1052
26.6k
                    while (tmp != NULL) {
1053
17.5k
                        if ((tmp->type == XML_TEXT_NODE) ||
1054
13.3k
                            (tmp->type == XML_CDATA_SECTION_NODE) ||
1055
12.9k
                            (tmp->type == XML_ENTITY_REF_NODE)) {
1056
5.02k
                            ctxt->format = 0;
1057
5.02k
                            unformattedNode = cur;
1058
5.02k
                            break;
1059
5.02k
                        }
1060
12.5k
                        tmp = tmp->next;
1061
12.5k
                    }
1062
14.1k
                }
1063
887k
                if (ctxt->format == 2)
1064
6
                    xmlOutputBufferWriteWSNonSig(ctxt, 1);
1065
887k
                xmlOutputBufferWrite(buf, 1, ">");
1066
887k
                if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
1067
887k
                if (ctxt->level >= 0) ctxt->level++;
1068
887k
                parent = cur;
1069
887k
                cur = cur->children;
1070
887k
                continue;
1071
887k
            }
1072
1073
171k
            break;
1074
1075
579k
        case XML_TEXT_NODE:
1076
579k
      if (cur->content == NULL)
1077
1.03k
                break;
1078
578k
      if (cur->name != xmlStringTextNoenc) {
1079
578k
                if (ctxt->escape)
1080
0
                    xmlOutputBufferWriteEscape(buf, cur->content,
1081
0
                                               ctxt->escape);
1082
#ifdef TEST_OUTPUT_BUFFER_WRITE_ESCAPE
1083
                else if (ctxt->encoding)
1084
                    xmlOutputBufferWriteEscape(buf, cur->content, NULL);
1085
#endif
1086
578k
                else
1087
578k
                    xmlSaveWriteText(ctxt, cur->content, /* flags */ 0);
1088
578k
      } else {
1089
    /*
1090
     * Disable escaping, needed for XSLT
1091
     */
1092
0
    xmlOutputBufferWriteString(buf, (const char *) cur->content);
1093
0
      }
1094
578k
      break;
1095
1096
19.3k
        case XML_PI_NODE:
1097
19.3k
      if ((cur != root) && (ctxt->format == 1))
1098
473
                xmlSaveWriteIndent(ctxt, 0);
1099
1100
19.3k
            if (cur->content != NULL) {
1101
12.9k
                xmlOutputBufferWrite(buf, 2, "<?");
1102
12.9k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1103
12.9k
                if (cur->content != NULL) {
1104
12.9k
                    if (ctxt->format == 2)
1105
0
                        xmlOutputBufferWriteWSNonSig(ctxt, 0);
1106
12.9k
                    else
1107
12.9k
                        xmlOutputBufferWrite(buf, 1, " ");
1108
12.9k
                    xmlOutputBufferWriteString(buf,
1109
12.9k
                            (const char *)cur->content);
1110
12.9k
                }
1111
12.9k
                xmlOutputBufferWrite(buf, 2, "?>");
1112
12.9k
            } else {
1113
6.44k
                xmlOutputBufferWrite(buf, 2, "<?");
1114
6.44k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1115
6.44k
                if (ctxt->format == 2)
1116
0
                    xmlOutputBufferWriteWSNonSig(ctxt, 0);
1117
6.44k
                xmlOutputBufferWrite(buf, 2, "?>");
1118
6.44k
            }
1119
19.3k
            break;
1120
1121
17.9k
        case XML_COMMENT_NODE:
1122
17.9k
      if ((cur != root) && (ctxt->format == 1))
1123
521
                xmlSaveWriteIndent(ctxt, 0);
1124
1125
17.9k
            if (cur->content != NULL) {
1126
17.1k
                xmlOutputBufferWrite(buf, 4, "<!--");
1127
17.1k
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
1128
17.1k
                xmlOutputBufferWrite(buf, 3, "-->");
1129
17.1k
            }
1130
17.9k
            break;
1131
1132
18.9k
        case XML_ENTITY_REF_NODE:
1133
18.9k
            xmlOutputBufferWrite(buf, 1, "&");
1134
18.9k
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1135
18.9k
            xmlOutputBufferWrite(buf, 1, ";");
1136
18.9k
            break;
1137
1138
11.7k
        case XML_CDATA_SECTION_NODE:
1139
11.7k
            if (cur->content == NULL || *cur->content == '\0') {
1140
8.02k
                xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
1141
8.02k
            } else {
1142
3.68k
                start = end = cur->content;
1143
56.4k
                while (*end != '\0') {
1144
52.7k
                    if ((*end == ']') && (*(end + 1) == ']') &&
1145
1.18k
                        (*(end + 2) == '>')) {
1146
235
                        end = end + 2;
1147
235
                        xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1148
235
                        xmlOutputBufferWrite(buf, end - start,
1149
235
                                (const char *)start);
1150
235
                        xmlOutputBufferWrite(buf, 3, "]]>");
1151
235
                        start = end;
1152
235
                    }
1153
52.7k
                    end++;
1154
52.7k
                }
1155
3.68k
                if (start != end) {
1156
3.68k
                    xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1157
3.68k
                    xmlOutputBufferWriteString(buf, (const char *)start);
1158
3.68k
                    xmlOutputBufferWrite(buf, 3, "]]>");
1159
3.68k
                }
1160
3.68k
            }
1161
11.7k
            break;
1162
1163
895
        case XML_ATTRIBUTE_NODE:
1164
895
            xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
1165
895
            break;
1166
1167
30
        case XML_NAMESPACE_DECL:
1168
30
            xmlNsDumpOutput(buf, (xmlNsPtr) cur, ctxt);
1169
30
            break;
1170
1171
378k
        default:
1172
378k
            break;
1173
2.20M
        }
1174
1175
2.20M
        while (1) {
1176
2.20M
            if (cur == root)
1177
181k
                return;
1178
2.02M
            if ((ctxt->format == 1) &&
1179
12.4k
                (cur->type != XML_XINCLUDE_START) &&
1180
12.4k
                (cur->type != XML_XINCLUDE_END))
1181
12.4k
                xmlOutputBufferWrite(buf, 1, "\n");
1182
2.02M
            if (cur->next != NULL) {
1183
1.13M
                cur = cur->next;
1184
1.13M
                break;
1185
1.13M
            }
1186
1187
887k
            cur = parent;
1188
            /* cur->parent was validated when descending. */
1189
887k
            parent = cur->parent;
1190
1191
887k
            if (cur->type == XML_ELEMENT_NODE) {
1192
887k
                if (ctxt->level > 0) ctxt->level--;
1193
887k
                if (ctxt->format == 1)
1194
9.08k
                    xmlSaveWriteIndent(ctxt, 0);
1195
1196
887k
                xmlOutputBufferWrite(buf, 2, "</");
1197
887k
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1198
17.4k
                    xmlOutputBufferWriteString(buf,
1199
17.4k
                            (const char *)cur->ns->prefix);
1200
17.4k
                    xmlOutputBufferWrite(buf, 1, ":");
1201
17.4k
                }
1202
1203
887k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1204
887k
                if (ctxt->format == 2)
1205
6
                    xmlOutputBufferWriteWSNonSig(ctxt, 0);
1206
887k
                xmlOutputBufferWrite(buf, 1, ">");
1207
1208
887k
                if (cur == unformattedNode) {
1209
5.02k
                    ctxt->format = format;
1210
5.02k
                    unformattedNode = NULL;
1211
5.02k
                }
1212
887k
            }
1213
887k
        }
1214
1.31M
    }
1215
181k
}
1216
1217
/**
1218
 * Dump an XML document.
1219
 *
1220
 * @param ctxt  the save context
1221
 * @param cur  the document
1222
 * @param encoding  character encoding (optional)
1223
 */
1224
static int
1225
xmlSaveDocInternal(xmlSaveCtxtPtr ctxt, xmlDocPtr cur,
1226
62.1k
                   const xmlChar *encoding) {
1227
62.1k
#ifdef LIBXML_HTML_ENABLED
1228
62.1k
    xmlDtdPtr dtd;
1229
62.1k
    int is_xhtml = 0;
1230
62.1k
#endif
1231
62.1k
    xmlOutputBufferPtr buf = ctxt->buf;
1232
62.1k
    int switched_encoding = 0;
1233
1234
62.1k
    xmlInitParser();
1235
1236
62.1k
    if ((cur->type != XML_HTML_DOCUMENT_NODE) &&
1237
35.1k
        (cur->type != XML_DOCUMENT_NODE))
1238
0
   return(-1);
1239
1240
62.1k
    if (encoding == NULL)
1241
44.9k
  encoding = cur->encoding;
1242
1243
62.1k
    if (((cur->type == XML_HTML_DOCUMENT_NODE) &&
1244
27.0k
         ((ctxt->options & XML_SAVE_AS_XML) == 0) &&
1245
0
         ((ctxt->options & XML_SAVE_XHTML) == 0)) ||
1246
62.1k
        (ctxt->options & XML_SAVE_AS_HTML)) {
1247
0
#ifdef LIBXML_HTML_ENABLED
1248
0
        int format = 0;
1249
1250
0
  if (ctxt->encoding == NULL) {
1251
0
            if (encoding == NULL)
1252
0
                encoding = BAD_CAST "HTML";
1253
1254
0
      if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
1255
0
    return(-1);
1256
0
      }
1257
0
            switched_encoding = 1;
1258
0
  }
1259
1260
0
        if (ctxt->options & XML_SAVE_FORMAT)
1261
0
            format = 1;
1262
0
        htmlNodeDumpInternal(buf, (htmlNodePtr) cur, (char *) ctxt->encoding,
1263
0
                             format);
1264
#else
1265
        return(-1);
1266
#endif
1267
62.1k
    } else if ((cur->type == XML_DOCUMENT_NODE) ||
1268
27.0k
               (ctxt->options & XML_SAVE_AS_XML) ||
1269
62.1k
               (ctxt->options & XML_SAVE_XHTML)) {
1270
62.1k
  if ((encoding != NULL) && (ctxt->encoding == NULL)) {
1271
9.76k
            if (xmlSaveSwitchEncoding(ctxt, (const char *) encoding) < 0)
1272
1.66k
                return(-1);
1273
8.10k
            switched_encoding = 1;
1274
8.10k
  }
1275
1276
  /*
1277
   * Save the XML declaration
1278
   */
1279
60.5k
  if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
1280
60.5k
      xmlOutputBufferWrite(buf, 15, "<?xml version=\"");
1281
60.5k
      if (cur->version != NULL)
1282
35.5k
    xmlOutputBufferWriteString(buf, (char *) cur->version);
1283
24.9k
      else
1284
24.9k
    xmlOutputBufferWrite(buf, 3, "1.0");
1285
60.5k
      xmlOutputBufferWrite(buf, 1, "\"");
1286
60.5k
      if (encoding != NULL) {
1287
21.5k
    xmlOutputBufferWrite(buf, 11, " encoding=\"");
1288
21.5k
    xmlOutputBufferWriteString(buf, (char *) encoding);
1289
21.5k
          xmlOutputBufferWrite(buf, 1, "\"");
1290
21.5k
      }
1291
60.5k
      switch (cur->standalone) {
1292
335
    case 0:
1293
335
        xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
1294
335
        break;
1295
31.9k
    case 1:
1296
31.9k
        xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
1297
31.9k
        break;
1298
60.5k
      }
1299
60.5k
      xmlOutputBufferWrite(buf, 3, "?>\n");
1300
60.5k
  }
1301
1302
60.5k
#ifdef LIBXML_HTML_ENABLED
1303
60.5k
        if (ctxt->options & XML_SAVE_XHTML)
1304
1.63k
            is_xhtml = 1;
1305
60.5k
  if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
1306
60.5k
      dtd = xmlGetIntSubset(cur);
1307
60.5k
      if (dtd != NULL) {
1308
44.8k
    is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
1309
44.8k
    if (is_xhtml < 0) is_xhtml = 0;
1310
44.8k
      }
1311
60.5k
  }
1312
60.5k
#endif
1313
60.5k
  if (cur->children != NULL) {
1314
54.9k
      xmlNodePtr child = cur->children;
1315
1316
139k
      while (child != NULL) {
1317
84.4k
    ctxt->level = 0;
1318
84.4k
#ifdef LIBXML_HTML_ENABLED
1319
84.4k
    if (is_xhtml)
1320
16.2k
        xhtmlNodeDumpOutput(ctxt, child);
1321
68.1k
    else
1322
68.1k
#endif
1323
68.1k
        xmlNodeDumpOutputInternal(ctxt, child);
1324
84.4k
                if ((child->type != XML_XINCLUDE_START) &&
1325
84.4k
                    (child->type != XML_XINCLUDE_END))
1326
84.4k
                    xmlOutputBufferWrite(buf, 1, "\n");
1327
84.4k
    child = child->next;
1328
84.4k
      }
1329
54.9k
  }
1330
60.5k
    }
1331
1332
    /*
1333
     * Restore the state of the saving context at the end of the document
1334
     */
1335
60.5k
    if (switched_encoding) {
1336
8.10k
  xmlSaveClearEncoding(ctxt);
1337
8.10k
    }
1338
1339
60.5k
    return(0);
1340
62.1k
}
1341
1342
#ifdef LIBXML_HTML_ENABLED
1343
/************************************************************************
1344
 *                  *
1345
 *    Functions specific to XHTML serialization   *
1346
 *                  *
1347
 ************************************************************************/
1348
1349
/**
1350
 * Check if a node is an empty xhtml node
1351
 *
1352
 * @param node  the node
1353
 * @returns 1 if the node is an empty node, 0 if not and -1 in case of error
1354
 */
1355
static int
1356
40.7k
xhtmlIsEmpty(xmlNodePtr node) {
1357
40.7k
    if (node == NULL)
1358
0
  return(-1);
1359
40.7k
    if (node->type != XML_ELEMENT_NODE)
1360
0
  return(0);
1361
40.7k
    if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
1362
1.82k
  return(0);
1363
38.8k
    if (node->children != NULL)
1364
0
  return(0);
1365
38.8k
    switch (node->name ? node->name[0] : 0) {
1366
4.15k
  case 'a':
1367
4.15k
      if (xmlStrEqual(node->name, BAD_CAST "area"))
1368
460
    return(1);
1369
3.69k
      return(0);
1370
2.44k
  case 'b':
1371
2.44k
      if (xmlStrEqual(node->name, BAD_CAST "br"))
1372
606
    return(1);
1373
1.83k
      if (xmlStrEqual(node->name, BAD_CAST "base"))
1374
478
    return(1);
1375
1.35k
      if (xmlStrEqual(node->name, BAD_CAST "basefont"))
1376
270
    return(1);
1377
1.08k
      return(0);
1378
1.70k
  case 'c':
1379
1.70k
      if (xmlStrEqual(node->name, BAD_CAST "col"))
1380
611
    return(1);
1381
1.09k
      return(0);
1382
5.48k
  case 'f':
1383
5.48k
      if (xmlStrEqual(node->name, BAD_CAST "frame"))
1384
1.04k
    return(1);
1385
4.44k
      return(0);
1386
5.11k
  case 'h':
1387
5.11k
      if (xmlStrEqual(node->name, BAD_CAST "hr"))
1388
596
    return(1);
1389
4.52k
      return(0);
1390
4.09k
  case 'i':
1391
4.09k
      if (xmlStrEqual(node->name, BAD_CAST "img"))
1392
1.41k
    return(1);
1393
2.67k
      if (xmlStrEqual(node->name, BAD_CAST "input"))
1394
604
    return(1);
1395
2.07k
      if (xmlStrEqual(node->name, BAD_CAST "isindex"))
1396
628
    return(1);
1397
1.44k
      return(0);
1398
1.91k
  case 'l':
1399
1.91k
      if (xmlStrEqual(node->name, BAD_CAST "link"))
1400
588
    return(1);
1401
1.32k
      return(0);
1402
3.32k
  case 'm':
1403
3.32k
      if (xmlStrEqual(node->name, BAD_CAST "meta"))
1404
1.36k
    return(1);
1405
1.95k
      return(0);
1406
1.43k
  case 'p':
1407
1.43k
      if (xmlStrEqual(node->name, BAD_CAST "param"))
1408
460
    return(1);
1409
970
      return(0);
1410
38.8k
    }
1411
9.22k
    return(0);
1412
38.8k
}
1413
1414
/**
1415
 * Dump a list of XML attributes
1416
 *
1417
 * @param ctxt  the save context
1418
 * @param cur  the first attribute pointer
1419
 */
1420
static void
1421
34.6k
xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
1422
34.6k
    xmlAttrPtr xml_lang = NULL;
1423
34.6k
    xmlAttrPtr lang = NULL;
1424
34.6k
    xmlAttrPtr name = NULL;
1425
34.6k
    xmlAttrPtr id = NULL;
1426
34.6k
    xmlNodePtr parent;
1427
34.6k
    xmlOutputBufferPtr buf;
1428
1429
34.6k
    if (cur == NULL) return;
1430
34.6k
    buf = ctxt->buf;
1431
34.6k
    parent = cur->parent;
1432
96.7k
    while (cur != NULL) {
1433
62.0k
  if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
1434
1.39k
      id = cur;
1435
60.6k
  else
1436
60.6k
  if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
1437
7.21k
      name = cur;
1438
53.4k
  else
1439
53.4k
  if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
1440
2.17k
      lang = cur;
1441
51.2k
  else
1442
51.2k
  if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
1443
4.51k
      (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
1444
3.69k
      xml_lang = cur;
1445
62.0k
        xmlAttrDumpOutput(ctxt, cur);
1446
62.0k
  cur = cur->next;
1447
62.0k
    }
1448
    /*
1449
     * C.8
1450
     */
1451
34.6k
    if ((name != NULL) && (id == NULL)) {
1452
6.55k
  if ((parent != NULL) && (parent->name != NULL) &&
1453
6.34k
      ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1454
5.68k
       (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1455
5.02k
       (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1456
4.42k
       (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1457
3.82k
       (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1458
3.29k
       (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1459
2.69k
       (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1460
2.10k
       (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1461
5.47k
       (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
1462
5.47k
      xmlOutputBufferWrite(buf, 5, " id=\"");
1463
5.47k
            xmlSaveWriteAttrContent(ctxt, name);
1464
5.47k
      xmlOutputBufferWrite(buf, 1, "\"");
1465
5.47k
  }
1466
6.55k
    }
1467
    /*
1468
     * C.7.
1469
     */
1470
34.6k
    if ((lang != NULL) && (xml_lang == NULL)) {
1471
1.06k
  xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1472
1.06k
        xmlSaveWriteAttrContent(ctxt, lang);
1473
1.06k
  xmlOutputBufferWrite(buf, 1, "\"");
1474
1.06k
    } else
1475
33.6k
    if ((xml_lang != NULL) && (lang == NULL)) {
1476
2.85k
  xmlOutputBufferWrite(buf, 7, " lang=\"");
1477
2.85k
        xmlSaveWriteAttrContent(ctxt, xml_lang);
1478
2.85k
  xmlOutputBufferWrite(buf, 1, "\"");
1479
2.85k
    }
1480
34.6k
}
1481
1482
/**
1483
 * Dump an XHTML node, recursive behaviour, children are printed too.
1484
 *
1485
 * @param ctxt  the save context
1486
 * @param cur  the current node
1487
 */
1488
static void
1489
39.2k
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
1490
39.2k
    int format = ctxt->format, addmeta, oldoptions;
1491
39.2k
    xmlNodePtr tmp, root, unformattedNode = NULL, parent;
1492
39.2k
    xmlChar *start, *end;
1493
39.2k
    xmlOutputBufferPtr buf = ctxt->buf;
1494
1495
39.2k
    if (cur == NULL) return;
1496
1497
39.2k
    oldoptions = ctxt->options;
1498
39.2k
    ctxt->options |= XML_SAVE_XHTML;
1499
1500
39.2k
    root = cur;
1501
39.2k
    parent = cur->parent;
1502
186k
    while (1) {
1503
186k
        switch (cur->type) {
1504
868
        case XML_DOCUMENT_NODE:
1505
1.63k
        case XML_HTML_DOCUMENT_NODE:
1506
1.63k
            xmlSaveDocInternal(ctxt, (xmlDocPtr) cur, ctxt->encoding);
1507
1.63k
      break;
1508
1509
0
        case XML_NAMESPACE_DECL:
1510
0
      xmlNsDumpOutput(buf, (xmlNsPtr) cur, ctxt);
1511
0
      break;
1512
1513
7.60k
        case XML_DTD_NODE:
1514
7.60k
            xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
1515
7.60k
      break;
1516
1517
392
        case XML_DOCUMENT_FRAG_NODE:
1518
            /* Always validate cur->parent when descending. */
1519
392
            if ((cur->parent == parent) && (cur->children != NULL)) {
1520
197
                parent = cur;
1521
197
                cur = cur->children;
1522
197
                continue;
1523
197
            }
1524
195
            break;
1525
1526
195
        case XML_ELEMENT_DECL:
1527
194
            xmlBufDumpElementDecl(buf, (xmlElementPtr) cur);
1528
194
      break;
1529
1530
194
        case XML_ATTRIBUTE_DECL:
1531
194
            xmlSaveWriteAttributeDecl(ctxt, (xmlAttributePtr) cur);
1532
194
      break;
1533
1534
194
        case XML_ENTITY_DECL:
1535
194
            xmlBufDumpEntityDecl(buf, (xmlEntityPtr) cur);
1536
194
      break;
1537
1538
89.1k
        case XML_ELEMENT_NODE:
1539
89.1k
            addmeta = 0;
1540
1541
89.1k
      if ((cur != root) && (ctxt->format == 1))
1542
3.78k
                xmlSaveWriteIndent(ctxt, 0);
1543
1544
            /*
1545
             * Some users like lxml are known to pass nodes with a corrupted
1546
             * tree structure. Fall back to a recursive call to handle this
1547
             * case.
1548
             */
1549
89.1k
            if ((cur->parent != parent) && (cur->children != NULL)) {
1550
0
                xhtmlNodeDumpOutput(ctxt, cur);
1551
0
                break;
1552
0
            }
1553
1554
89.1k
            xmlOutputBufferWrite(buf, 1, "<");
1555
89.1k
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1556
1.74k
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1557
1.74k
                xmlOutputBufferWrite(buf, 1, ":");
1558
1.74k
            }
1559
1560
89.1k
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1561
89.1k
            if (cur->nsDef)
1562
8.44k
                xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
1563
89.1k
            if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1564
6.03k
                (cur->ns == NULL) && (cur->nsDef == NULL))) {
1565
                /*
1566
                 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1567
                 */
1568
2.92k
                xmlOutputBufferWriteString(buf,
1569
2.92k
                        " xmlns=\"http://www.w3.org/1999/xhtml\"");
1570
2.92k
            }
1571
89.1k
            if (cur->properties != NULL)
1572
34.6k
                xhtmlAttrListDumpOutput(ctxt, cur->properties);
1573
1574
89.1k
            if ((parent != NULL) &&
1575
77.4k
                (parent->parent == (xmlNodePtr) cur->doc) &&
1576
13.9k
                xmlStrEqual(cur->name, BAD_CAST"head") &&
1577
4.07k
                xmlStrEqual(parent->name, BAD_CAST"html")) {
1578
1579
3.61k
                tmp = cur->children;
1580
7.94k
                while (tmp != NULL) {
1581
4.59k
                    if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1582
1.36k
                        int res;
1583
1.36k
                        xmlChar *httpequiv;
1584
1585
1.36k
                        res = xmlNodeGetAttrValue(tmp, BAD_CAST "http-equiv",
1586
1.36k
                                                  NULL, &httpequiv);
1587
1.36k
                        if (res < 0) {
1588
0
                            xmlSaveErrMemory(buf);
1589
1.36k
                        } else if (res == 0) {
1590
274
                            if (xmlStrcasecmp(httpequiv,
1591
274
                                        BAD_CAST"Content-Type") == 0) {
1592
274
                                xmlFree(httpequiv);
1593
274
                                break;
1594
274
                            }
1595
0
                            xmlFree(httpequiv);
1596
0
                        }
1597
1.36k
                    }
1598
4.32k
                    tmp = tmp->next;
1599
4.32k
                }
1600
3.61k
                if (tmp == NULL)
1601
3.34k
                    addmeta = 1;
1602
3.61k
            }
1603
1604
89.1k
            if (cur->children == NULL) {
1605
41.5k
                if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
1606
40.7k
                    ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
1607
                    /*
1608
                     * C.2. Empty Elements
1609
                     */
1610
9.12k
                    xmlOutputBufferWrite(buf, 3, " />");
1611
32.3k
                } else {
1612
32.3k
                    if (addmeta == 1) {
1613
2.09k
                        xmlOutputBufferWrite(buf, 1, ">");
1614
2.09k
                        if (ctxt->format == 1) {
1615
324
                            xmlOutputBufferWrite(buf, 1, "\n");
1616
324
                            xmlSaveWriteIndent(ctxt, 1);
1617
324
                        }
1618
2.09k
                        xmlOutputBufferWriteString(buf,
1619
2.09k
                                "<meta http-equiv=\"Content-Type\" "
1620
2.09k
                                "content=\"text/html; charset=");
1621
2.09k
                        if (ctxt->encoding) {
1622
1.19k
                            xmlOutputBufferWriteString(buf,
1623
1.19k
                                    (const char *)ctxt->encoding);
1624
1.19k
                        } else {
1625
905
                            xmlOutputBufferWrite(buf, 5, "UTF-8");
1626
905
                        }
1627
2.09k
                        xmlOutputBufferWrite(buf, 4, "\" />");
1628
2.09k
                        if (ctxt->format == 1)
1629
324
                            xmlOutputBufferWrite(buf, 1, "\n");
1630
30.3k
                    } else {
1631
30.3k
                        xmlOutputBufferWrite(buf, 1, ">");
1632
30.3k
                    }
1633
                    /*
1634
                     * C.3. Element Minimization and Empty Element Content
1635
                     */
1636
32.3k
                    xmlOutputBufferWrite(buf, 2, "</");
1637
32.3k
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1638
807
                        xmlOutputBufferWriteString(buf,
1639
807
                                (const char *)cur->ns->prefix);
1640
807
                        xmlOutputBufferWrite(buf, 1, ":");
1641
807
                    }
1642
32.3k
                    xmlOutputBufferWriteString(buf, (const char *)cur->name);
1643
32.3k
                    xmlOutputBufferWrite(buf, 1, ">");
1644
32.3k
                }
1645
47.6k
            } else {
1646
47.6k
                xmlOutputBufferWrite(buf, 1, ">");
1647
47.6k
                if (addmeta == 1) {
1648
1.25k
                    if (ctxt->format == 1) {
1649
250
                        xmlOutputBufferWrite(buf, 1, "\n");
1650
250
                        xmlSaveWriteIndent(ctxt, 1);
1651
250
                    }
1652
1.25k
                    xmlOutputBufferWriteString(buf,
1653
1.25k
                            "<meta http-equiv=\"Content-Type\" "
1654
1.25k
                            "content=\"text/html; charset=");
1655
1.25k
                    if (ctxt->encoding) {
1656
581
                        xmlOutputBufferWriteString(buf,
1657
581
                                (const char *)ctxt->encoding);
1658
669
                    } else {
1659
669
                        xmlOutputBufferWrite(buf, 5, "UTF-8");
1660
669
                    }
1661
1.25k
                    xmlOutputBufferWrite(buf, 4, "\" />");
1662
1.25k
                }
1663
1664
47.6k
                if (ctxt->format == 1) {
1665
4.83k
                    tmp = cur->children;
1666
8.83k
                    while (tmp != NULL) {
1667
6.36k
                        if ((tmp->type == XML_TEXT_NODE) ||
1668
4.22k
                            (tmp->type == XML_ENTITY_REF_NODE)) {
1669
2.35k
                            unformattedNode = cur;
1670
2.35k
                            ctxt->format = 0;
1671
2.35k
                            break;
1672
2.35k
                        }
1673
4.00k
                        tmp = tmp->next;
1674
4.00k
                    }
1675
4.83k
                }
1676
1677
47.6k
                if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
1678
47.6k
                if (ctxt->level >= 0) ctxt->level++;
1679
47.6k
                parent = cur;
1680
47.6k
                cur = cur->children;
1681
47.6k
                continue;
1682
47.6k
            }
1683
1684
41.5k
            break;
1685
1686
50.5k
        case XML_TEXT_NODE:
1687
50.5k
      if (cur->content == NULL)
1688
195
                break;
1689
50.3k
      if ((cur->name == xmlStringText) ||
1690
50.3k
    (cur->name != xmlStringTextNoenc)) {
1691
50.3k
                if (ctxt->escape)
1692
0
                    xmlOutputBufferWriteEscape(buf, cur->content,
1693
0
                                               ctxt->escape);
1694
50.3k
                else
1695
50.3k
                    xmlSaveWriteText(ctxt, cur->content, /* flags */ 0);
1696
50.3k
      } else {
1697
    /*
1698
     * Disable escaping, needed for XSLT
1699
     */
1700
0
    xmlOutputBufferWriteString(buf, (const char *) cur->content);
1701
0
      }
1702
50.3k
      break;
1703
1704
2.25k
        case XML_PI_NODE:
1705
2.25k
            if (cur->content != NULL) {
1706
1.20k
                xmlOutputBufferWrite(buf, 2, "<?");
1707
1.20k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1708
1.20k
                if (cur->content != NULL) {
1709
1.20k
                    xmlOutputBufferWrite(buf, 1, " ");
1710
1.20k
                    xmlOutputBufferWriteString(buf,
1711
1.20k
                            (const char *)cur->content);
1712
1.20k
                }
1713
1.20k
                xmlOutputBufferWrite(buf, 2, "?>");
1714
1.20k
            } else {
1715
1.05k
                xmlOutputBufferWrite(buf, 2, "<?");
1716
1.05k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1717
1.05k
                xmlOutputBufferWrite(buf, 2, "?>");
1718
1.05k
            }
1719
2.25k
            break;
1720
1721
30.4k
        case XML_COMMENT_NODE:
1722
30.4k
            if (cur->content != NULL) {
1723
30.2k
                xmlOutputBufferWrite(buf, 4, "<!--");
1724
30.2k
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
1725
30.2k
                xmlOutputBufferWrite(buf, 3, "-->");
1726
30.2k
            }
1727
30.4k
            break;
1728
1729
1.19k
        case XML_ENTITY_REF_NODE:
1730
1.19k
            xmlOutputBufferWrite(buf, 1, "&");
1731
1.19k
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1732
1.19k
            xmlOutputBufferWrite(buf, 1, ";");
1733
1.19k
            break;
1734
1735
1.72k
        case XML_CDATA_SECTION_NODE:
1736
1.72k
            if (cur->content == NULL || *cur->content == '\0') {
1737
672
                xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
1738
1.05k
            } else {
1739
1.05k
                start = end = cur->content;
1740
10.5k
                while (*end != '\0') {
1741
9.54k
                    if (*end == ']' && *(end + 1) == ']' &&
1742
807
                        *(end + 2) == '>') {
1743
0
                        end = end + 2;
1744
0
                        xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1745
0
                        xmlOutputBufferWrite(buf, end - start,
1746
0
                                (const char *)start);
1747
0
                        xmlOutputBufferWrite(buf, 3, "]]>");
1748
0
                        start = end;
1749
0
                    }
1750
9.54k
                    end++;
1751
9.54k
                }
1752
1.05k
                if (start != end) {
1753
1.05k
                    xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1754
1.05k
                    xmlOutputBufferWriteString(buf, (const char *)start);
1755
1.05k
                    xmlOutputBufferWrite(buf, 3, "]]>");
1756
1.05k
                }
1757
1.05k
            }
1758
1.72k
            break;
1759
1760
423
        case XML_ATTRIBUTE_NODE:
1761
423
            xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
1762
423
      break;
1763
1764
352
        default:
1765
352
            break;
1766
186k
        }
1767
1768
186k
        while (1) {
1769
186k
            if (cur == root)
1770
39.2k
                return;
1771
147k
            if (ctxt->format == 1)
1772
3.96k
                xmlOutputBufferWrite(buf, 1, "\n");
1773
147k
            if (cur->next != NULL) {
1774
99.2k
                cur = cur->next;
1775
99.2k
                break;
1776
99.2k
            }
1777
1778
47.8k
            cur = parent;
1779
            /* cur->parent was validated when descending. */
1780
47.8k
            parent = cur->parent;
1781
1782
47.8k
            if (cur->type == XML_ELEMENT_NODE) {
1783
47.6k
                if (ctxt->level > 0) ctxt->level--;
1784
47.6k
                if (ctxt->format == 1)
1785
2.47k
                    xmlSaveWriteIndent(ctxt, 0);
1786
1787
47.6k
                xmlOutputBufferWrite(buf, 2, "</");
1788
47.6k
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1789
936
                    xmlOutputBufferWriteString(buf,
1790
936
                            (const char *)cur->ns->prefix);
1791
936
                    xmlOutputBufferWrite(buf, 1, ":");
1792
936
                }
1793
1794
47.6k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1795
47.6k
                xmlOutputBufferWrite(buf, 1, ">");
1796
1797
47.6k
                if (cur == unformattedNode) {
1798
2.35k
                    ctxt->format = format;
1799
2.35k
                    unformattedNode = NULL;
1800
2.35k
                }
1801
47.6k
            }
1802
47.8k
        }
1803
138k
    }
1804
1805
0
    ctxt->options = oldoptions;
1806
0
}
1807
#endif
1808
1809
/************************************************************************
1810
 *                  *
1811
 *      Public entry points       *
1812
 *                  *
1813
 ************************************************************************/
1814
1815
/**
1816
 * Create a document saving context serializing to a file descriptor
1817
 * with the encoding and the options given.
1818
 *
1819
 * If `encoding` is NULL, #xmlSaveDoc uses the document's
1820
 * encoding and #xmlSaveTree uses UTF-8.
1821
 *
1822
 * This function doesn't allow to distinguish unsupported
1823
 * encoding errors from failed memory allocations.
1824
 *
1825
 * @param fd  a file descriptor number
1826
 * @param encoding  the encoding name to use (optional)
1827
 * @param options  a set of xmlSaveOptions
1828
 * @returns a new serialization context or NULL in case of error.
1829
 */
1830
xmlSaveCtxt *
1831
xmlSaveToFd(int fd, const char *encoding, int options)
1832
123
{
1833
123
    xmlSaveCtxtPtr ret;
1834
1835
123
    ret = xmlNewSaveCtxt(encoding, options);
1836
123
    if (ret == NULL) return(NULL);
1837
39
    ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1838
39
    if (ret->buf == NULL) {
1839
0
        xmlCharEncCloseFunc(ret->handler);
1840
0
  xmlFreeSaveCtxt(ret);
1841
0
  return(NULL);
1842
0
    }
1843
39
    return(ret);
1844
39
}
1845
1846
/**
1847
 * Create a document saving context serializing to a filename
1848
 * with the encoding and the options given.
1849
 *
1850
 * If `encoding` is NULL, #xmlSaveDoc uses the document's
1851
 * encoding and #xmlSaveTree uses UTF-8.
1852
 *
1853
 * This function doesn't allow to distinguish unsupported
1854
 * encoding errors from failed memory allocations.
1855
 *
1856
 * @param filename  a file name or an URL
1857
 * @param encoding  the encoding name to use or NULL
1858
 * @param options  a set of xmlSaveOptions
1859
 * @returns a new serialization context or NULL in case of error.
1860
 */
1861
xmlSaveCtxt *
1862
xmlSaveToFilename(const char *filename, const char *encoding, int options)
1863
0
{
1864
0
    xmlSaveCtxtPtr ret;
1865
0
    int compression = 0; /* TODO handle compression option */
1866
1867
0
    ret = xmlNewSaveCtxt(encoding, options);
1868
0
    if (ret == NULL) return(NULL);
1869
0
    ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
1870
0
                                             compression);
1871
0
    if (ret->buf == NULL) {
1872
0
        xmlCharEncCloseFunc(ret->handler);
1873
0
  xmlFreeSaveCtxt(ret);
1874
0
  return(NULL);
1875
0
    }
1876
0
    return(ret);
1877
0
}
1878
1879
/**
1880
 * Create a document saving context serializing to a buffer
1881
 * with the encoding and the options given.
1882
 *
1883
 * If `encoding` is NULL, #xmlSaveDoc uses the document's
1884
 * encoding and #xmlSaveTree uses UTF-8.
1885
 *
1886
 * This function doesn't allow to distinguish unsupported
1887
 * encoding errors from failed memory allocations.
1888
 *
1889
 * @param buffer  a buffer
1890
 * @param encoding  the encoding name to use or NULL
1891
 * @param options  a set of xmlSaveOptions
1892
 * @returns a new serialization context or NULL in case of error.
1893
 */
1894
1895
xmlSaveCtxt *
1896
xmlSaveToBuffer(xmlBuffer *buffer, const char *encoding, int options)
1897
17.0k
{
1898
17.0k
    xmlSaveCtxtPtr ret;
1899
1900
17.0k
    ret = xmlNewSaveCtxt(encoding, options);
1901
17.0k
    if (ret == NULL) return(NULL);
1902
16.9k
    ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler);
1903
16.9k
    if (ret->buf == NULL) {
1904
366
        xmlCharEncCloseFunc(ret->handler);
1905
366
  xmlFreeSaveCtxt(ret);
1906
366
  return(NULL);
1907
366
    }
1908
16.5k
    return(ret);
1909
16.9k
}
1910
1911
/**
1912
 * Create a document saving context serializing to a file descriptor
1913
 * with the encoding and the options given
1914
 *
1915
 * If `encoding` is NULL, #xmlSaveDoc uses the document's
1916
 * encoding and #xmlSaveTree uses UTF-8.
1917
 *
1918
 * This function doesn't allow to distinguish unsupported
1919
 * encoding errors from failed memory allocations.
1920
 *
1921
 * @param iowrite  an I/O write function
1922
 * @param ioclose  an I/O close function
1923
 * @param ioctx  an I/O handler
1924
 * @param encoding  the encoding name to use or NULL
1925
 * @param options  a set of xmlSaveOptions
1926
 * @returns a new serialization context or NULL in case of error.
1927
 */
1928
xmlSaveCtxt *
1929
xmlSaveToIO(xmlOutputWriteCallback iowrite,
1930
            xmlOutputCloseCallback ioclose,
1931
            void *ioctx, const char *encoding, int options)
1932
0
{
1933
0
    xmlSaveCtxtPtr ret;
1934
1935
0
    ret = xmlNewSaveCtxt(encoding, options);
1936
0
    if (ret == NULL) return(NULL);
1937
0
    ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1938
0
    if (ret->buf == NULL) {
1939
0
        xmlCharEncCloseFunc(ret->handler);
1940
0
  xmlFreeSaveCtxt(ret);
1941
0
  return(NULL);
1942
0
    }
1943
0
    return(ret);
1944
0
}
1945
1946
/**
1947
 * Serialize a document.
1948
 *
1949
 * If the save context has no encoding, uses the document's
1950
 * encoding. If the document has no encoding, uses ASCII
1951
 * without an encoding declaration.
1952
 *
1953
 * @param ctxt  a document saving context
1954
 * @param doc  a document
1955
 * @returns 0 on success or -1 in case of error.
1956
 */
1957
long
1958
xmlSaveDoc(xmlSaveCtxt *ctxt, xmlDoc *doc)
1959
13.4k
{
1960
13.4k
    long ret = 0;
1961
1962
13.4k
    if ((ctxt == NULL) || (doc == NULL)) return(-1);
1963
13.4k
    if (xmlSaveDocInternal(ctxt, doc, ctxt->encoding) < 0)
1964
11
        return(-1);
1965
13.3k
    return(ret);
1966
13.4k
}
1967
1968
/**
1969
 * Serialize a subtree starting.
1970
 *
1971
 * If the save context has no encoding, uses UTF-8.
1972
 *
1973
 * @param ctxt  a document saving context
1974
 * @param cur  the root of the subtree to save
1975
 * @returns 0 on success or -1 in case of error.
1976
 */
1977
long
1978
xmlSaveTree(xmlSaveCtxt *ctxt, xmlNode *cur)
1979
4.37k
{
1980
4.37k
    long ret = 0;
1981
1982
4.37k
    if ((ctxt == NULL) || (cur == NULL)) return(-1);
1983
4.29k
#ifdef LIBXML_HTML_ENABLED
1984
4.29k
    if (ctxt->options & XML_SAVE_XHTML) {
1985
0
        xhtmlNodeDumpOutput(ctxt, cur);
1986
0
        return(ret);
1987
0
    }
1988
4.29k
    if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
1989
2.81k
         (cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
1990
2.07k
         ((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
1991
2.21k
        (ctxt->options & XML_SAVE_AS_HTML)) {
1992
2.07k
  htmlNodeDumpOutputInternal(ctxt, cur);
1993
2.07k
  return(ret);
1994
2.07k
    }
1995
2.21k
#endif
1996
2.21k
    xmlNodeDumpOutputInternal(ctxt, cur);
1997
2.21k
    return(ret);
1998
4.29k
}
1999
2000
/**
2001
 * Serialize a notation declaration.
2002
 *
2003
 * @param ctxt  save context
2004
 * @param cur  notation
2005
 * @returns 0 on succes, -1 on error.
2006
 */
2007
int
2008
0
xmlSaveNotationDecl(xmlSaveCtxt *ctxt, xmlNotation *cur) {
2009
0
    if (ctxt == NULL)
2010
0
        return(-1);
2011
0
    xmlBufDumpNotationDecl(ctxt->buf, cur);
2012
0
    return(0);
2013
0
}
2014
2015
/**
2016
 * Serialize notation declarations of a document.
2017
 *
2018
 * @param ctxt  save context
2019
 * @param cur  notation table
2020
 * @returns 0 on succes, -1 on error.
2021
 */
2022
int
2023
499
xmlSaveNotationTable(xmlSaveCtxt *ctxt, xmlNotationTable *cur) {
2024
499
    if (ctxt == NULL)
2025
1
        return(-1);
2026
498
    xmlBufDumpNotationTable(ctxt->buf, cur);
2027
498
    return(0);
2028
499
}
2029
2030
/**
2031
 * Flush a document saving context, i.e. make sure that all
2032
 * buffered input has been processed.
2033
 *
2034
 * @param ctxt  a document saving context
2035
 * @returns the number of bytes written or -1 in case of error.
2036
 */
2037
int
2038
xmlSaveFlush(xmlSaveCtxt *ctxt)
2039
39
{
2040
39
    if (ctxt == NULL) return(-1);
2041
39
    if (ctxt->buf == NULL) return(-1);
2042
39
    return(xmlOutputBufferFlush(ctxt->buf));
2043
39
}
2044
2045
/**
2046
 * Close a document saving context, i.e. make sure that all
2047
 * buffered input has been processed and free the context struct.
2048
 *
2049
 * @param ctxt  a document saving context
2050
 * @returns the number of bytes written or -1 in case of error.
2051
 */
2052
int
2053
xmlSaveClose(xmlSaveCtxt *ctxt)
2054
39
{
2055
39
    int ret;
2056
2057
39
    if (ctxt == NULL) return(-1);
2058
39
    ret = xmlSaveFlush(ctxt);
2059
39
    xmlFreeSaveCtxt(ctxt);
2060
39
    return(ret);
2061
39
}
2062
2063
/**
2064
 * Close a document saving context, i.e. make sure that all
2065
 * buffered input has been processed and free the context struct.
2066
 *
2067
 * @since 2.13.0
2068
 *
2069
 * @param ctxt  a document saving context
2070
 * @returns an xmlParserErrors code.
2071
 */
2072
xmlParserErrors
2073
xmlSaveFinish(xmlSaveCtxt *ctxt)
2074
16.6k
{
2075
16.6k
    int ret;
2076
2077
16.6k
    if (ctxt == NULL)
2078
9
        return(XML_ERR_INTERNAL_ERROR);
2079
2080
16.5k
    ret = xmlOutputBufferClose(ctxt->buf);
2081
16.5k
    ctxt->buf = NULL;
2082
16.5k
    if (ret < 0)
2083
71
        ret = -ret;
2084
16.5k
    else
2085
16.5k
        ret = XML_ERR_OK;
2086
2087
16.5k
    xmlFreeSaveCtxt(ctxt);
2088
16.5k
    return(ret);
2089
16.6k
}
2090
2091
/**
2092
 * Set a custom escaping function to be used for text in element
2093
 * content.
2094
 *
2095
 * @deprecated Don't use.
2096
 *
2097
 * @param ctxt  a document saving context
2098
 * @param escape  the escaping function
2099
 * @returns 0 if successful or -1 in case of error.
2100
 */
2101
int
2102
xmlSaveSetEscape(xmlSaveCtxt *ctxt, xmlCharEncodingOutputFunc escape)
2103
0
{
2104
0
    if (ctxt == NULL) return(-1);
2105
0
    ctxt->escape = escape;
2106
0
    return(0);
2107
0
}
2108
2109
/**
2110
 * Has no effect.
2111
 *
2112
 * @deprecated Don't use.
2113
 *
2114
 * @param ctxt  a document saving context
2115
 * @param escape  the escaping function
2116
 * @returns 0 if successful or -1 in case of error.
2117
 */
2118
int
2119
xmlSaveSetAttrEscape(xmlSaveCtxt *ctxt,
2120
                     xmlCharEncodingOutputFunc escape ATTRIBUTE_UNUSED)
2121
0
{
2122
0
    if (ctxt == NULL) return(-1);
2123
0
    return(0);
2124
0
}
2125
2126
/************************************************************************
2127
 *                  *
2128
 *    Public entry points based on buffers      *
2129
 *                  *
2130
 ************************************************************************/
2131
2132
/**
2133
 * Serialize attribute text to an output buffer.
2134
 *
2135
 * @param buf  output buffer
2136
 * @param doc  the document
2137
 * @param string  the text content
2138
 */
2139
void
2140
xmlBufAttrSerializeTxtContent(xmlOutputBuffer *buf, xmlDoc *doc,
2141
                              const xmlChar *string)
2142
5.85k
{
2143
5.85k
    int flags = XML_ESCAPE_ATTR;
2144
2145
5.85k
    if ((doc == NULL) || (doc->encoding == NULL))
2146
5.64k
        flags |= XML_ESCAPE_NON_ASCII;
2147
5.85k
    xmlSerializeText(buf, string, SIZE_MAX, flags);
2148
5.85k
}
2149
2150
/**
2151
 * Serialize attribute text to an xmlBuffer.
2152
 *
2153
 * @param buf  the XML buffer output
2154
 * @param doc  the document
2155
 * @param attr  the attribute node
2156
 * @param string  the text content
2157
 */
2158
void
2159
xmlAttrSerializeTxtContent(xmlBuffer *buf, xmlDoc *doc,
2160
                           xmlAttr *attr ATTRIBUTE_UNUSED,
2161
                           const xmlChar *string)
2162
7.54k
{
2163
7.54k
    xmlOutputBufferPtr out;
2164
2165
7.54k
    if ((buf == NULL) || (string == NULL))
2166
1.69k
        return;
2167
5.85k
    out = xmlOutputBufferCreateBuffer(buf, NULL);
2168
5.85k
    xmlBufAttrSerializeTxtContent(out, doc, string);
2169
5.85k
    xmlOutputBufferFlush(out);
2170
5.85k
    if ((out == NULL) || (out->error))
2171
18
        xmlFree(xmlBufferDetach(buf));
2172
5.85k
    xmlOutputBufferClose(out);
2173
5.85k
}
2174
2175
/**
2176
 * Serialize an XML node to an xmlBuffer.
2177
 *
2178
 * Uses the document's encoding. If the document has no encoding,
2179
 * uses ASCII without an encoding declaration.
2180
 *
2181
 * Note that `format` only works if the document was parsed with
2182
 * XML_PARSE_NOBLANKS.
2183
 *
2184
 * Since this is using xmlBuffer structures it is limited to 2GB and
2185
 * somewhat deprecated, use #xmlNodeDumpOutput instead.
2186
 *
2187
 * @param buf  the XML buffer output
2188
 * @param doc  the document
2189
 * @param cur  the current node
2190
 * @param level  the initial indenting level
2191
 * @param format  is formatting allowed
2192
 * @returns the number of bytes written to the buffer or -1 in case of error
2193
 */
2194
int
2195
xmlNodeDump(xmlBuffer *buf, xmlDoc *doc, xmlNode *cur, int level,
2196
            int format)
2197
17.4k
{
2198
17.4k
    xmlBufPtr buffer;
2199
17.4k
    size_t ret1;
2200
17.4k
    int ret2;
2201
2202
17.4k
    if ((buf == NULL) || (cur == NULL))
2203
2.53k
        return(-1);
2204
14.8k
    if (level < 0)
2205
3.07k
        level = 0;
2206
11.8k
    else if (level > 100)
2207
2.74k
        level = 100;
2208
14.8k
    buffer = xmlBufFromBuffer(buf);
2209
14.8k
    if (buffer == NULL)
2210
1
        return(-1);
2211
14.8k
    ret1 = xmlBufNodeDump(buffer, doc, cur, level, format);
2212
14.8k
    ret2 = xmlBufBackToBuffer(buffer, buf);
2213
14.8k
    if ((ret1 == (size_t) -1) || (ret2 < 0))
2214
15
        return(-1);
2215
14.8k
    return(ret1 > INT_MAX ? INT_MAX : ret1);
2216
14.8k
}
2217
2218
/**
2219
 * Serialize an XML node to an xmlBuf.
2220
 *
2221
 * Uses the document's encoding. If the document has no encoding,
2222
 * uses ASCII without an encoding declaration.
2223
 *
2224
 * Note that `format` only works if the document was parsed with
2225
 * XML_PARSE_NOBLANKS.
2226
 *
2227
 * @param buf  the XML buffer output
2228
 * @param doc  the document
2229
 * @param cur  the current node
2230
 * @param level  the imbrication level for indenting
2231
 * @param format  is formatting allowed
2232
 * @returns the number of bytes written to the buffer, in case of error 0
2233
 *     is returned or `buf` stores the error
2234
 */
2235
2236
size_t
2237
xmlBufNodeDump(xmlBuf *buf, xmlDoc *doc, xmlNode *cur, int level,
2238
            int format)
2239
14.8k
{
2240
14.8k
    size_t use;
2241
14.8k
    size_t ret;
2242
14.8k
    xmlOutputBufferPtr outbuf;
2243
2244
14.8k
    xmlInitParser();
2245
2246
14.8k
    if (cur == NULL) {
2247
0
        return ((size_t) -1);
2248
0
    }
2249
14.8k
    if (buf == NULL) {
2250
0
        return ((size_t) -1);
2251
0
    }
2252
14.8k
    outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
2253
14.8k
    if (outbuf == NULL) {
2254
1
        xmlSaveErrMemory(NULL);
2255
1
        return ((size_t) -1);
2256
1
    }
2257
14.8k
    memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
2258
14.8k
    outbuf->buffer = buf;
2259
14.8k
    outbuf->encoder = NULL;
2260
14.8k
    outbuf->writecallback = NULL;
2261
14.8k
    outbuf->closecallback = NULL;
2262
14.8k
    outbuf->context = NULL;
2263
14.8k
    outbuf->written = 0;
2264
2265
14.8k
    use = xmlBufUse(buf);
2266
14.8k
    xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
2267
14.8k
    if (outbuf->error)
2268
14
        ret = (size_t) -1;
2269
14.8k
    else
2270
14.8k
        ret = xmlBufUse(buf) - use;
2271
14.8k
    xmlFree(outbuf);
2272
14.8k
    return (ret);
2273
14.8k
}
2274
2275
/**
2276
 * Serialize an XML node to a `FILE`.
2277
 *
2278
 * Uses the document's encoding. If the document has no encoding,
2279
 * uses ASCII without an encoding declaration.
2280
 *
2281
 * @param f  the FILE * for the output
2282
 * @param doc  the document
2283
 * @param cur  the current node
2284
 */
2285
void
2286
xmlElemDump(FILE * f, xmlDoc *doc, xmlNode *cur)
2287
0
{
2288
0
    xmlOutputBufferPtr outbuf;
2289
2290
0
    xmlInitParser();
2291
2292
0
    if (cur == NULL) {
2293
0
        return;
2294
0
    }
2295
2296
0
    outbuf = xmlOutputBufferCreateFile(f, NULL);
2297
0
    if (outbuf == NULL)
2298
0
        return;
2299
0
#ifdef LIBXML_HTML_ENABLED
2300
0
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE))
2301
0
        htmlNodeDumpOutput(outbuf, doc, cur, NULL);
2302
0
    else
2303
0
#endif /* LIBXML_HTML_ENABLED */
2304
0
        xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
2305
0
    xmlOutputBufferClose(outbuf);
2306
0
}
2307
2308
/************************************************************************
2309
 *                  *
2310
 *    Saving functions front-ends       *
2311
 *                  *
2312
 ************************************************************************/
2313
2314
/**
2315
 * Serialize an XML node to an output buffer.
2316
 *
2317
 * If `encoding` is NULL, uses the document's encoding. If the
2318
 * document has no encoding, serializes as ASCII without an
2319
 * encoding declaration.
2320
 *
2321
 * Note that `format` only works if the document was parsed with
2322
 * XML_PARSE_NOBLANKS.
2323
 *
2324
 * @param buf  the XML buffer output
2325
 * @param doc  the document
2326
 * @param cur  the current node
2327
 * @param level  the imbrication level for indenting
2328
 * @param format  is formatting allowed
2329
 * @param encoding  an optional encoding string
2330
 */
2331
void
2332
xmlNodeDumpOutput(xmlOutputBuffer *buf, xmlDoc *doc, xmlNode *cur,
2333
                  int level, int format, const char *encoding)
2334
61.3k
{
2335
61.3k
    xmlSaveCtxt ctxt;
2336
61.3k
    int options;
2337
61.3k
#ifdef LIBXML_HTML_ENABLED
2338
61.3k
    xmlDtdPtr dtd;
2339
61.3k
    int is_xhtml = 0;
2340
61.3k
#endif
2341
2342
61.3k
    (void) doc;
2343
2344
61.3k
    xmlInitParser();
2345
2346
61.3k
    if ((buf == NULL) || (cur == NULL)) return;
2347
2348
60.8k
    if (level < 0)
2349
1.23k
        level = 0;
2350
59.6k
    else if (level > 100)
2351
1.67k
        level = 100;
2352
2353
60.8k
    if (encoding == NULL)
2354
50.5k
        encoding = "UTF-8";
2355
2356
60.8k
    memset(&ctxt, 0, sizeof(ctxt));
2357
60.8k
    ctxt.buf = buf;
2358
60.8k
    ctxt.level = level;
2359
60.8k
    ctxt.encoding = (const xmlChar *) encoding;
2360
2361
60.8k
    options = XML_SAVE_AS_XML;
2362
60.8k
    if (format)
2363
14.2k
        options |= XML_SAVE_FORMAT;
2364
60.8k
    xmlSaveCtxtInit(&ctxt, options);
2365
2366
60.8k
#ifdef LIBXML_HTML_ENABLED
2367
60.8k
    dtd = xmlGetIntSubset(doc);
2368
60.8k
    if (dtd != NULL) {
2369
42.5k
  is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
2370
42.5k
  if (is_xhtml < 0)
2371
7.54k
      is_xhtml = 0;
2372
42.5k
    }
2373
2374
60.8k
    if (is_xhtml)
2375
22.9k
        xhtmlNodeDumpOutput(&ctxt, cur);
2376
37.9k
    else
2377
37.9k
#endif
2378
37.9k
        xmlNodeDumpOutputInternal(&ctxt, cur);
2379
60.8k
}
2380
2381
static void
2382
xmlDocDumpInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding,
2383
35.2k
                   int format) {
2384
35.2k
    xmlSaveCtxt ctxt;
2385
35.2k
    int options;
2386
2387
35.2k
    memset(&ctxt, 0, sizeof(ctxt));
2388
35.2k
    ctxt.buf = buf;
2389
2390
35.2k
    if (buf->encoder != NULL) {
2391
        /*
2392
         * Keep original encoding
2393
         */
2394
0
        encoding = buf->encoder->name;
2395
0
        ctxt.encoding = BAD_CAST encoding;
2396
0
    }
2397
2398
35.2k
    options = XML_SAVE_AS_XML;
2399
35.2k
    if (format)
2400
11.4k
        options |= XML_SAVE_FORMAT;
2401
35.2k
    xmlSaveCtxtInit(&ctxt, options);
2402
2403
35.2k
    xmlSaveDocInternal(&ctxt, doc, (const xmlChar *) encoding);
2404
35.2k
}
2405
2406
/**
2407
 * Serialize an XML document to memory.
2408
 *
2409
 * If `encoding` is NULL, uses the document's encoding. If the
2410
 * document has no encoding, serializes as ASCII without an
2411
 * encoding declaration.
2412
 *
2413
 * It is up to the caller of this function to free the returned
2414
 * memory with #xmlFree.
2415
 *
2416
 * Note that `format` only works if the document was parsed with
2417
 * XML_PARSE_NOBLANKS.
2418
 *
2419
 * @param out_doc  Document to generate XML text from
2420
 * @param doc_txt_ptr  Memory pointer for allocated XML text
2421
 * @param doc_txt_len  Length of the generated XML text
2422
 * @param txt_encoding  Character encoding to use when generating XML text
2423
 * @param format  should formatting spaces been added
2424
 */
2425
2426
void
2427
xmlDocDumpFormatMemoryEnc(xmlDoc *out_doc, xmlChar **doc_txt_ptr,
2428
    int * doc_txt_len, const char * txt_encoding,
2429
28.2k
    int format) {
2430
28.2k
    xmlOutputBufferPtr buf = NULL;
2431
2432
28.2k
    if (doc_txt_len != NULL)
2433
28.2k
        *doc_txt_len = 0;
2434
2435
28.2k
    if (doc_txt_ptr == NULL)
2436
0
        return;
2437
28.2k
    *doc_txt_ptr = NULL;
2438
2439
28.2k
    if (out_doc == NULL)
2440
2.38k
        return;
2441
2442
25.8k
    buf = xmlAllocOutputBuffer(NULL);
2443
25.8k
    if (buf == NULL) {
2444
7
        xmlSaveErrMemory(NULL);
2445
7
        return;
2446
7
    }
2447
2448
25.8k
    xmlDocDumpInternal(buf, out_doc, txt_encoding, format);
2449
2450
25.8k
    xmlOutputBufferFlush(buf);
2451
2452
25.8k
    if (!buf->error) {
2453
23.5k
        if (doc_txt_len != NULL)
2454
23.5k
            *doc_txt_len = xmlBufUse(buf->buffer);
2455
23.5k
        *doc_txt_ptr = xmlBufDetach(buf->buffer);
2456
23.5k
    }
2457
2458
25.8k
    xmlOutputBufferClose(buf);
2459
25.8k
}
2460
2461
/**
2462
 * Same as #xmlDocDumpFormatMemoryEnc with `encoding` set to
2463
 * NULL and `format` set to 0.
2464
 *
2465
 * @param cur  the document
2466
 * @param mem  OUT: the memory pointer
2467
 * @param size  OUT: the memory length
2468
 */
2469
void
2470
11.9k
xmlDocDumpMemory(xmlDoc *cur, xmlChar**mem, int *size) {
2471
11.9k
    xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2472
11.9k
}
2473
2474
/**
2475
 * Same as #xmlDocDumpFormatMemoryEnc with `encoding` set to
2476
 * NULL.
2477
 *
2478
 * @param cur  the document
2479
 * @param mem  OUT: the memory pointer
2480
 * @param size  OUT: the memory length
2481
 * @param format  should formatting spaces been added
2482
 */
2483
void
2484
10.3k
xmlDocDumpFormatMemory(xmlDoc *cur, xmlChar**mem, int *size, int format) {
2485
10.3k
    xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2486
10.3k
}
2487
2488
/**
2489
 * Same as #xmlDocDumpFormatMemoryEnc with `format` set to 0.
2490
 *
2491
 * @param out_doc  Document to generate XML text from
2492
 * @param doc_txt_ptr  Memory pointer for allocated XML text
2493
 * @param doc_txt_len  Length of the generated XML text
2494
 * @param txt_encoding  Character encoding to use when generating XML text
2495
 */
2496
2497
void
2498
xmlDocDumpMemoryEnc(xmlDoc *out_doc, xmlChar **doc_txt_ptr,
2499
3.46k
              int * doc_txt_len, const char * txt_encoding) {
2500
3.46k
    xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2501
3.46k
                        txt_encoding, 0);
2502
3.46k
}
2503
2504
/**
2505
 * Serialize an XML document to a `FILE`.
2506
 *
2507
 * Uses the document's encoding. If the document has no encoding,
2508
 * uses ASCII without an encoding declaration.
2509
 *
2510
 * Note that `format` only works if the document was parsed with
2511
 * XML_PARSE_NOBLANKS.
2512
 *
2513
 * @param f  the FILE*
2514
 * @param cur  the document
2515
 * @param format  should formatting spaces been added
2516
 * @returns the number of bytes written or -1 in case of failure.
2517
 */
2518
int
2519
0
xmlDocFormatDump(FILE *f, xmlDoc *cur, int format) {
2520
0
    xmlOutputBufferPtr buf;
2521
2522
0
    if (cur == NULL) {
2523
0
  return(-1);
2524
0
    }
2525
2526
0
    buf = xmlOutputBufferCreateFile(f, NULL);
2527
0
    if (buf == NULL) return(-1);
2528
2529
0
    xmlDocDumpInternal(buf, cur, NULL, format);
2530
2531
0
    return(xmlOutputBufferClose(buf));
2532
0
}
2533
2534
/**
2535
 * Serialize an XML document to a `FILE`.
2536
 *
2537
 * Uses the document's encoding. If the document has no encoding,
2538
 * uses ASCII without an encoding declaration.
2539
 *
2540
 * @param f  the FILE*
2541
 * @param cur  the document
2542
 * @returns the number of bytes written or -1 in case of failure.
2543
 */
2544
int
2545
0
xmlDocDump(FILE *f, xmlDoc *cur) {
2546
0
    return(xmlDocFormatDump (f, cur, 0));
2547
0
}
2548
2549
/**
2550
 * Same as #xmlSaveFormatFileTo with `format` set to 0.
2551
 *
2552
 * WARNING: This calls #xmlOutputBufferClose and frees `buf`.
2553
 *
2554
 * @param buf  an output I/O buffer
2555
 * @param cur  the document
2556
 * @param encoding  the encoding if any assuming the I/O layer handles the transcoding
2557
 * @returns the number of bytes written or -1 in case of failure.
2558
 */
2559
int
2560
5.10k
xmlSaveFileTo(xmlOutputBuffer *buf, xmlDoc *cur, const char *encoding) {
2561
5.10k
    return(xmlSaveFormatFileTo(buf, cur, encoding, 0));
2562
5.10k
}
2563
2564
/**
2565
 * Serialize an XML document to an output buffer.
2566
 *
2567
 * If the output buffer already uses a (non-default) encoding,
2568
 * `encoding` is ignored. If the output buffer has no encoding
2569
 * and `encoding` is NULL, uses the document's encoding or
2570
 * ASCII without an encoding declaration.
2571
 *
2572
 * Note that `format` only works if the document was parsed with
2573
 * XML_PARSE_NOBLANKS.
2574
 *
2575
 * WARNING: This calls #xmlOutputBufferClose and frees `buf`.
2576
 *
2577
 * @param buf  an output I/O buffer
2578
 * @param cur  the document
2579
 * @param encoding  the encoding if any assuming the I/O layer handles the transcoding
2580
 * @param format  should formatting spaces been added
2581
 * @returns the number of bytes written or -1 in case of failure.
2582
 */
2583
int
2584
xmlSaveFormatFileTo(xmlOutputBuffer *buf, xmlDoc *cur,
2585
                    const char *encoding, int format)
2586
10.9k
{
2587
10.9k
    if (buf == NULL) return(-1);
2588
10.8k
    if ((cur == NULL) ||
2589
9.43k
        ((cur->type != XML_DOCUMENT_NODE) &&
2590
4.68k
   (cur->type != XML_HTML_DOCUMENT_NODE))) {
2591
1.46k
        xmlOutputBufferClose(buf);
2592
1.46k
  return(-1);
2593
1.46k
    }
2594
2595
9.43k
    xmlDocDumpInternal(buf, cur, encoding, format);
2596
2597
9.43k
    return(xmlOutputBufferClose(buf));
2598
10.8k
}
2599
2600
/**
2601
 * Serialize an XML document to a file using the given encoding.
2602
 * If `filename` is `"-"`, stdout is used. This is potentially
2603
 * insecure and might be changed in a future version.
2604
 *
2605
 * If `encoding` is NULL, uses the document's encoding. If the
2606
 * document has no encoding, serializes as ASCII without an
2607
 * encoding declaration.
2608
 *
2609
 * Note that `format` only works if the document was parsed with
2610
 * XML_PARSE_NOBLANKS.
2611
 *
2612
 * @param filename  the filename or URL to output
2613
 * @param cur  the document being saved
2614
 * @param encoding  the name of the encoding to use or NULL.
2615
 * @param format  should formatting spaces be added.
2616
 * @returns the number of bytes written or -1 in case of error.
2617
 */
2618
int
2619
xmlSaveFormatFileEnc( const char * filename, xmlDoc *cur,
2620
6
      const char * encoding, int format ) {
2621
6
    xmlOutputBufferPtr buf;
2622
2623
6
    if (cur == NULL)
2624
0
  return(-1);
2625
2626
6
#ifdef LIBXML_ZLIB_ENABLED
2627
6
    if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2628
6
#endif
2629
    /*
2630
     * save the content to a temp buffer.
2631
     */
2632
6
    buf = xmlOutputBufferCreateFilename(filename, NULL, cur->compression);
2633
6
    if (buf == NULL) return(-1);
2634
2635
6
    xmlDocDumpInternal(buf, cur, encoding, format);
2636
2637
6
    return(xmlOutputBufferClose(buf));
2638
6
}
2639
2640
2641
/**
2642
 * Same as #xmlSaveFormatFileEnc with `format` set to 0.
2643
 *
2644
 * @param filename  the filename (or URL)
2645
 * @param cur  the document
2646
 * @param encoding  the name of an encoding (or NULL)
2647
 * @returns the number of bytes written or -1 in case of failure.
2648
 */
2649
int
2650
0
xmlSaveFileEnc(const char *filename, xmlDoc *cur, const char *encoding) {
2651
0
    return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2652
0
}
2653
2654
/**
2655
 * Same as #xmlSaveFormatFileEnc with `encoding` set to NULL.
2656
 *
2657
 * @param filename  the filename (or URL)
2658
 * @param cur  the document
2659
 * @param format  should formatting spaces been added
2660
 * @returns the number of bytes written or -1 in case of failure.
2661
 */
2662
int
2663
0
xmlSaveFormatFile(const char *filename, xmlDoc *cur, int format) {
2664
0
    return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2665
0
}
2666
2667
/**
2668
 * Same as #xmlSaveFormatFileEnc with `encoding` set to NULL
2669
 * and `format` set to 0.
2670
 *
2671
 * @param filename  the filename (or URL)
2672
 * @param cur  the document
2673
 * @returns the number of bytes written or -1 in case of failure.
2674
 */
2675
int
2676
6
xmlSaveFile(const char *filename, xmlDoc *cur) {
2677
    return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2678
6
}
2679
2680
#endif /* LIBXML_OUTPUT_ENABLED */
2681