| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * xmlwriter.c: XML text writer implementation | 3 * xmlwriter.c: XML text writer implementation |
| 4 * | 4 * |
| 5 * For license and disclaimer see the license and disclaimer of | 5 * For license and disclaimer see the license and disclaimer of |
| 6 * libxml2. | 6 * libxml2. |
| 7 * | 7 * |
| 8 * alfred@mickautsch.de | 8 * alfred@mickautsch.de |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #define IN_LIBXML | 11 #define IN_LIBXML |
| 12 #include "libxml.h" | 12 #include "libxml.h" |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include <libxml/xmlmemory.h> | 15 #include <libxml/xmlmemory.h> |
| 16 #include <libxml/parser.h> | 16 #include <libxml/parser.h> |
| 17 #include <libxml/uri.h> | 17 #include <libxml/uri.h> |
| 18 #include <libxml/HTMLtree.h> | 18 #include <libxml/HTMLtree.h> |
| 19 | 19 |
| 20 #ifdef LIBXML_WRITER_ENABLED | 20 #ifdef LIBXML_WRITER_ENABLED |
| 21 | 21 |
| 22 #include <libxml/xmlwriter.h> | 22 #include <libxml/xmlwriter.h> |
| 23 | 23 |
| 24 #include "buf.h" |
| 25 #include "enc.h" |
| 26 #include "save.h" |
| 27 |
| 24 #define B64LINELEN 72 | 28 #define B64LINELEN 72 |
| 25 #define B64CRLF "\r\n" | 29 #define B64CRLF "\r\n" |
| 26 | 30 |
| 27 /* | 31 /* |
| 28 * The following VA_COPY was coded following an example in | 32 * The following VA_COPY was coded following an example in |
| 29 * the Samba project. It may not be sufficient for some | 33 * the Samba project. It may not be sufficient for some |
| 30 * esoteric implementations of va_list (i.e. it may need | 34 * esoteric implementations of va_list but (hopefully) will |
| 31 * something involving a memcpy) but (hopefully) will be | 35 * be sufficient for libxml2. |
| 32 * sufficient for libxml2. | |
| 33 */ | 36 */ |
| 34 #ifndef VA_COPY | 37 #ifndef VA_COPY |
| 35 #ifdef HAVE_VA_COPY | 38 #ifdef HAVE_VA_COPY |
| 36 #define VA_COPY(dest, src) va_copy(dest, src) | 39 #define VA_COPY(dest, src) va_copy(dest, src) |
| 37 #else | 40 #else |
| 38 #ifdef HAVE___VA_COPY | 41 #ifdef HAVE___VA_COPY |
| 39 #define VA_COPY(dest,src) __va_copy(dest, src) | 42 #define VA_COPY(dest,src) __va_copy(dest, src) |
| 40 #else | 43 #else |
| 41 #define VA_COPY(dest,src) (dest) = (src) | 44 #ifndef VA_LIST_IS_ARRAY |
| 45 #define VA_COPY(dest,src) (dest) = (src) |
| 46 #else |
| 47 #include <string.h> |
| 48 #define VA_COPY(dest,src) memcpy((char *)(dest),(char *)(src),sizeof(va_
list)) |
| 49 #endif |
| 42 #endif | 50 #endif |
| 43 #endif | 51 #endif |
| 44 #endif | 52 #endif |
| 45 | 53 |
| 46 /* | 54 /* |
| 47 * Types are kept private | 55 * Types are kept private |
| 48 */ | 56 */ |
| 49 typedef enum { | 57 typedef enum { |
| 50 XML_TEXTWRITER_NONE = 0, | 58 XML_TEXTWRITER_NONE = 0, |
| 51 XML_TEXTWRITER_NAME, | 59 XML_TEXTWRITER_NAME, |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 if (encoder == NULL) { | 549 if (encoder == NULL) { |
| 542 xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY, | 550 xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY, |
| 543 "xmlTextWriterStartDocument : out of memory!\n"); | 551 "xmlTextWriterStartDocument : out of memory!\n"); |
| 544 return -1; | 552 return -1; |
| 545 } | 553 } |
| 546 } | 554 } |
| 547 | 555 |
| 548 writer->out->encoder = encoder; | 556 writer->out->encoder = encoder; |
| 549 if (encoder != NULL) { | 557 if (encoder != NULL) { |
| 550 if (writer->out->conv == NULL) { | 558 if (writer->out->conv == NULL) { |
| 551 » writer->out->conv = xmlBufferCreateSize(4000); | 559 » writer->out->conv = xmlBufCreateSize(4000); |
| 552 } | 560 } |
| 553 xmlCharEncOutFunc(encoder, writer->out->conv, NULL); | 561 xmlCharEncOutput(writer->out, 1); |
| 554 if ((writer->doc != NULL) && (writer->doc->encoding == NULL)) | 562 if ((writer->doc != NULL) && (writer->doc->encoding == NULL)) |
| 555 writer->doc->encoding = xmlStrdup((xmlChar *)writer->out->encoder->n
ame); | 563 writer->doc->encoding = xmlStrdup((xmlChar *)writer->out->encoder->n
ame); |
| 556 } else | 564 } else |
| 557 writer->out->conv = NULL; | 565 writer->out->conv = NULL; |
| 558 | 566 |
| 559 sum = 0; | 567 sum = 0; |
| 560 count = xmlOutputBufferWriteString(writer->out, "<?xml version="); | 568 count = xmlOutputBufferWriteString(writer->out, "<?xml version="); |
| 561 if (count < 0) | 569 if (count < 0) |
| 562 return -1; | 570 return -1; |
| 563 sum += count; | 571 sum += count; |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1067 buf = xmlStrcat(buf, name); | 1075 buf = xmlStrcat(buf, name); |
| 1068 | 1076 |
| 1069 sum = 0; | 1077 sum = 0; |
| 1070 count = xmlTextWriterStartElement(writer, buf); | 1078 count = xmlTextWriterStartElement(writer, buf); |
| 1071 xmlFree(buf); | 1079 xmlFree(buf); |
| 1072 if (count < 0) | 1080 if (count < 0) |
| 1073 return -1; | 1081 return -1; |
| 1074 sum += count; | 1082 sum += count; |
| 1075 | 1083 |
| 1076 if (namespaceURI != 0) { | 1084 if (namespaceURI != 0) { |
| 1077 xmlTextWriterNsStackEntry *p = (xmlTextWriterNsStackEntry *) | 1085 xmlTextWriterNsStackEntry *p = (xmlTextWriterNsStackEntry *) |
| 1078 xmlMalloc(sizeof(xmlTextWriterNsStackEntry)); | 1086 xmlMalloc(sizeof(xmlTextWriterNsStackEntry)); |
| 1079 if (p == 0) { | 1087 if (p == 0) { |
| 1080 xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY, | 1088 xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY, |
| 1081 "xmlTextWriterStartElementNS : out of memory!\n"); | 1089 "xmlTextWriterStartElementNS : out of memory!\n"); |
| 1082 return -1; | 1090 return -1; |
| 1083 } | 1091 } |
| 1084 | 1092 |
| 1085 buf = xmlStrdup(BAD_CAST "xmlns"); | 1093 buf = xmlStrdup(BAD_CAST "xmlns"); |
| 1086 if (prefix != 0) { | 1094 if (prefix != 0) { |
| 1087 buf = xmlStrcat(buf, BAD_CAST ":"); | 1095 buf = xmlStrcat(buf, BAD_CAST ":"); |
| 1088 buf = xmlStrcat(buf, prefix); | 1096 buf = xmlStrcat(buf, prefix); |
| 1089 } | 1097 } |
| 1090 | 1098 |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1494 case XML_TEXTWRITER_NAME: | 1502 case XML_TEXTWRITER_NAME: |
| 1495 case XML_TEXTWRITER_TEXT: | 1503 case XML_TEXTWRITER_TEXT: |
| 1496 #if 0 | 1504 #if 0 |
| 1497 buf = NULL; | 1505 buf = NULL; |
| 1498 xmlOutputBufferWriteEscape(writer->out, content, NULL); | 1506 xmlOutputBufferWriteEscape(writer->out, content, NULL); |
| 1499 #endif | 1507 #endif |
| 1500 buf = xmlEncodeSpecialChars(NULL, content); | 1508 buf = xmlEncodeSpecialChars(NULL, content); |
| 1501 break; | 1509 break; |
| 1502 case XML_TEXTWRITER_ATTRIBUTE: | 1510 case XML_TEXTWRITER_ATTRIBUTE: |
| 1503 buf = NULL; | 1511 buf = NULL; |
| 1504 xmlAttrSerializeTxtContent(writer->out->buffer, writer->doc, | 1512 xmlBufAttrSerializeTxtContent(writer->out->buffer, |
| 1505 NULL, content); | 1513 writer->doc, NULL, content); |
| 1506 break; | 1514 break; |
| 1507 default: | 1515 default: |
| 1508 break; | 1516 break; |
| 1509 } | 1517 } |
| 1510 } | 1518 } |
| 1511 } | 1519 } |
| 1512 | 1520 |
| 1513 if (buf != NULL) { | 1521 if (buf != NULL) { |
| 1514 count = xmlTextWriterWriteRaw(writer, buf); | 1522 count = xmlTextWriterWriteRaw(writer, buf); |
| 1515 | 1523 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1656 | 1664 |
| 1657 /** | 1665 /** |
| 1658 * xmlOutputBufferWriteBinHex: | 1666 * xmlOutputBufferWriteBinHex: |
| 1659 * @out: the xmlOutputBufferPtr | 1667 * @out: the xmlOutputBufferPtr |
| 1660 * @data: binary data | 1668 * @data: binary data |
| 1661 * @len: the number of bytes to encode | 1669 * @len: the number of bytes to encode |
| 1662 * | 1670 * |
| 1663 * Write hqx encoded data to an xmlOutputBuffer. | 1671 * Write hqx encoded data to an xmlOutputBuffer. |
| 1664 * ::todo | 1672 * ::todo |
| 1665 * | 1673 * |
| 1666 * Returns the bytes written (may be 0 because of buffering) | 1674 * Returns the bytes written (may be 0 because of buffering) |
| 1667 * or -1 in case of error | 1675 * or -1 in case of error |
| 1668 */ | 1676 */ |
| 1669 static int | 1677 static int |
| 1670 xmlOutputBufferWriteBinHex(xmlOutputBufferPtr out, | 1678 xmlOutputBufferWriteBinHex(xmlOutputBufferPtr out, |
| 1671 int len, const unsigned char *data) | 1679 int len, const unsigned char *data) |
| 1672 { | 1680 { |
| 1673 int count; | 1681 int count; |
| 1674 int sum; | 1682 int sum; |
| 1675 static char hex[16] = | 1683 static char hex[16] = |
| 1676 » {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | 1684 » {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; |
| 1677 int i; | 1685 int i; |
| 1678 | 1686 |
| 1679 if ((out == NULL) || (data == NULL) || (len < 0)) { | 1687 if ((out == NULL) || (data == NULL) || (len < 0)) { |
| 1680 return -1; | 1688 return -1; |
| 1681 } | 1689 } |
| 1682 | 1690 |
| 1683 sum = 0; | 1691 sum = 0; |
| 1684 for (i = 0; i < len; i++) { | 1692 for (i = 0; i < len; i++) { |
| 1685 count = | 1693 count = |
| 1686 xmlOutputBufferWrite(out, 1, | 1694 xmlOutputBufferWrite(out, 1, |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1842 buf = xmlStrdup(BAD_CAST "xmlns"); | 1850 buf = xmlStrdup(BAD_CAST "xmlns"); |
| 1843 if (prefix != 0) { | 1851 if (prefix != 0) { |
| 1844 buf = xmlStrcat(buf, BAD_CAST ":"); | 1852 buf = xmlStrcat(buf, BAD_CAST ":"); |
| 1845 buf = xmlStrcat(buf, prefix); | 1853 buf = xmlStrcat(buf, prefix); |
| 1846 } | 1854 } |
| 1847 | 1855 |
| 1848 nsentry.prefix = buf; | 1856 nsentry.prefix = buf; |
| 1849 nsentry.uri = (xmlChar *)namespaceURI; | 1857 nsentry.uri = (xmlChar *)namespaceURI; |
| 1850 nsentry.elem = xmlListFront(writer->nodes); | 1858 nsentry.elem = xmlListFront(writer->nodes); |
| 1851 | 1859 |
| 1852 curns = (xmlTextWriterNsStackEntry *)xmlListSearch(writer->nsstack, | 1860 curns = (xmlTextWriterNsStackEntry *)xmlListSearch(writer->nsstack, |
| 1853 (void *)&nsentry); | 1861 (void *)&nsentry); |
| 1854 if ((curns != NULL)) { | 1862 if ((curns != NULL)) { |
| 1855 xmlFree(buf); | 1863 xmlFree(buf); |
| 1856 if (xmlStrcmp(curns->uri, namespaceURI) == 0) { | 1864 if (xmlStrcmp(curns->uri, namespaceURI) == 0) { |
| 1857 /* Namespace already defined on element skip */ | 1865 /* Namespace already defined on element skip */ |
| 1858 buf = NULL; | 1866 buf = NULL; |
| 1859 } else { | 1867 } else { |
| 1860 /* Prefix mismatch so error out */ | 1868 /* Prefix mismatch so error out */ |
| 1861 return -1; | 1869 return -1; |
| 1862 } | 1870 } |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2227 const xmlChar * content) | 2235 const xmlChar * content) |
| 2228 { | 2236 { |
| 2229 int count; | 2237 int count; |
| 2230 int sum; | 2238 int sum; |
| 2231 | 2239 |
| 2232 sum = 0; | 2240 sum = 0; |
| 2233 count = xmlTextWriterStartElement(writer, name); | 2241 count = xmlTextWriterStartElement(writer, name); |
| 2234 if (count == -1) | 2242 if (count == -1) |
| 2235 return -1; | 2243 return -1; |
| 2236 sum += count; | 2244 sum += count; |
| 2237 count = xmlTextWriterWriteString(writer, content); | 2245 if (content != NULL) { |
| 2238 if (count == -1) | 2246 » count = xmlTextWriterWriteString(writer, content); |
| 2239 return -1; | 2247 » if (count == -1) |
| 2240 sum += count; | 2248 » return -1; |
| 2249 » sum += count; |
| 2250 } |
| 2241 count = xmlTextWriterEndElement(writer); | 2251 count = xmlTextWriterEndElement(writer); |
| 2242 if (count == -1) | 2252 if (count == -1) |
| 2243 return -1; | 2253 return -1; |
| 2244 sum += count; | 2254 sum += count; |
| 2245 | 2255 |
| 2246 return sum; | 2256 return sum; |
| 2247 } | 2257 } |
| 2248 | 2258 |
| 2249 /** | 2259 /** |
| 2250 * xmlTextWriterWriteFormatElementNS: | 2260 * xmlTextWriterWriteFormatElementNS: |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2489 if (count < 0) | 2499 if (count < 0) |
| 2490 return -1; | 2500 return -1; |
| 2491 sum += count; | 2501 sum += count; |
| 2492 break; | 2502 break; |
| 2493 default: | 2503 default: |
| 2494 return -1; | 2504 return -1; |
| 2495 } | 2505 } |
| 2496 | 2506 |
| 2497 if (writer->indent) { | 2507 if (writer->indent) { |
| 2498 count = xmlOutputBufferWriteString(writer->out, "\n"); | 2508 count = xmlOutputBufferWriteString(writer->out, "\n"); |
| 2499 » if (count < 0) | 2509 » if (count < 0) |
| 2500 »return -1; | 2510 » return -1; |
| 2501 sum += count; | 2511 sum += count; |
| 2502 } | 2512 } |
| 2503 | 2513 |
| 2504 xmlListPopFront(writer->nodes); | 2514 xmlListPopFront(writer->nodes); |
| 2505 return sum; | 2515 return sum; |
| 2506 } | 2516 } |
| 2507 | 2517 |
| 2508 /** | 2518 /** |
| 2509 * xmlTextWriterWriteFormatPI: | 2519 * xmlTextWriterWriteFormatPI: |
| 2510 * @writer: the xmlTextWriterPtr | 2520 * @writer: the xmlTextWriterPtr |
| (...skipping 2088 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4599 xmlFree(writer->ichar); | 4609 xmlFree(writer->ichar); |
| 4600 writer->ichar = xmlStrdup(str); | 4610 writer->ichar = xmlStrdup(str); |
| 4601 | 4611 |
| 4602 if (!writer->ichar) | 4612 if (!writer->ichar) |
| 4603 return -1; | 4613 return -1; |
| 4604 else | 4614 else |
| 4605 return 0; | 4615 return 0; |
| 4606 } | 4616 } |
| 4607 | 4617 |
| 4608 /** | 4618 /** |
| 4619 * xmlTextWriterSetQuoteChar: |
| 4620 * @writer: the xmlTextWriterPtr |
| 4621 * @quotechar: the quote character |
| 4622 * |
| 4623 * Set the character used for quoting attributes. |
| 4624 * |
| 4625 * Returns -1 on error or 0 otherwise. |
| 4626 */ |
| 4627 int |
| 4628 xmlTextWriterSetQuoteChar(xmlTextWriterPtr writer, xmlChar quotechar) |
| 4629 { |
| 4630 if ((writer == NULL) || ((quotechar != '\'') && (quotechar != '"'))) |
| 4631 return -1; |
| 4632 |
| 4633 writer->qchar = quotechar; |
| 4634 |
| 4635 return 0; |
| 4636 } |
| 4637 |
| 4638 /** |
| 4609 * xmlTextWriterWriteIndent: | 4639 * xmlTextWriterWriteIndent: |
| 4610 * @writer: the xmlTextWriterPtr | 4640 * @writer: the xmlTextWriterPtr |
| 4611 * | 4641 * |
| 4612 * Write indent string. | 4642 * Write indent string. |
| 4613 * | 4643 * |
| 4614 * Returns -1 on error or the number of strings written. | 4644 * Returns -1 on error or the number of strings written. |
| 4615 */ | 4645 */ |
| 4616 static int | 4646 static int |
| 4617 xmlTextWriterWriteIndent(xmlTextWriterPtr writer) | 4647 xmlTextWriterWriteIndent(xmlTextWriterPtr writer) |
| 4618 { | 4648 { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4704 return -1; | 4734 return -1; |
| 4705 sum += count; | 4735 sum += count; |
| 4706 } | 4736 } |
| 4707 | 4737 |
| 4708 return sum; | 4738 return sum; |
| 4709 } | 4739 } |
| 4710 | 4740 |
| 4711 #define bottom_xmlwriter | 4741 #define bottom_xmlwriter |
| 4712 #include "elfgcchack.h" | 4742 #include "elfgcchack.h" |
| 4713 #endif | 4743 #endif |
| OLD | NEW |