Coverage Report

Created: 2025-07-01 06:27

/src/libxml2/xmlsave.c
Line
Count
Source (jump to first uncovered line)
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
3.60k
#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
0
#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
53
{
61
53
    if (out != NULL)
62
0
        out->error = XML_ERR_NO_MEMORY;
63
53
    xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_OUTPUT, NULL);
64
53
}
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
125
{
78
125
    const char *msg = NULL;
79
125
    int res;
80
81
    /* Don't overwrite catastrophic errors */
82
125
    if ((out != NULL) &&
83
125
        (out->error != XML_ERR_OK) &&
84
125
        (xmlIsCatastrophicError(XML_ERR_FATAL, out->error)))
85
0
        return;
86
87
125
    if (code == XML_ERR_NO_MEMORY) {
88
53
        xmlSaveErrMemory(out);
89
53
        return;
90
53
    }
91
92
72
    if (out != NULL)
93
0
        out->error = code;
94
95
72
    if (code == XML_ERR_UNSUPPORTED_ENCODING) {
96
72
        msg = "Unsupported encoding: %s";
97
72
    } else {
98
0
        msg = xmlErrString(code);
99
0
        extra = NULL;
100
0
    }
101
102
72
    res = xmlRaiseError(NULL, NULL, NULL, NULL, node,
103
72
                        XML_FROM_OUTPUT, code, XML_ERR_ERROR, NULL, 0,
104
72
                        extra, NULL, NULL, 0, 0,
105
72
                        msg, extra);
106
72
    if (res < 0)
107
0
        xmlSaveErrMemory(out);
108
72
}
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
1.80k
xmlSaveSetIndentString(xmlSaveCtxt *ctxt, const char *indent) {
127
1.80k
    size_t len;
128
1.80k
    int i;
129
130
1.80k
    if ((ctxt == NULL) || (indent == NULL))
131
0
        return(-1);
132
133
1.80k
    len = strlen(indent);
134
1.80k
    if ((len <= 0) || (len > MAX_INDENT))
135
0
        return(-1);
136
137
1.80k
    ctxt->indent_size = len;
138
1.80k
    ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
139
55.8k
    for (i = 0; i < ctxt->indent_nr; i++)
140
54.0k
        memcpy(&ctxt->indent[i * ctxt->indent_size], indent, len);
141
142
1.80k
    return(0);
143
1.80k
}
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
1.80k
{
154
1.80k
    if (ctxt == NULL) return;
155
156
1.80k
    xmlSaveSetIndentString(ctxt, xmlTreeIndentString);
157
158
1.80k
    if (options & XML_SAVE_FORMAT)
159
11
        ctxt->format = 1;
160
1.79k
    else if (options & XML_SAVE_WSNONSIG)
161
6
        ctxt->format = 2;
162
163
1.80k
    if (((options & XML_SAVE_EMPTY) == 0) &&
164
1.80k
        (xmlSaveNoEmptyTags))
165
0
  options |= XML_SAVE_NO_EMPTY;
166
167
1.80k
    ctxt->options = options;
168
1.80k
}
169
170
/**
171
 * Free a saving context, destroying the output in any remaining buffer
172
 */
173
static void
174
xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
175
159
{
176
159
    if (ctxt == NULL) return;
177
159
    if (ctxt->encoding != NULL)
178
1
        xmlFree((char *) ctxt->encoding);
179
159
    if (ctxt->buf != NULL)
180
34
        xmlOutputBufferClose(ctxt->buf);
181
159
    xmlFree(ctxt);
182
159
}
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
159
{
192
159
    xmlSaveCtxtPtr ret;
193
194
159
    ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
195
159
    if (ret == NULL) {
196
0
  xmlSaveErrMemory(NULL);
197
0
  return ( NULL );
198
0
    }
199
159
    memset(ret, 0, sizeof(xmlSaveCtxt));
200
201
159
    if (encoding != NULL) {
202
126
        xmlParserErrors res;
203
204
126
        res = xmlOpenCharEncodingHandler(encoding, /* output */ 1,
205
126
                                         &ret->handler);
206
126
  if (res != XML_ERR_OK) {
207
125
      xmlSaveErr(NULL, res, NULL, encoding);
208
125
            xmlFreeSaveCtxt(ret);
209
125
      return(NULL);
210
125
  }
211
1
        ret->encoding = xmlStrdup((const xmlChar *)encoding);
212
1
    }
213
214
34
    xmlSaveCtxtInit(ret, options);
215
216
34
    return(ret);
217
159
}
218
219
/************************************************************************
220
 *                  *
221
 *    Dumping XML tree content to a simple buffer   *
222
 *                  *
223
 ************************************************************************/
224
225
static void
226
1.77k
xmlSaveWriteText(xmlSaveCtxt *ctxt, const xmlChar *text, unsigned flags) {
227
1.77k
    if (ctxt->encoding == NULL)
228
42
        flags |= XML_ESCAPE_NON_ASCII;
229
230
1.77k
    xmlSerializeText(ctxt->buf, text, SIZE_MAX, flags);
231
1.77k
}
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
0
{
242
0
    xmlNodePtr children;
243
0
    xmlOutputBufferPtr buf = ctxt->buf;
244
245
0
    children = attr->children;
246
0
    while (children != NULL) {
247
0
        switch (children->type) {
248
0
            case XML_TEXT_NODE:
249
0
          xmlSaveWriteText(ctxt, children->content, XML_ESCAPE_ATTR);
250
0
    break;
251
0
            case XML_ENTITY_REF_NODE:
252
0
                xmlOutputBufferWrite(buf, 1, "&");
253
0
                xmlOutputBufferWriteString(buf, (const char *) children->name);
254
0
                xmlOutputBufferWrite(buf, 1, ";");
255
0
                break;
256
0
            default:
257
                /* should not happen unless we have a badly built tree */
258
0
                break;
259
0
        }
260
0
        children = children->next;
261
0
    }
262
0
}
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
0
xmlBufDumpNotationDecl(xmlOutputBufferPtr buf, xmlNotationPtr nota) {
272
0
    xmlOutputBufferWrite(buf, 11, "<!NOTATION ");
273
0
    xmlOutputBufferWriteString(buf, (const char *) nota->name);
274
275
0
    if (nota->PublicID != NULL) {
276
0
  xmlOutputBufferWrite(buf, 8, " PUBLIC ");
277
0
  xmlOutputBufferWriteQuotedString(buf, nota->PublicID);
278
0
  if (nota->SystemID != NULL) {
279
0
      xmlOutputBufferWrite(buf, 1, " ");
280
0
      xmlOutputBufferWriteQuotedString(buf, nota->SystemID);
281
0
  }
282
0
    } else {
283
0
  xmlOutputBufferWrite(buf, 8, " SYSTEM ");
284
0
  xmlOutputBufferWriteQuotedString(buf, nota->SystemID);
285
0
    }
286
287
0
    xmlOutputBufferWrite(buf, 3, " >\n");
288
0
}
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
0
                           const xmlChar *name ATTRIBUTE_UNUSED) {
300
0
    xmlBufDumpNotationDecl((xmlOutputBufferPtr) buf, (xmlNotationPtr) nota);
301
0
}
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
0
xmlBufDumpNotationTable(xmlOutputBufferPtr buf, xmlNotationTablePtr table) {
311
0
    xmlHashScan(table, xmlBufDumpNotationDeclScan, buf);
312
0
}
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
0
xmlBufDumpElementOccur(xmlOutputBufferPtr buf, xmlElementContentPtr cur) {
322
0
    switch (cur->ocur) {
323
0
        case XML_ELEMENT_CONTENT_ONCE:
324
0
            break;
325
0
        case XML_ELEMENT_CONTENT_OPT:
326
0
            xmlOutputBufferWrite(buf, 1, "?");
327
0
            break;
328
0
        case XML_ELEMENT_CONTENT_MULT:
329
0
            xmlOutputBufferWrite(buf, 1, "*");
330
0
            break;
331
0
        case XML_ELEMENT_CONTENT_PLUS:
332
0
            xmlOutputBufferWrite(buf, 1, "+");
333
0
            break;
334
0
    }
335
0
}
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
0
                         xmlElementContentPtr content) {
346
0
    xmlElementContentPtr cur;
347
348
0
    if (content == NULL) return;
349
350
0
    xmlOutputBufferWrite(buf, 1, "(");
351
0
    cur = content;
352
353
0
    do {
354
0
        if (cur == NULL) return;
355
356
0
        switch (cur->type) {
357
0
            case XML_ELEMENT_CONTENT_PCDATA:
358
0
                xmlOutputBufferWrite(buf, 7, "#PCDATA");
359
0
                break;
360
0
            case XML_ELEMENT_CONTENT_ELEMENT:
361
0
                if (cur->prefix != NULL) {
362
0
                    xmlOutputBufferWriteString(buf,
363
0
                            (const char *) cur->prefix);
364
0
                    xmlOutputBufferWrite(buf, 1, ":");
365
0
                }
366
0
                xmlOutputBufferWriteString(buf, (const char *) cur->name);
367
0
                break;
368
0
            case XML_ELEMENT_CONTENT_SEQ:
369
0
            case XML_ELEMENT_CONTENT_OR:
370
0
                if ((cur != content) &&
371
0
                    (cur->parent != NULL) &&
372
0
                    ((cur->type != cur->parent->type) ||
373
0
                     (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
374
0
                    xmlOutputBufferWrite(buf, 1, "(");
375
0
                cur = cur->c1;
376
0
                continue;
377
0
        }
378
379
0
        while (cur != content) {
380
0
            xmlElementContentPtr parent = cur->parent;
381
382
0
            if (parent == NULL) return;
383
384
0
            if (((cur->type == XML_ELEMENT_CONTENT_OR) ||
385
0
                 (cur->type == XML_ELEMENT_CONTENT_SEQ)) &&
386
0
                ((cur->type != parent->type) ||
387
0
                 (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
388
0
                xmlOutputBufferWrite(buf, 1, ")");
389
0
            xmlBufDumpElementOccur(buf, cur);
390
391
0
            if (cur == parent->c1) {
392
0
                if (parent->type == XML_ELEMENT_CONTENT_SEQ)
393
0
                    xmlOutputBufferWrite(buf, 3, " , ");
394
0
                else if (parent->type == XML_ELEMENT_CONTENT_OR)
395
0
                    xmlOutputBufferWrite(buf, 3, " | ");
396
397
0
                cur = parent->c2;
398
0
                break;
399
0
            }
400
401
0
            cur = parent;
402
0
        }
403
0
    } while (cur != content);
404
405
0
    xmlOutputBufferWrite(buf, 1, ")");
406
0
    xmlBufDumpElementOccur(buf, content);
407
0
}
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
0
xmlBufDumpElementDecl(xmlOutputBufferPtr buf, xmlElementPtr elem) {
418
0
    xmlOutputBufferWrite(buf, 10, "<!ELEMENT ");
419
0
    if (elem->prefix != NULL) {
420
0
        xmlOutputBufferWriteString(buf, (const char *) elem->prefix);
421
0
        xmlOutputBufferWrite(buf, 1, ":");
422
0
    }
423
0
    xmlOutputBufferWriteString(buf, (const char *) elem->name);
424
0
    xmlOutputBufferWrite(buf, 1, " ");
425
426
0
    switch (elem->etype) {
427
0
  case XML_ELEMENT_TYPE_EMPTY:
428
0
      xmlOutputBufferWrite(buf, 5, "EMPTY");
429
0
      break;
430
0
  case XML_ELEMENT_TYPE_ANY:
431
0
      xmlOutputBufferWrite(buf, 3, "ANY");
432
0
      break;
433
0
  case XML_ELEMENT_TYPE_MIXED:
434
0
  case XML_ELEMENT_TYPE_ELEMENT:
435
0
      xmlBufDumpElementContent(buf, elem->content);
436
0
      break;
437
0
        default:
438
            /* assert(0); */
439
0
            break;
440
0
    }
441
442
0
    xmlOutputBufferWrite(buf, 2, ">\n");
443
0
}
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
0
xmlBufDumpEnumeration(xmlOutputBufferPtr buf, xmlEnumerationPtr cur) {
453
0
    while (cur != NULL) {
454
0
        xmlOutputBufferWriteString(buf, (const char *) cur->name);
455
0
        if (cur->next != NULL)
456
0
            xmlOutputBufferWrite(buf, 3, " | ");
457
458
0
        cur = cur->next;
459
0
    }
460
461
0
    xmlOutputBufferWrite(buf, 1, ")");
462
0
}
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
0
xmlSaveWriteAttributeDecl(xmlSaveCtxtPtr ctxt, xmlAttributePtr attr) {
472
0
    xmlOutputBufferPtr buf = ctxt->buf;
473
474
0
    xmlOutputBufferWrite(buf, 10, "<!ATTLIST ");
475
0
    xmlOutputBufferWriteString(buf, (const char *) attr->elem);
476
0
    xmlOutputBufferWrite(buf, 1, " ");
477
0
    if (attr->prefix != NULL) {
478
0
  xmlOutputBufferWriteString(buf, (const char *) attr->prefix);
479
0
  xmlOutputBufferWrite(buf, 1, ":");
480
0
    }
481
0
    xmlOutputBufferWriteString(buf, (const char *) attr->name);
482
483
0
    switch (attr->atype) {
484
0
  case XML_ATTRIBUTE_CDATA:
485
0
      xmlOutputBufferWrite(buf, 6, " CDATA");
486
0
      break;
487
0
  case XML_ATTRIBUTE_ID:
488
0
      xmlOutputBufferWrite(buf, 3, " ID");
489
0
      break;
490
0
  case XML_ATTRIBUTE_IDREF:
491
0
      xmlOutputBufferWrite(buf, 6, " IDREF");
492
0
      break;
493
0
  case XML_ATTRIBUTE_IDREFS:
494
0
      xmlOutputBufferWrite(buf, 7, " IDREFS");
495
0
      break;
496
0
  case XML_ATTRIBUTE_ENTITY:
497
0
      xmlOutputBufferWrite(buf, 7, " ENTITY");
498
0
      break;
499
0
  case XML_ATTRIBUTE_ENTITIES:
500
0
      xmlOutputBufferWrite(buf, 9, " ENTITIES");
501
0
      break;
502
0
  case XML_ATTRIBUTE_NMTOKEN:
503
0
      xmlOutputBufferWrite(buf, 8, " NMTOKEN");
504
0
      break;
505
0
  case XML_ATTRIBUTE_NMTOKENS:
506
0
      xmlOutputBufferWrite(buf, 9, " NMTOKENS");
507
0
      break;
508
0
  case XML_ATTRIBUTE_ENUMERATION:
509
0
      xmlOutputBufferWrite(buf, 2, " (");
510
0
      xmlBufDumpEnumeration(buf, attr->tree);
511
0
      break;
512
0
  case XML_ATTRIBUTE_NOTATION:
513
0
      xmlOutputBufferWrite(buf, 11, " NOTATION (");
514
0
      xmlBufDumpEnumeration(buf, attr->tree);
515
0
      break;
516
0
  default:
517
            /* assert(0); */
518
0
            break;
519
0
    }
520
521
0
    switch (attr->def) {
522
0
  case XML_ATTRIBUTE_NONE:
523
0
      break;
524
0
  case XML_ATTRIBUTE_REQUIRED:
525
0
      xmlOutputBufferWrite(buf, 10, " #REQUIRED");
526
0
      break;
527
0
  case XML_ATTRIBUTE_IMPLIED:
528
0
      xmlOutputBufferWrite(buf, 9, " #IMPLIED");
529
0
      break;
530
0
  case XML_ATTRIBUTE_FIXED:
531
0
      xmlOutputBufferWrite(buf, 7, " #FIXED");
532
0
      break;
533
0
  default:
534
            /* assert(0); */
535
0
            break;
536
0
    }
537
538
0
    if (attr->defaultValue != NULL) {
539
0
        xmlOutputBufferWrite(buf, 2, " \"");
540
0
        xmlSaveWriteText(ctxt, attr->defaultValue, XML_ESCAPE_ATTR);
541
0
        xmlOutputBufferWrite(buf, 1, "\"");
542
0
    }
543
544
0
    xmlOutputBufferWrite(buf, 2, ">\n");
545
0
}
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
0
xmlBufDumpEntityContent(xmlOutputBufferPtr buf, const xmlChar *content) {
556
0
    const char * base, *cur;
557
558
0
    if (content == NULL)
559
0
        return;
560
561
0
    xmlOutputBufferWrite(buf, 1, "\"");
562
0
    base = cur = (const char *) content;
563
0
    while (*cur != 0) {
564
0
        if (*cur == '"') {
565
0
            if (base != cur)
566
0
                xmlOutputBufferWrite(buf, cur - base, base);
567
0
            xmlOutputBufferWrite(buf, 6, "&quot;");
568
0
            cur++;
569
0
            base = cur;
570
0
        } else if (*cur == '%') {
571
0
            if (base != cur)
572
0
                xmlOutputBufferWrite(buf, cur - base, base);
573
0
            xmlOutputBufferWrite(buf, 6, "&#x25;");
574
0
            cur++;
575
0
            base = cur;
576
0
        } else {
577
0
            cur++;
578
0
        }
579
0
    }
580
0
    if (base != cur)
581
0
        xmlOutputBufferWrite(buf, cur - base, base);
582
0
    xmlOutputBufferWrite(buf, 1, "\"");
583
0
}
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
0
xmlBufDumpEntityDecl(xmlOutputBufferPtr buf, xmlEntityPtr ent) {
593
0
    if ((ent->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
594
0
        (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY))
595
0
        xmlOutputBufferWrite(buf, 11, "<!ENTITY % ");
596
0
    else
597
0
        xmlOutputBufferWrite(buf, 9, "<!ENTITY ");
598
0
    xmlOutputBufferWriteString(buf, (const char *) ent->name);
599
0
    xmlOutputBufferWrite(buf, 1, " ");
600
601
0
    if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) ||
602
0
        (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) ||
603
0
        (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
604
0
        if (ent->ExternalID != NULL) {
605
0
             xmlOutputBufferWrite(buf, 7, "PUBLIC ");
606
0
             xmlOutputBufferWriteQuotedString(buf, ent->ExternalID);
607
0
             xmlOutputBufferWrite(buf, 1, " ");
608
0
        } else {
609
0
             xmlOutputBufferWrite(buf, 7, "SYSTEM ");
610
0
        }
611
0
        xmlOutputBufferWriteQuotedString(buf, ent->SystemID);
612
0
    }
613
614
0
    if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
615
0
        if (ent->content != NULL) { /* Should be true ! */
616
0
            xmlOutputBufferWrite(buf, 7, " NDATA ");
617
0
            if (ent->orig != NULL)
618
0
                xmlOutputBufferWriteString(buf, (const char *) ent->orig);
619
0
            else
620
0
                xmlOutputBufferWriteString(buf, (const char *) ent->content);
621
0
        }
622
0
    }
623
624
0
    if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
625
0
        (ent->etype == XML_INTERNAL_PARAMETER_ENTITY)) {
626
        /*
627
         * We could save the original quote character and avoid
628
         * calling xmlOutputBufferWriteQuotedString here.
629
         */
630
0
        if (ent->orig != NULL)
631
0
            xmlOutputBufferWriteQuotedString(buf, ent->orig);
632
0
        else
633
0
            xmlBufDumpEntityContent(buf, ent->content);
634
0
    }
635
636
0
    xmlOutputBufferWrite(buf, 2, ">\n");
637
0
}
638
639
/************************************************************************
640
 *                  *
641
 *    Dumping XML tree content to an I/O output buffer  *
642
 *                  *
643
 ************************************************************************/
644
645
static int
646
0
xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
647
0
    xmlOutputBufferPtr buf = ctxt->buf;
648
0
    xmlCharEncodingHandler *handler;
649
0
    xmlParserErrors res;
650
651
    /* shouldn't happen */
652
0
    if ((buf->encoder != NULL) || (buf->conv != NULL))
653
0
        return(-1);
654
655
0
    res = xmlOpenCharEncodingHandler(encoding, /* output */ 1, &handler);
656
0
    if (res != XML_ERR_OK) {
657
0
        xmlSaveErr(buf, res, NULL, encoding);
658
0
        return(-1);
659
0
    }
660
661
0
    if (handler != NULL) {
662
0
        xmlBufPtr newbuf;
663
664
0
        newbuf = xmlBufCreate(4000 /* MINLEN */);
665
0
        if (newbuf == NULL) {
666
0
            xmlCharEncCloseFunc(handler);
667
0
            xmlSaveErrMemory(buf);
668
0
            return(-1);
669
0
        }
670
671
0
        buf->conv = buf->buffer;
672
0
        buf->buffer = newbuf;
673
0
        buf->encoder = handler;
674
0
    }
675
676
0
    ctxt->encoding = (const xmlChar *) encoding;
677
678
    /*
679
     * initialize the state, e.g. if outputting a BOM
680
     */
681
0
    xmlCharEncOutput(buf, 1);
682
683
0
    return(0);
684
0
}
685
686
static int
687
0
xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) {
688
0
    xmlOutputBufferPtr buf = ctxt->buf;
689
690
0
    xmlOutputBufferFlush(buf);
691
692
0
    if (buf->encoder != NULL) {
693
0
        xmlCharEncCloseFunc(buf->encoder);
694
0
        buf->encoder = NULL;
695
0
        xmlBufFree(buf->buffer);
696
0
        buf->buffer = buf->conv;
697
0
        buf->conv = NULL;
698
0
    }
699
700
0
    ctxt->encoding = NULL;
701
702
0
    return(0);
703
0
}
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
0
{
718
0
    int level;
719
720
0
    if ((ctxt->options & XML_SAVE_NO_INDENT) ||
721
0
        (((ctxt->options & XML_SAVE_INDENT) == 0) &&
722
0
         (xmlIndentTreeOutput == 0)))
723
0
        return;
724
725
0
    level = ctxt->level + extra;
726
0
    if (level > ctxt->indent_nr)
727
0
        level = ctxt->indent_nr;
728
0
    xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size * level, ctxt->indent);
729
0
}
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
28
xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
763
28
    unsigned escapeFlags = XML_ESCAPE_ATTR;
764
765
28
    if ((cur == NULL) || (buf == NULL)) return;
766
767
28
    if ((ctxt == NULL) || (ctxt->encoding == NULL))
768
0
        escapeFlags |= XML_ESCAPE_NON_ASCII;
769
770
28
    if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
771
28
  if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
772
28
      return;
773
774
0
  if (ctxt != NULL && ctxt->format == 2)
775
0
      xmlOutputBufferWriteWSNonSig(ctxt, 2);
776
0
  else
777
0
      xmlOutputBufferWrite(buf, 1, " ");
778
779
        /* Within the context of an element attributes */
780
0
  if (cur->prefix != NULL) {
781
0
      xmlOutputBufferWrite(buf, 6, "xmlns:");
782
0
      xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
783
0
  } else
784
0
      xmlOutputBufferWrite(buf, 5, "xmlns");
785
0
        xmlOutputBufferWrite(buf, 2, "=\"");
786
0
        xmlSerializeText(buf, cur->href, SIZE_MAX, escapeFlags);
787
0
        xmlOutputBufferWrite(buf, 1, "\"");
788
0
    }
789
28
}
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
0
xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
800
0
    while (cur != NULL) {
801
0
        xmlNsDumpOutput(ctxt->buf, cur, ctxt);
802
0
  cur = cur->next;
803
0
    }
804
0
}
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
0
xmlNsListDumpOutput(xmlOutputBuffer *buf, xmlNs *cur) {
814
0
    while (cur != NULL) {
815
0
        xmlNsDumpOutput(buf, cur, NULL);
816
0
  cur = cur->next;
817
0
    }
818
0
}
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
0
xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
828
0
    xmlOutputBufferPtr buf;
829
0
    xmlNodePtr cur;
830
0
    int format, level;
831
832
0
    if (dtd == NULL) return;
833
0
    if ((ctxt == NULL) || (ctxt->buf == NULL))
834
0
        return;
835
0
    buf = ctxt->buf;
836
0
    xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
837
0
    xmlOutputBufferWriteString(buf, (const char *)dtd->name);
838
0
    if (dtd->ExternalID != NULL) {
839
0
  xmlOutputBufferWrite(buf, 8, " PUBLIC ");
840
0
  xmlOutputBufferWriteQuotedString(buf, dtd->ExternalID);
841
0
  xmlOutputBufferWrite(buf, 1, " ");
842
0
  xmlOutputBufferWriteQuotedString(buf, dtd->SystemID);
843
0
    }  else if (dtd->SystemID != NULL) {
844
0
  xmlOutputBufferWrite(buf, 8, " SYSTEM ");
845
0
  xmlOutputBufferWriteQuotedString(buf, dtd->SystemID);
846
0
    }
847
0
    if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
848
0
        (dtd->attributes == NULL) && (dtd->notations == NULL) &&
849
0
  (dtd->pentities == NULL)) {
850
0
  xmlOutputBufferWrite(buf, 1, ">");
851
0
  return;
852
0
    }
853
0
    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
0
    if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
859
0
        (dtd->doc->intSubset == dtd))) {
860
0
        xmlBufDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
861
0
    }
862
0
    format = ctxt->format;
863
0
    level = ctxt->level;
864
0
    ctxt->format = 0;
865
0
    ctxt->level = -1;
866
0
    for (cur = dtd->children; cur != NULL; cur = cur->next) {
867
0
        xmlNodeDumpOutputInternal(ctxt, cur);
868
0
    }
869
0
    ctxt->format = format;
870
0
    ctxt->level = level;
871
0
    xmlOutputBufferWrite(buf, 2, "]>");
872
0
}
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
0
xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
882
0
    xmlOutputBufferPtr buf;
883
884
0
    if (cur == NULL) return;
885
0
    buf = ctxt->buf;
886
0
    if (buf == NULL) return;
887
0
    if (ctxt->format == 2)
888
0
        xmlOutputBufferWriteWSNonSig(ctxt, 2);
889
0
    else
890
0
        xmlOutputBufferWrite(buf, 1, " ");
891
0
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
892
0
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
893
0
  xmlOutputBufferWrite(buf, 1, ":");
894
0
    }
895
0
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
896
0
    xmlOutputBufferWrite(buf, 2, "=\"");
897
0
#ifdef LIBXML_HTML_ENABLED
898
0
    if ((ctxt->options & XML_SAVE_XHTML) &&
899
0
        (cur->ns == NULL) &&
900
0
        ((cur->children == NULL) ||
901
0
         (cur->children->content == NULL) ||
902
0
         (cur->children->content[0] == 0)) &&
903
0
        (htmlIsBooleanAttr(cur->name))) {
904
0
        xmlOutputBufferWriteString(buf, (const char *) cur->name);
905
0
    } else
906
0
#endif
907
0
    {
908
0
        xmlSaveWriteAttrContent(ctxt, cur);
909
0
    }
910
0
    xmlOutputBufferWrite(buf, 1, "\"");
911
0
}
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
0
htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
922
0
    int switched_encoding = 0;
923
0
    int format = 0;
924
925
0
    xmlInitParser();
926
927
0
    if (ctxt->encoding == NULL) {
928
0
        const char *encoding = NULL;
929
930
0
        if (cur->doc != NULL)
931
0
            encoding = (char *) cur->doc->encoding;
932
933
0
        if (encoding == NULL)
934
0
            encoding = "HTML";
935
936
0
  if (xmlSaveSwitchEncoding(ctxt, encoding) < 0)
937
0
      return(-1);
938
0
  switched_encoding = 1;
939
0
    }
940
941
0
    if (ctxt->options & XML_SAVE_FORMAT)
942
0
        format = 1;
943
944
0
    htmlNodeDumpInternal(ctxt->buf, cur, (char *) ctxt->encoding, format);
945
946
0
    if (switched_encoding) {
947
0
  xmlSaveClearEncoding(ctxt);
948
0
    }
949
950
0
    return(0);
951
0
}
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
2.43k
xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
962
2.43k
    int format = ctxt->format;
963
2.43k
    xmlNodePtr tmp, root, unformattedNode = NULL, parent;
964
2.43k
    xmlAttrPtr attr;
965
2.43k
    xmlChar *start, *end;
966
2.43k
    xmlOutputBufferPtr buf;
967
968
2.43k
    if (cur == NULL) return;
969
2.43k
    buf = ctxt->buf;
970
971
2.43k
    root = cur;
972
2.43k
    parent = cur->parent;
973
3.73k
    while (1) {
974
3.73k
        switch (cur->type) {
975
637
        case XML_DOCUMENT_NODE:
976
637
        case XML_HTML_DOCUMENT_NODE:
977
637
      xmlSaveDocInternal(ctxt, (xmlDocPtr) cur, ctxt->encoding);
978
637
      break;
979
980
0
        case XML_DTD_NODE:
981
0
            xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
982
0
            break;
983
984
0
        case XML_DOCUMENT_FRAG_NODE:
985
            /* Always validate cur->parent when descending. */
986
0
            if ((cur->parent == parent) && (cur->children != NULL)) {
987
0
                parent = cur;
988
0
                cur = cur->children;
989
0
                continue;
990
0
            }
991
0
      break;
992
993
0
        case XML_ELEMENT_DECL:
994
0
            xmlBufDumpElementDecl(buf, (xmlElementPtr) cur);
995
0
            break;
996
997
0
        case XML_ATTRIBUTE_DECL:
998
0
            xmlSaveWriteAttributeDecl(ctxt, (xmlAttributePtr) cur);
999
0
            break;
1000
1001
0
        case XML_ENTITY_DECL:
1002
0
            xmlBufDumpEntityDecl(buf, (xmlEntityPtr) cur);
1003
0
            break;
1004
1005
1.29k
        case XML_ELEMENT_NODE:
1006
1.29k
      if ((cur != root) && (ctxt->format == 1))
1007
0
                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.29k
            if ((cur->parent != parent) && (cur->children != NULL)) {
1015
0
                xmlNodeDumpOutputInternal(ctxt, cur);
1016
0
                break;
1017
0
            }
1018
1019
1.29k
            xmlOutputBufferWrite(buf, 1, "<");
1020
1.29k
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1021
0
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1022
0
                xmlOutputBufferWrite(buf, 1, ":");
1023
0
            }
1024
1.29k
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1025
1.29k
            if (cur->nsDef)
1026
0
                xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
1027
1.29k
            for (attr = cur->properties; attr != NULL; attr = attr->next)
1028
0
                xmlAttrDumpOutput(ctxt, attr);
1029
1030
1.29k
            if (cur->children == NULL) {
1031
0
                if ((ctxt->options & XML_SAVE_NO_EMPTY) == 0) {
1032
0
                    if (ctxt->format == 2)
1033
0
                        xmlOutputBufferWriteWSNonSig(ctxt, 0);
1034
0
                    xmlOutputBufferWrite(buf, 2, "/>");
1035
0
                } 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
1.29k
            } else {
1050
1.29k
                if (ctxt->format == 1) {
1051
11
                    tmp = cur->children;
1052
11
                    while (tmp != NULL) {
1053
11
                        if ((tmp->type == XML_TEXT_NODE) ||
1054
11
                            (tmp->type == XML_CDATA_SECTION_NODE) ||
1055
11
                            (tmp->type == XML_ENTITY_REF_NODE)) {
1056
11
                            ctxt->format = 0;
1057
11
                            unformattedNode = cur;
1058
11
                            break;
1059
11
                        }
1060
0
                        tmp = tmp->next;
1061
0
                    }
1062
11
                }
1063
1.29k
                if (ctxt->format == 2)
1064
6
                    xmlOutputBufferWriteWSNonSig(ctxt, 1);
1065
1.29k
                xmlOutputBufferWrite(buf, 1, ">");
1066
1.29k
                if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
1067
1.29k
                if (ctxt->level >= 0) ctxt->level++;
1068
1.29k
                parent = cur;
1069
1.29k
                cur = cur->children;
1070
1.29k
                continue;
1071
1.29k
            }
1072
1073
0
            break;
1074
1075
1.77k
        case XML_TEXT_NODE:
1076
1.77k
      if (cur->content == NULL)
1077
0
                break;
1078
1.77k
      if (cur->name != xmlStringTextNoenc) {
1079
1.77k
                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
1.77k
                else
1087
1.77k
                    xmlSaveWriteText(ctxt, cur->content, /* flags */ 0);
1088
1.77k
      } else {
1089
    /*
1090
     * Disable escaping, needed for XSLT
1091
     */
1092
0
    xmlOutputBufferWriteString(buf, (const char *) cur->content);
1093
0
      }
1094
1.77k
      break;
1095
1096
0
        case XML_PI_NODE:
1097
0
      if ((cur != root) && (ctxt->format == 1))
1098
0
                xmlSaveWriteIndent(ctxt, 0);
1099
1100
0
            if (cur->content != NULL) {
1101
0
                xmlOutputBufferWrite(buf, 2, "<?");
1102
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1103
0
                if (cur->content != NULL) {
1104
0
                    if (ctxt->format == 2)
1105
0
                        xmlOutputBufferWriteWSNonSig(ctxt, 0);
1106
0
                    else
1107
0
                        xmlOutputBufferWrite(buf, 1, " ");
1108
0
                    xmlOutputBufferWriteString(buf,
1109
0
                            (const char *)cur->content);
1110
0
                }
1111
0
                xmlOutputBufferWrite(buf, 2, "?>");
1112
0
            } else {
1113
0
                xmlOutputBufferWrite(buf, 2, "<?");
1114
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1115
0
                if (ctxt->format == 2)
1116
0
                    xmlOutputBufferWriteWSNonSig(ctxt, 0);
1117
0
                xmlOutputBufferWrite(buf, 2, "?>");
1118
0
            }
1119
0
            break;
1120
1121
0
        case XML_COMMENT_NODE:
1122
0
      if ((cur != root) && (ctxt->format == 1))
1123
0
                xmlSaveWriteIndent(ctxt, 0);
1124
1125
0
            if (cur->content != NULL) {
1126
0
                xmlOutputBufferWrite(buf, 4, "<!--");
1127
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
1128
0
                xmlOutputBufferWrite(buf, 3, "-->");
1129
0
            }
1130
0
            break;
1131
1132
0
        case XML_ENTITY_REF_NODE:
1133
0
            xmlOutputBufferWrite(buf, 1, "&");
1134
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1135
0
            xmlOutputBufferWrite(buf, 1, ";");
1136
0
            break;
1137
1138
0
        case XML_CDATA_SECTION_NODE:
1139
0
            if (cur->content == NULL || *cur->content == '\0') {
1140
0
                xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
1141
0
            } else {
1142
0
                start = end = cur->content;
1143
0
                while (*end != '\0') {
1144
0
                    if ((*end == ']') && (*(end + 1) == ']') &&
1145
0
                        (*(end + 2) == '>')) {
1146
0
                        end = end + 2;
1147
0
                        xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1148
0
                        xmlOutputBufferWrite(buf, end - start,
1149
0
                                (const char *)start);
1150
0
                        xmlOutputBufferWrite(buf, 3, "]]>");
1151
0
                        start = end;
1152
0
                    }
1153
0
                    end++;
1154
0
                }
1155
0
                if (start != end) {
1156
0
                    xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1157
0
                    xmlOutputBufferWriteString(buf, (const char *)start);
1158
0
                    xmlOutputBufferWrite(buf, 3, "]]>");
1159
0
                }
1160
0
            }
1161
0
            break;
1162
1163
0
        case XML_ATTRIBUTE_NODE:
1164
0
            xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
1165
0
            break;
1166
1167
28
        case XML_NAMESPACE_DECL:
1168
28
            xmlNsDumpOutput(buf, (xmlNsPtr) cur, ctxt);
1169
28
            break;
1170
1171
0
        default:
1172
0
            break;
1173
3.73k
        }
1174
1175
3.73k
        while (1) {
1176
3.73k
            if (cur == root)
1177
2.43k
                return;
1178
1.29k
            if ((ctxt->format == 1) &&
1179
1.29k
                (cur->type != XML_XINCLUDE_START) &&
1180
1.29k
                (cur->type != XML_XINCLUDE_END))
1181
0
                xmlOutputBufferWrite(buf, 1, "\n");
1182
1.29k
            if (cur->next != NULL) {
1183
0
                cur = cur->next;
1184
0
                break;
1185
0
            }
1186
1187
1.29k
            cur = parent;
1188
            /* cur->parent was validated when descending. */
1189
1.29k
            parent = cur->parent;
1190
1191
1.29k
            if (cur->type == XML_ELEMENT_NODE) {
1192
1.29k
                if (ctxt->level > 0) ctxt->level--;
1193
1.29k
                if (ctxt->format == 1)
1194
0
                    xmlSaveWriteIndent(ctxt, 0);
1195
1196
1.29k
                xmlOutputBufferWrite(buf, 2, "</");
1197
1.29k
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1198
0
                    xmlOutputBufferWriteString(buf,
1199
0
                            (const char *)cur->ns->prefix);
1200
0
                    xmlOutputBufferWrite(buf, 1, ":");
1201
0
                }
1202
1203
1.29k
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1204
1.29k
                if (ctxt->format == 2)
1205
6
                    xmlOutputBufferWriteWSNonSig(ctxt, 0);
1206
1.29k
                xmlOutputBufferWrite(buf, 1, ">");
1207
1208
1.29k
                if (cur == unformattedNode) {
1209
11
                    ctxt->format = format;
1210
11
                    unformattedNode = NULL;
1211
11
                }
1212
1.29k
            }
1213
1.29k
        }
1214
2.43k
    }
1215
2.43k
}
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
680
                   const xmlChar *encoding) {
1227
680
#ifdef LIBXML_HTML_ENABLED
1228
680
    xmlDtdPtr dtd;
1229
680
    int is_xhtml = 0;
1230
680
#endif
1231
680
    xmlOutputBufferPtr buf = ctxt->buf;
1232
680
    int switched_encoding = 0;
1233
1234
680
    xmlInitParser();
1235
1236
680
    if ((cur->type != XML_HTML_DOCUMENT_NODE) &&
1237
680
        (cur->type != XML_DOCUMENT_NODE))
1238
0
   return(-1);
1239
1240
680
    if (encoding == NULL)
1241
42
  encoding = cur->encoding;
1242
1243
680
    if (((cur->type == XML_HTML_DOCUMENT_NODE) &&
1244
680
         ((ctxt->options & XML_SAVE_AS_XML) == 0) &&
1245
680
         ((ctxt->options & XML_SAVE_XHTML) == 0)) ||
1246
680
        (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
680
    } else if ((cur->type == XML_DOCUMENT_NODE) ||
1268
680
               (ctxt->options & XML_SAVE_AS_XML) ||
1269
680
               (ctxt->options & XML_SAVE_XHTML)) {
1270
680
  if ((encoding != NULL) && (ctxt->encoding == NULL)) {
1271
0
            if (xmlSaveSwitchEncoding(ctxt, (const char *) encoding) < 0)
1272
0
                return(-1);
1273
0
            switched_encoding = 1;
1274
0
  }
1275
1276
  /*
1277
   * Save the XML declaration
1278
   */
1279
680
  if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
1280
680
      xmlOutputBufferWrite(buf, 15, "<?xml version=\"");
1281
680
      if (cur->version != NULL)
1282
680
    xmlOutputBufferWriteString(buf, (char *) cur->version);
1283
0
      else
1284
0
    xmlOutputBufferWrite(buf, 3, "1.0");
1285
680
      xmlOutputBufferWrite(buf, 1, "\"");
1286
680
      if (encoding != NULL) {
1287
638
    xmlOutputBufferWrite(buf, 11, " encoding=\"");
1288
638
    xmlOutputBufferWriteString(buf, (char *) encoding);
1289
638
          xmlOutputBufferWrite(buf, 1, "\"");
1290
638
      }
1291
680
      switch (cur->standalone) {
1292
0
    case 0:
1293
0
        xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
1294
0
        break;
1295
0
    case 1:
1296
0
        xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
1297
0
        break;
1298
680
      }
1299
680
      xmlOutputBufferWrite(buf, 3, "?>\n");
1300
680
  }
1301
1302
680
#ifdef LIBXML_HTML_ENABLED
1303
680
        if (ctxt->options & XML_SAVE_XHTML)
1304
0
            is_xhtml = 1;
1305
680
  if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
1306
680
      dtd = xmlGetIntSubset(cur);
1307
680
      if (dtd != NULL) {
1308
0
    is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
1309
0
    if (is_xhtml < 0) is_xhtml = 0;
1310
0
      }
1311
680
  }
1312
680
#endif
1313
680
  if (cur->children != NULL) {
1314
680
      xmlNodePtr child = cur->children;
1315
1316
1.36k
      while (child != NULL) {
1317
680
    ctxt->level = 0;
1318
680
#ifdef LIBXML_HTML_ENABLED
1319
680
    if (is_xhtml)
1320
0
        xhtmlNodeDumpOutput(ctxt, child);
1321
680
    else
1322
680
#endif
1323
680
        xmlNodeDumpOutputInternal(ctxt, child);
1324
680
                if ((child->type != XML_XINCLUDE_START) &&
1325
680
                    (child->type != XML_XINCLUDE_END))
1326
680
                    xmlOutputBufferWrite(buf, 1, "\n");
1327
680
    child = child->next;
1328
680
      }
1329
680
  }
1330
680
    }
1331
1332
    /*
1333
     * Restore the state of the saving context at the end of the document
1334
     */
1335
680
    if (switched_encoding) {
1336
0
  xmlSaveClearEncoding(ctxt);
1337
0
    }
1338
1339
680
    return(0);
1340
680
}
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
0
xhtmlIsEmpty(xmlNodePtr node) {
1357
0
    if (node == NULL)
1358
0
  return(-1);
1359
0
    if (node->type != XML_ELEMENT_NODE)
1360
0
  return(0);
1361
0
    if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
1362
0
  return(0);
1363
0
    if (node->children != NULL)
1364
0
  return(0);
1365
0
    switch (node->name ? node->name[0] : 0) {
1366
0
  case 'a':
1367
0
      if (xmlStrEqual(node->name, BAD_CAST "area"))
1368
0
    return(1);
1369
0
      return(0);
1370
0
  case 'b':
1371
0
      if (xmlStrEqual(node->name, BAD_CAST "br"))
1372
0
    return(1);
1373
0
      if (xmlStrEqual(node->name, BAD_CAST "base"))
1374
0
    return(1);
1375
0
      if (xmlStrEqual(node->name, BAD_CAST "basefont"))
1376
0
    return(1);
1377
0
      return(0);
1378
0
  case 'c':
1379
0
      if (xmlStrEqual(node->name, BAD_CAST "col"))
1380
0
    return(1);
1381
0
      return(0);
1382
0
  case 'f':
1383
0
      if (xmlStrEqual(node->name, BAD_CAST "frame"))
1384
0
    return(1);
1385
0
      return(0);
1386
0
  case 'h':
1387
0
      if (xmlStrEqual(node->name, BAD_CAST "hr"))
1388
0
    return(1);
1389
0
      return(0);
1390
0
  case 'i':
1391
0
      if (xmlStrEqual(node->name, BAD_CAST "img"))
1392
0
    return(1);
1393
0
      if (xmlStrEqual(node->name, BAD_CAST "input"))
1394
0
    return(1);
1395
0
      if (xmlStrEqual(node->name, BAD_CAST "isindex"))
1396
0
    return(1);
1397
0
      return(0);
1398
0
  case 'l':
1399
0
      if (xmlStrEqual(node->name, BAD_CAST "link"))
1400
0
    return(1);
1401
0
      return(0);
1402
0
  case 'm':
1403
0
      if (xmlStrEqual(node->name, BAD_CAST "meta"))
1404
0
    return(1);
1405
0
      return(0);
1406
0
  case 'p':
1407
0
      if (xmlStrEqual(node->name, BAD_CAST "param"))
1408
0
    return(1);
1409
0
      return(0);
1410
0
    }
1411
0
    return(0);
1412
0
}
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
0
xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
1422
0
    xmlAttrPtr xml_lang = NULL;
1423
0
    xmlAttrPtr lang = NULL;
1424
0
    xmlAttrPtr name = NULL;
1425
0
    xmlAttrPtr id = NULL;
1426
0
    xmlNodePtr parent;
1427
0
    xmlOutputBufferPtr buf;
1428
1429
0
    if (cur == NULL) return;
1430
0
    buf = ctxt->buf;
1431
0
    parent = cur->parent;
1432
0
    while (cur != NULL) {
1433
0
  if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
1434
0
      id = cur;
1435
0
  else
1436
0
  if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
1437
0
      name = cur;
1438
0
  else
1439
0
  if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
1440
0
      lang = cur;
1441
0
  else
1442
0
  if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
1443
0
      (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
1444
0
      xml_lang = cur;
1445
0
        xmlAttrDumpOutput(ctxt, cur);
1446
0
  cur = cur->next;
1447
0
    }
1448
    /*
1449
     * C.8
1450
     */
1451
0
    if ((name != NULL) && (id == NULL)) {
1452
0
  if ((parent != NULL) && (parent->name != NULL) &&
1453
0
      ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1454
0
       (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1455
0
       (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1456
0
       (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1457
0
       (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1458
0
       (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1459
0
       (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1460
0
       (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1461
0
       (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
1462
0
      xmlOutputBufferWrite(buf, 5, " id=\"");
1463
0
            xmlSaveWriteAttrContent(ctxt, name);
1464
0
      xmlOutputBufferWrite(buf, 1, "\"");
1465
0
  }
1466
0
    }
1467
    /*
1468
     * C.7.
1469
     */
1470
0
    if ((lang != NULL) && (xml_lang == NULL)) {
1471
0
  xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1472
0
        xmlSaveWriteAttrContent(ctxt, lang);
1473
0
  xmlOutputBufferWrite(buf, 1, "\"");
1474
0
    } else
1475
0
    if ((xml_lang != NULL) && (lang == NULL)) {
1476
0
  xmlOutputBufferWrite(buf, 7, " lang=\"");
1477
0
        xmlSaveWriteAttrContent(ctxt, xml_lang);
1478
0
  xmlOutputBufferWrite(buf, 1, "\"");
1479
0
    }
1480
0
}
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
0
xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
1490
0
    int format = ctxt->format, addmeta, oldoptions;
1491
0
    xmlNodePtr tmp, root, unformattedNode = NULL, parent;
1492
0
    xmlChar *start, *end;
1493
0
    xmlOutputBufferPtr buf = ctxt->buf;
1494
1495
0
    if (cur == NULL) return;
1496
1497
0
    oldoptions = ctxt->options;
1498
0
    ctxt->options |= XML_SAVE_XHTML;
1499
1500
0
    root = cur;
1501
0
    parent = cur->parent;
1502
0
    while (1) {
1503
0
        switch (cur->type) {
1504
0
        case XML_DOCUMENT_NODE:
1505
0
        case XML_HTML_DOCUMENT_NODE:
1506
0
            xmlSaveDocInternal(ctxt, (xmlDocPtr) cur, ctxt->encoding);
1507
0
      break;
1508
1509
0
        case XML_NAMESPACE_DECL:
1510
0
      xmlNsDumpOutput(buf, (xmlNsPtr) cur, ctxt);
1511
0
      break;
1512
1513
0
        case XML_DTD_NODE:
1514
0
            xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
1515
0
      break;
1516
1517
0
        case XML_DOCUMENT_FRAG_NODE:
1518
            /* Always validate cur->parent when descending. */
1519
0
            if ((cur->parent == parent) && (cur->children != NULL)) {
1520
0
                parent = cur;
1521
0
                cur = cur->children;
1522
0
                continue;
1523
0
            }
1524
0
            break;
1525
1526
0
        case XML_ELEMENT_DECL:
1527
0
            xmlBufDumpElementDecl(buf, (xmlElementPtr) cur);
1528
0
      break;
1529
1530
0
        case XML_ATTRIBUTE_DECL:
1531
0
            xmlSaveWriteAttributeDecl(ctxt, (xmlAttributePtr) cur);
1532
0
      break;
1533
1534
0
        case XML_ENTITY_DECL:
1535
0
            xmlBufDumpEntityDecl(buf, (xmlEntityPtr) cur);
1536
0
      break;
1537
1538
0
        case XML_ELEMENT_NODE:
1539
0
            addmeta = 0;
1540
1541
0
      if ((cur != root) && (ctxt->format == 1))
1542
0
                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
0
            if ((cur->parent != parent) && (cur->children != NULL)) {
1550
0
                xhtmlNodeDumpOutput(ctxt, cur);
1551
0
                break;
1552
0
            }
1553
1554
0
            xmlOutputBufferWrite(buf, 1, "<");
1555
0
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1556
0
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1557
0
                xmlOutputBufferWrite(buf, 1, ":");
1558
0
            }
1559
1560
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1561
0
            if (cur->nsDef)
1562
0
                xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
1563
0
            if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1564
0
                (cur->ns == NULL) && (cur->nsDef == NULL))) {
1565
                /*
1566
                 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1567
                 */
1568
0
                xmlOutputBufferWriteString(buf,
1569
0
                        " xmlns=\"http://www.w3.org/1999/xhtml\"");
1570
0
            }
1571
0
            if (cur->properties != NULL)
1572
0
                xhtmlAttrListDumpOutput(ctxt, cur->properties);
1573
1574
0
            if ((parent != NULL) &&
1575
0
                (parent->parent == (xmlNodePtr) cur->doc) &&
1576
0
                xmlStrEqual(cur->name, BAD_CAST"head") &&
1577
0
                xmlStrEqual(parent->name, BAD_CAST"html")) {
1578
1579
0
                tmp = cur->children;
1580
0
                while (tmp != NULL) {
1581
0
                    if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1582
0
                        int res;
1583
0
                        xmlChar *httpequiv;
1584
1585
0
                        res = xmlNodeGetAttrValue(tmp, BAD_CAST "http-equiv",
1586
0
                                                  NULL, &httpequiv);
1587
0
                        if (res < 0) {
1588
0
                            xmlSaveErrMemory(buf);
1589
0
                        } else if (res == 0) {
1590
0
                            if (xmlStrcasecmp(httpequiv,
1591
0
                                        BAD_CAST"Content-Type") == 0) {
1592
0
                                xmlFree(httpequiv);
1593
0
                                break;
1594
0
                            }
1595
0
                            xmlFree(httpequiv);
1596
0
                        }
1597
0
                    }
1598
0
                    tmp = tmp->next;
1599
0
                }
1600
0
                if (tmp == NULL)
1601
0
                    addmeta = 1;
1602
0
            }
1603
1604
0
            if (cur->children == NULL) {
1605
0
                if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
1606
0
                    ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
1607
                    /*
1608
                     * C.2. Empty Elements
1609
                     */
1610
0
                    xmlOutputBufferWrite(buf, 3, " />");
1611
0
                } else {
1612
0
                    if (addmeta == 1) {
1613
0
                        xmlOutputBufferWrite(buf, 1, ">");
1614
0
                        if (ctxt->format == 1) {
1615
0
                            xmlOutputBufferWrite(buf, 1, "\n");
1616
0
                            xmlSaveWriteIndent(ctxt, 1);
1617
0
                        }
1618
0
                        xmlOutputBufferWriteString(buf,
1619
0
                                "<meta http-equiv=\"Content-Type\" "
1620
0
                                "content=\"text/html; charset=");
1621
0
                        if (ctxt->encoding) {
1622
0
                            xmlOutputBufferWriteString(buf,
1623
0
                                    (const char *)ctxt->encoding);
1624
0
                        } else {
1625
0
                            xmlOutputBufferWrite(buf, 5, "UTF-8");
1626
0
                        }
1627
0
                        xmlOutputBufferWrite(buf, 4, "\" />");
1628
0
                        if (ctxt->format == 1)
1629
0
                            xmlOutputBufferWrite(buf, 1, "\n");
1630
0
                    } else {
1631
0
                        xmlOutputBufferWrite(buf, 1, ">");
1632
0
                    }
1633
                    /*
1634
                     * C.3. Element Minimization and Empty Element Content
1635
                     */
1636
0
                    xmlOutputBufferWrite(buf, 2, "</");
1637
0
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1638
0
                        xmlOutputBufferWriteString(buf,
1639
0
                                (const char *)cur->ns->prefix);
1640
0
                        xmlOutputBufferWrite(buf, 1, ":");
1641
0
                    }
1642
0
                    xmlOutputBufferWriteString(buf, (const char *)cur->name);
1643
0
                    xmlOutputBufferWrite(buf, 1, ">");
1644
0
                }
1645
0
            } else {
1646
0
                xmlOutputBufferWrite(buf, 1, ">");
1647
0
                if (addmeta == 1) {
1648
0
                    if (ctxt->format == 1) {
1649
0
                        xmlOutputBufferWrite(buf, 1, "\n");
1650
0
                        xmlSaveWriteIndent(ctxt, 1);
1651
0
                    }
1652
0
                    xmlOutputBufferWriteString(buf,
1653
0
                            "<meta http-equiv=\"Content-Type\" "
1654
0
                            "content=\"text/html; charset=");
1655
0
                    if (ctxt->encoding) {
1656
0
                        xmlOutputBufferWriteString(buf,
1657
0
                                (const char *)ctxt->encoding);
1658
0
                    } else {
1659
0
                        xmlOutputBufferWrite(buf, 5, "UTF-8");
1660
0
                    }
1661
0
                    xmlOutputBufferWrite(buf, 4, "\" />");
1662
0
                }
1663
1664
0
                if (ctxt->format == 1) {
1665
0
                    tmp = cur->children;
1666
0
                    while (tmp != NULL) {
1667
0
                        if ((tmp->type == XML_TEXT_NODE) ||
1668
0
                            (tmp->type == XML_ENTITY_REF_NODE)) {
1669
0
                            unformattedNode = cur;
1670
0
                            ctxt->format = 0;
1671
0
                            break;
1672
0
                        }
1673
0
                        tmp = tmp->next;
1674
0
                    }
1675
0
                }
1676
1677
0
                if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
1678
0
                if (ctxt->level >= 0) ctxt->level++;
1679
0
                parent = cur;
1680
0
                cur = cur->children;
1681
0
                continue;
1682
0
            }
1683
1684
0
            break;
1685
1686
0
        case XML_TEXT_NODE:
1687
0
      if (cur->content == NULL)
1688
0
                break;
1689
0
      if ((cur->name == xmlStringText) ||
1690
0
    (cur->name != xmlStringTextNoenc)) {
1691
0
                if (ctxt->escape)
1692
0
                    xmlOutputBufferWriteEscape(buf, cur->content,
1693
0
                                               ctxt->escape);
1694
0
                else
1695
0
                    xmlSaveWriteText(ctxt, cur->content, /* flags */ 0);
1696
0
      } else {
1697
    /*
1698
     * Disable escaping, needed for XSLT
1699
     */
1700
0
    xmlOutputBufferWriteString(buf, (const char *) cur->content);
1701
0
      }
1702
0
      break;
1703
1704
0
        case XML_PI_NODE:
1705
0
            if (cur->content != NULL) {
1706
0
                xmlOutputBufferWrite(buf, 2, "<?");
1707
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1708
0
                if (cur->content != NULL) {
1709
0
                    xmlOutputBufferWrite(buf, 1, " ");
1710
0
                    xmlOutputBufferWriteString(buf,
1711
0
                            (const char *)cur->content);
1712
0
                }
1713
0
                xmlOutputBufferWrite(buf, 2, "?>");
1714
0
            } else {
1715
0
                xmlOutputBufferWrite(buf, 2, "<?");
1716
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1717
0
                xmlOutputBufferWrite(buf, 2, "?>");
1718
0
            }
1719
0
            break;
1720
1721
0
        case XML_COMMENT_NODE:
1722
0
            if (cur->content != NULL) {
1723
0
                xmlOutputBufferWrite(buf, 4, "<!--");
1724
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
1725
0
                xmlOutputBufferWrite(buf, 3, "-->");
1726
0
            }
1727
0
            break;
1728
1729
0
        case XML_ENTITY_REF_NODE:
1730
0
            xmlOutputBufferWrite(buf, 1, "&");
1731
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
1732
0
            xmlOutputBufferWrite(buf, 1, ";");
1733
0
            break;
1734
1735
0
        case XML_CDATA_SECTION_NODE:
1736
0
            if (cur->content == NULL || *cur->content == '\0') {
1737
0
                xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
1738
0
            } else {
1739
0
                start = end = cur->content;
1740
0
                while (*end != '\0') {
1741
0
                    if (*end == ']' && *(end + 1) == ']' &&
1742
0
                        *(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
0
                    end++;
1751
0
                }
1752
0
                if (start != end) {
1753
0
                    xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1754
0
                    xmlOutputBufferWriteString(buf, (const char *)start);
1755
0
                    xmlOutputBufferWrite(buf, 3, "]]>");
1756
0
                }
1757
0
            }
1758
0
            break;
1759
1760
0
        case XML_ATTRIBUTE_NODE:
1761
0
            xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
1762
0
      break;
1763
1764
0
        default:
1765
0
            break;
1766
0
        }
1767
1768
0
        while (1) {
1769
0
            if (cur == root)
1770
0
                return;
1771
0
            if (ctxt->format == 1)
1772
0
                xmlOutputBufferWrite(buf, 1, "\n");
1773
0
            if (cur->next != NULL) {
1774
0
                cur = cur->next;
1775
0
                break;
1776
0
            }
1777
1778
0
            cur = parent;
1779
            /* cur->parent was validated when descending. */
1780
0
            parent = cur->parent;
1781
1782
0
            if (cur->type == XML_ELEMENT_NODE) {
1783
0
                if (ctxt->level > 0) ctxt->level--;
1784
0
                if (ctxt->format == 1)
1785
0
                    xmlSaveWriteIndent(ctxt, 0);
1786
1787
0
                xmlOutputBufferWrite(buf, 2, "</");
1788
0
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1789
0
                    xmlOutputBufferWriteString(buf,
1790
0
                            (const char *)cur->ns->prefix);
1791
0
                    xmlOutputBufferWrite(buf, 1, ":");
1792
0
                }
1793
1794
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
1795
0
                xmlOutputBufferWrite(buf, 1, ">");
1796
1797
0
                if (cur == unformattedNode) {
1798
0
                    ctxt->format = format;
1799
0
                    unformattedNode = NULL;
1800
0
                }
1801
0
            }
1802
0
        }
1803
0
    }
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
159
{
1833
159
    xmlSaveCtxtPtr ret;
1834
1835
159
    ret = xmlNewSaveCtxt(encoding, options);
1836
159
    if (ret == NULL) return(NULL);
1837
34
    ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1838
34
    if (ret->buf == NULL) {
1839
0
        xmlCharEncCloseFunc(ret->handler);
1840
0
  xmlFreeSaveCtxt(ret);
1841
0
  return(NULL);
1842
0
    }
1843
34
    return(ret);
1844
34
}
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
0
{
1898
0
    xmlSaveCtxtPtr ret;
1899
1900
0
    ret = xmlNewSaveCtxt(encoding, options);
1901
0
    if (ret == NULL) return(NULL);
1902
0
    ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler);
1903
0
    if (ret->buf == NULL) {
1904
0
        xmlCharEncCloseFunc(ret->handler);
1905
0
  xmlFreeSaveCtxt(ret);
1906
0
  return(NULL);
1907
0
    }
1908
0
    return(ret);
1909
0
}
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
34
{
1960
34
    long ret = 0;
1961
1962
34
    if ((ctxt == NULL) || (doc == NULL)) return(-1);
1963
34
    if (xmlSaveDocInternal(ctxt, doc, ctxt->encoding) < 0)
1964
0
        return(-1);
1965
34
    return(ret);
1966
34
}
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
0
{
1980
0
    long ret = 0;
1981
1982
0
    if ((ctxt == NULL) || (cur == NULL)) return(-1);
1983
0
#ifdef LIBXML_HTML_ENABLED
1984
0
    if (ctxt->options & XML_SAVE_XHTML) {
1985
0
        xhtmlNodeDumpOutput(ctxt, cur);
1986
0
        return(ret);
1987
0
    }
1988
0
    if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
1989
0
         (cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
1990
0
         ((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
1991
0
        (ctxt->options & XML_SAVE_AS_HTML)) {
1992
0
  htmlNodeDumpOutputInternal(ctxt, cur);
1993
0
  return(ret);
1994
0
    }
1995
0
#endif
1996
0
    xmlNodeDumpOutputInternal(ctxt, cur);
1997
0
    return(ret);
1998
0
}
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
0
xmlSaveNotationTable(xmlSaveCtxt *ctxt, xmlNotationTable *cur) {
2024
0
    if (ctxt == NULL)
2025
0
        return(-1);
2026
0
    xmlBufDumpNotationTable(ctxt->buf, cur);
2027
0
    return(0);
2028
0
}
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
34
{
2040
34
    if (ctxt == NULL) return(-1);
2041
34
    if (ctxt->buf == NULL) return(-1);
2042
34
    return(xmlOutputBufferFlush(ctxt->buf));
2043
34
}
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
34
{
2055
34
    int ret;
2056
2057
34
    if (ctxt == NULL) return(-1);
2058
34
    ret = xmlSaveFlush(ctxt);
2059
34
    xmlFreeSaveCtxt(ctxt);
2060
34
    return(ret);
2061
34
}
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
0
{
2075
0
    int ret;
2076
2077
0
    if (ctxt == NULL)
2078
0
        return(XML_ERR_INTERNAL_ERROR);
2079
2080
0
    ret = xmlOutputBufferClose(ctxt->buf);
2081
0
    ctxt->buf = NULL;
2082
0
    if (ret < 0)
2083
0
        ret = -ret;
2084
0
    else
2085
0
        ret = XML_ERR_OK;
2086
2087
0
    xmlFreeSaveCtxt(ctxt);
2088
0
    return(ret);
2089
0
}
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
0
{
2143
0
    int flags = XML_ESCAPE_ATTR;
2144
2145
0
    if ((doc == NULL) || (doc->encoding == NULL))
2146
0
        flags |= XML_ESCAPE_NON_ASCII;
2147
0
    xmlSerializeText(buf, string, SIZE_MAX, flags);
2148
0
}
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
0
{
2163
0
    xmlOutputBufferPtr out;
2164
2165
0
    if ((buf == NULL) || (string == NULL))
2166
0
        return;
2167
0
    out = xmlOutputBufferCreateBuffer(buf, NULL);
2168
0
    xmlBufAttrSerializeTxtContent(out, doc, string);
2169
0
    xmlOutputBufferFlush(out);
2170
0
    if ((out == NULL) || (out->error))
2171
0
        xmlFree(xmlBufferDetach(buf));
2172
0
    xmlOutputBufferClose(out);
2173
0
}
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
0
{
2198
0
    xmlBufPtr buffer;
2199
0
    size_t ret1;
2200
0
    int ret2;
2201
2202
0
    if ((buf == NULL) || (cur == NULL))
2203
0
        return(-1);
2204
0
    if (level < 0)
2205
0
        level = 0;
2206
0
    else if (level > 100)
2207
0
        level = 100;
2208
0
    buffer = xmlBufFromBuffer(buf);
2209
0
    if (buffer == NULL)
2210
0
        return(-1);
2211
0
    ret1 = xmlBufNodeDump(buffer, doc, cur, level, format);
2212
0
    ret2 = xmlBufBackToBuffer(buffer, buf);
2213
0
    if ((ret1 == (size_t) -1) || (ret2 < 0))
2214
0
        return(-1);
2215
0
    return(ret1 > INT_MAX ? INT_MAX : ret1);
2216
0
}
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
0
{
2240
0
    size_t use;
2241
0
    size_t ret;
2242
0
    xmlOutputBufferPtr outbuf;
2243
2244
0
    xmlInitParser();
2245
2246
0
    if (cur == NULL) {
2247
0
        return ((size_t) -1);
2248
0
    }
2249
0
    if (buf == NULL) {
2250
0
        return ((size_t) -1);
2251
0
    }
2252
0
    outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
2253
0
    if (outbuf == NULL) {
2254
0
        xmlSaveErrMemory(NULL);
2255
0
        return ((size_t) -1);
2256
0
    }
2257
0
    memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
2258
0
    outbuf->buffer = buf;
2259
0
    outbuf->encoder = NULL;
2260
0
    outbuf->writecallback = NULL;
2261
0
    outbuf->closecallback = NULL;
2262
0
    outbuf->context = NULL;
2263
0
    outbuf->written = 0;
2264
2265
0
    use = xmlBufUse(buf);
2266
0
    xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
2267
0
    if (outbuf->error)
2268
0
        ret = (size_t) -1;
2269
0
    else
2270
0
        ret = xmlBufUse(buf) - use;
2271
0
    xmlFree(outbuf);
2272
0
    return (ret);
2273
0
}
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
1.75k
{
2335
1.75k
    xmlSaveCtxt ctxt;
2336
1.75k
    int options;
2337
1.75k
#ifdef LIBXML_HTML_ENABLED
2338
1.75k
    xmlDtdPtr dtd;
2339
1.75k
    int is_xhtml = 0;
2340
1.75k
#endif
2341
2342
1.75k
    (void) doc;
2343
2344
1.75k
    xmlInitParser();
2345
2346
1.75k
    if ((buf == NULL) || (cur == NULL)) return;
2347
2348
1.75k
    if (level < 0)
2349
0
        level = 0;
2350
1.75k
    else if (level > 100)
2351
0
        level = 100;
2352
2353
1.75k
    if (encoding == NULL)
2354
1.75k
        encoding = "UTF-8";
2355
2356
1.75k
    memset(&ctxt, 0, sizeof(ctxt));
2357
1.75k
    ctxt.buf = buf;
2358
1.75k
    ctxt.level = level;
2359
1.75k
    ctxt.encoding = (const xmlChar *) encoding;
2360
2361
1.75k
    options = XML_SAVE_AS_XML;
2362
1.75k
    if (format)
2363
0
        options |= XML_SAVE_FORMAT;
2364
1.75k
    xmlSaveCtxtInit(&ctxt, options);
2365
2366
1.75k
#ifdef LIBXML_HTML_ENABLED
2367
1.75k
    dtd = xmlGetIntSubset(doc);
2368
1.75k
    if (dtd != NULL) {
2369
0
  is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
2370
0
  if (is_xhtml < 0)
2371
0
      is_xhtml = 0;
2372
0
    }
2373
2374
1.75k
    if (is_xhtml)
2375
0
        xhtmlNodeDumpOutput(&ctxt, cur);
2376
1.75k
    else
2377
1.75k
#endif
2378
1.75k
        xmlNodeDumpOutputInternal(&ctxt, cur);
2379
1.75k
}
2380
2381
static void
2382
xmlDocDumpInternal(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding,
2383
9
                   int format) {
2384
9
    xmlSaveCtxt ctxt;
2385
9
    int options;
2386
2387
9
    memset(&ctxt, 0, sizeof(ctxt));
2388
9
    ctxt.buf = buf;
2389
2390
9
    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
9
    options = XML_SAVE_AS_XML;
2399
9
    if (format)
2400
0
        options |= XML_SAVE_FORMAT;
2401
9
    xmlSaveCtxtInit(&ctxt, options);
2402
2403
9
    xmlSaveDocInternal(&ctxt, doc, (const xmlChar *) encoding);
2404
9
}
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
0
    int format) {
2430
0
    xmlOutputBufferPtr buf = NULL;
2431
2432
0
    if (doc_txt_len != NULL)
2433
0
        *doc_txt_len = 0;
2434
2435
0
    if (doc_txt_ptr == NULL)
2436
0
        return;
2437
0
    *doc_txt_ptr = NULL;
2438
2439
0
    if (out_doc == NULL)
2440
0
        return;
2441
2442
0
    buf = xmlAllocOutputBuffer(NULL);
2443
0
    if (buf == NULL) {
2444
0
        xmlSaveErrMemory(NULL);
2445
0
        return;
2446
0
    }
2447
2448
0
    xmlDocDumpInternal(buf, out_doc, txt_encoding, format);
2449
2450
0
    xmlOutputBufferFlush(buf);
2451
2452
0
    if (!buf->error) {
2453
0
        if (doc_txt_len != NULL)
2454
0
            *doc_txt_len = xmlBufUse(buf->buffer);
2455
0
        *doc_txt_ptr = xmlBufDetach(buf->buffer);
2456
0
    }
2457
2458
0
    xmlOutputBufferClose(buf);
2459
0
}
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
0
xmlDocDumpMemory(xmlDoc *cur, xmlChar**mem, int *size) {
2471
0
    xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2472
0
}
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
0
xmlDocDumpFormatMemory(xmlDoc *cur, xmlChar**mem, int *size, int format) {
2485
0
    xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2486
0
}
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
0
              int * doc_txt_len, const char * txt_encoding) {
2500
0
    xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2501
0
                        txt_encoding, 0);
2502
0
}
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
0
xmlSaveFileTo(xmlOutputBuffer *buf, xmlDoc *cur, const char *encoding) {
2561
0
    return(xmlSaveFormatFileTo(buf, cur, encoding, 0));
2562
0
}
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
0
{
2587
0
    if (buf == NULL) return(-1);
2588
0
    if ((cur == NULL) ||
2589
0
        ((cur->type != XML_DOCUMENT_NODE) &&
2590
0
   (cur->type != XML_HTML_DOCUMENT_NODE))) {
2591
0
        xmlOutputBufferClose(buf);
2592
0
  return(-1);
2593
0
    }
2594
2595
0
    xmlDocDumpInternal(buf, cur, encoding, format);
2596
2597
0
    return(xmlOutputBufferClose(buf));
2598
0
}
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
9
      const char * encoding, int format ) {
2621
9
    xmlOutputBufferPtr buf;
2622
2623
9
    if (cur == NULL)
2624
0
  return(-1);
2625
2626
9
#ifdef LIBXML_ZLIB_ENABLED
2627
9
    if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2628
9
#endif
2629
    /*
2630
     * save the content to a temp buffer.
2631
     */
2632
9
    buf = xmlOutputBufferCreateFilename(filename, NULL, cur->compression);
2633
9
    if (buf == NULL) return(-1);
2634
2635
9
    xmlDocDumpInternal(buf, cur, encoding, format);
2636
2637
9
    return(xmlOutputBufferClose(buf));
2638
9
}
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
9
xmlSaveFile(const char *filename, xmlDoc *cur) {
2677
9
    return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2678
9
}
2679
2680
#endif /* LIBXML_OUTPUT_ENABLED */
2681