2 * "$Id: mxml-file.c 455 2014-01-05 03:28:03Z msweet $"
4 * File loading code for Mini-XML, a small XML-like file parsing library.
6 * Copyright 2003-2014 by Michael R Sweet.
8 * These coded instructions, statements, and computer programs are the
9 * property of Michael R Sweet and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "COPYING"
11 * which should have been included with this file. If this file is
12 * missing or damaged, see the license at:
14 * http://www.msweet.org/projects.php/Mini-XML
17 /*** This file modified by ARM on 25 Aug 2014 to avoid pointer overflow when checking if the write position is beyond the end of the buffer in mxmlSaveString and mxml_string_putc ***/
20 * Include necessary headers...
26 #include "mxml-private.h"
30 * Character encoding...
33 #define ENCODE_UTF8 0 /* UTF-8 */
34 #define ENCODE_UTF16BE 1 /* UTF-16 Big-Endian */
35 #define ENCODE_UTF16LE 2 /* UTF-16 Little-Endian */
39 * Macro to test for a bad XML character...
42 #define mxml_bad_char(ch) ((ch) < ' ' && (ch) != '\n' && (ch) != '\r' && (ch) != '\t')
46 * Types and structures...
49 typedef int (*_mxml_getc_cb_t)(void *, int *);
50 typedef int (*_mxml_putc_cb_t)(int, void *);
52 typedef struct _mxml_fdbuf_s /**** File descriptor buffer ****/
54 int fd; /* File descriptor */
55 unsigned char *current, /* Current position in buffer */
56 *end, /* End of buffer */
57 buffer[8192]; /* Character buffer */
65 static int mxml_add_char(int ch, char **ptr, char **buffer,
67 static int mxml_fd_getc(void *p, int *encoding);
68 static int mxml_fd_putc(int ch, void *p);
69 static int mxml_fd_read(_mxml_fdbuf_t *buf);
70 static int mxml_fd_write(_mxml_fdbuf_t *buf);
71 static int mxml_file_getc(void *p, int *encoding);
72 static int mxml_file_putc(int ch, void *p);
73 static int mxml_get_entity(mxml_node_t *parent, void *p,
75 _mxml_getc_cb_t getc_cb);
76 static inline int mxml_isspace(int ch)
78 return (ch == ' ' || ch == '\t' || ch == '\r' ||
81 static mxml_node_t *mxml_load_data(mxml_node_t *top, void *p,
83 _mxml_getc_cb_t getc_cb,
84 mxml_sax_cb_t sax_cb, void *sax_data);
85 static int mxml_parse_element(mxml_node_t *node, void *p,
87 _mxml_getc_cb_t getc_cb);
88 static int mxml_string_getc(void *p, int *encoding);
89 static int mxml_string_putc(int ch, void *p);
90 static int mxml_write_name(const char *s, void *p,
91 _mxml_putc_cb_t putc_cb);
92 static int mxml_write_node(mxml_node_t *node, void *p,
93 mxml_save_cb_t cb, int col,
94 _mxml_putc_cb_t putc_cb,
95 _mxml_global_t *global);
96 static int mxml_write_string(const char *s, void *p,
97 _mxml_putc_cb_t putc_cb);
98 static int mxml_write_ws(mxml_node_t *node, void *p,
99 mxml_save_cb_t cb, int ws,
100 int col, _mxml_putc_cb_t putc_cb);
104 * 'mxmlLoadFd()' - Load a file descriptor into an XML node tree.
106 * The nodes in the specified file are added to the specified top node.
107 * If no top node is provided, the XML file MUST be well-formed with a
108 * single parent node like <?xml> for the entire file. The callback
109 * function returns the value type that should be used for child nodes.
110 * If MXML_NO_CALLBACK is specified then all child nodes will be either
111 * MXML_ELEMENT or MXML_TEXT nodes.
113 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
114 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
115 * child nodes of the specified type.
118 mxml_node_t * /* O - First node or NULL if the file could not be read. */
119 mxmlLoadFd(mxml_node_t *top, /* I - Top node */
120 int fd, /* I - File descriptor to read from */
121 mxml_load_cb_t cb) /* I - Callback function or MXML_NO_CALLBACK */
123 _mxml_fdbuf_t buf; /* File descriptor buffer */
127 * Initialize the file descriptor buffer...
131 buf.current = buf.buffer;
132 buf.end = buf.buffer;
135 * Read the XML data...
138 return (mxml_load_data(top, &buf, cb, mxml_fd_getc, MXML_NO_CALLBACK, NULL));
143 * 'mxmlLoadFile()' - Load a file into an XML node tree.
145 * The nodes in the specified file are added to the specified top node.
146 * If no top node is provided, the XML file MUST be well-formed with a
147 * single parent node like <?xml> for the entire file. The callback
148 * function returns the value type that should be used for child nodes.
149 * If MXML_NO_CALLBACK is specified then all child nodes will be either
150 * MXML_ELEMENT or MXML_TEXT nodes.
152 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
153 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
154 * child nodes of the specified type.
157 mxml_node_t * /* O - First node or NULL if the file could not be read. */
158 mxmlLoadFile(mxml_node_t *top, /* I - Top node */
159 FILE *fp, /* I - File to read from */
160 mxml_load_cb_t cb) /* I - Callback function or MXML_NO_CALLBACK */
163 * Read the XML data...
166 return (mxml_load_data(top, fp, cb, mxml_file_getc, MXML_NO_CALLBACK, NULL));
171 * 'mxmlLoadString()' - Load a string into an XML node tree.
173 * The nodes in the specified string are added to the specified top node.
174 * If no top node is provided, the XML string MUST be well-formed with a
175 * single parent node like <?xml> for the entire string. The callback
176 * function returns the value type that should be used for child nodes.
177 * If MXML_NO_CALLBACK is specified then all child nodes will be either
178 * MXML_ELEMENT or MXML_TEXT nodes.
180 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
181 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
182 * child nodes of the specified type.
185 mxml_node_t * /* O - First node or NULL if the string has errors. */
186 mxmlLoadString(mxml_node_t *top, /* I - Top node */
187 const char *s, /* I - String to load */
188 mxml_load_cb_t cb) /* I - Callback function or MXML_NO_CALLBACK */
191 * Read the XML data...
194 return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, MXML_NO_CALLBACK,
200 * 'mxmlSaveAllocString()' - Save an XML tree to an allocated string.
202 * This function returns a pointer to a string containing the textual
203 * representation of the XML node tree. The string should be freed
204 * using the free() function when you are done with it. NULL is returned
205 * if the node would produce an empty string or if the string cannot be
208 * The callback argument specifies a function that returns a whitespace
209 * string or NULL before and after each element. If MXML_NO_CALLBACK
210 * is specified, whitespace will only be added before MXML_TEXT nodes
211 * with leading whitespace and before attribute names inside opening
215 char * /* O - Allocated string or NULL */
217 mxml_node_t *node, /* I - Node to write */
218 mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
220 int bytes; /* Required bytes */
221 char buffer[8192]; /* Temporary buffer */
222 char *s; /* Allocated string */
226 * Write the node to the temporary buffer...
229 bytes = mxmlSaveString(node, buffer, sizeof(buffer), cb);
234 if (bytes < (int)(sizeof(buffer) - 1))
237 * Node fit inside the buffer, so just duplicate that string and
241 return (strdup(buffer));
245 * Allocate a buffer of the required size and save the node to the
249 if ((s = malloc(bytes + 1)) == NULL)
252 mxmlSaveString(node, s, bytes + 1, cb);
255 * Return the allocated string...
263 * 'mxmlSaveFd()' - Save an XML tree to a file descriptor.
265 * The callback argument specifies a function that returns a whitespace
266 * string or NULL before and after each element. If MXML_NO_CALLBACK
267 * is specified, whitespace will only be added before MXML_TEXT nodes
268 * with leading whitespace and before attribute names inside opening
272 int /* O - 0 on success, -1 on error. */
273 mxmlSaveFd(mxml_node_t *node, /* I - Node to write */
274 int fd, /* I - File descriptor to write to */
275 mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
277 int col; /* Final column */
278 _mxml_fdbuf_t buf; /* File descriptor buffer */
279 _mxml_global_t *global = _mxml_global();
284 * Initialize the file descriptor buffer...
288 buf.current = buf.buffer;
289 buf.end = buf.buffer + sizeof(buf.buffer);
295 if ((col = mxml_write_node(node, &buf, cb, 0, mxml_fd_putc, global)) < 0)
299 if (mxml_fd_putc('\n', &buf) < 0)
303 * Flush and return...
306 return (mxml_fd_write(&buf));
311 * 'mxmlSaveFile()' - Save an XML tree to a file.
313 * The callback argument specifies a function that returns a whitespace
314 * string or NULL before and after each element. If MXML_NO_CALLBACK
315 * is specified, whitespace will only be added before MXML_TEXT nodes
316 * with leading whitespace and before attribute names inside opening
320 int /* O - 0 on success, -1 on error. */
321 mxmlSaveFile(mxml_node_t *node, /* I - Node to write */
322 FILE *fp, /* I - File to write to */
323 mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
325 int col; /* Final column */
326 _mxml_global_t *global = _mxml_global();
334 if ((col = mxml_write_node(node, fp, cb, 0, mxml_file_putc, global)) < 0)
338 if (putc('\n', fp) < 0)
342 * Return 0 (success)...
350 * 'mxmlSaveString()' - Save an XML node tree to a string.
352 * This function returns the total number of bytes that would be
353 * required for the string but only copies (bufsize - 1) characters
354 * into the specified buffer.
356 * The callback argument specifies a function that returns a whitespace
357 * string or NULL before and after each element. If MXML_NO_CALLBACK
358 * is specified, whitespace will only be added before MXML_TEXT nodes
359 * with leading whitespace and before attribute names inside opening
363 int /* O - Size of string */
364 mxmlSaveString(mxml_node_t *node, /* I - Node to write */
365 char *buffer, /* I - String buffer */
366 int bufsize, /* I - Size of string buffer */
367 mxml_save_cb_t cb) /* I - Whitespace callback or MXML_NO_CALLBACK */
369 int col; /* Final column */
370 char *ptr[3]; /* Pointers for putc_cb */
371 _mxml_global_t *global = _mxml_global();
380 ptr[1] = buffer + bufsize;
383 if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc, global)) < 0)
387 mxml_string_putc('\n', ptr);
390 * Nul-terminate the buffer...
394 buffer[bufsize - 1] = '\0';
399 * Return the number of characters...
402 return (ptr[0] - buffer);
407 * 'mxmlSAXLoadFd()' - Load a file descriptor into an XML node tree
408 * using a SAX callback.
410 * The nodes in the specified file are added to the specified top node.
411 * If no top node is provided, the XML file MUST be well-formed with a
412 * single parent node like <?xml> for the entire file. The callback
413 * function returns the value type that should be used for child nodes.
414 * If MXML_NO_CALLBACK is specified then all child nodes will be either
415 * MXML_ELEMENT or MXML_TEXT nodes.
417 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
418 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
419 * child nodes of the specified type.
421 * The SAX callback must call mxmlRetain() for any nodes that need to
422 * be kept for later use. Otherwise, nodes are deleted when the parent
423 * node is closed or after each data, comment, CDATA, or directive node.
425 * @since Mini-XML 2.3@
428 mxml_node_t * /* O - First node or NULL if the file could not be read. */
429 mxmlSAXLoadFd(mxml_node_t *top, /* I - Top node */
430 int fd, /* I - File descriptor to read from */
431 mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
432 mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
433 void *sax_data) /* I - SAX user data */
435 _mxml_fdbuf_t buf; /* File descriptor buffer */
439 * Initialize the file descriptor buffer...
443 buf.current = buf.buffer;
444 buf.end = buf.buffer;
447 * Read the XML data...
450 return (mxml_load_data(top, &buf, cb, mxml_fd_getc, sax_cb, sax_data));
455 * 'mxmlSAXLoadFile()' - Load a file into an XML node tree
456 * using a SAX callback.
458 * The nodes in the specified file are added to the specified top node.
459 * If no top node is provided, the XML file MUST be well-formed with a
460 * single parent node like <?xml> for the entire file. The callback
461 * function returns the value type that should be used for child nodes.
462 * If MXML_NO_CALLBACK is specified then all child nodes will be either
463 * MXML_ELEMENT or MXML_TEXT nodes.
465 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
466 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
467 * child nodes of the specified type.
469 * The SAX callback must call mxmlRetain() for any nodes that need to
470 * be kept for later use. Otherwise, nodes are deleted when the parent
471 * node is closed or after each data, comment, CDATA, or directive node.
473 * @since Mini-XML 2.3@
476 mxml_node_t * /* O - First node or NULL if the file could not be read. */
478 mxml_node_t *top, /* I - Top node */
479 FILE *fp, /* I - File to read from */
480 mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
481 mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
482 void *sax_data) /* I - SAX user data */
485 * Read the XML data...
488 return (mxml_load_data(top, fp, cb, mxml_file_getc, sax_cb, sax_data));
493 * 'mxmlSAXLoadString()' - Load a string into an XML node tree
494 * using a SAX callback.
496 * The nodes in the specified string are added to the specified top node.
497 * If no top node is provided, the XML string MUST be well-formed with a
498 * single parent node like <?xml> for the entire string. The callback
499 * function returns the value type that should be used for child nodes.
500 * If MXML_NO_CALLBACK is specified then all child nodes will be either
501 * MXML_ELEMENT or MXML_TEXT nodes.
503 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
504 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
505 * child nodes of the specified type.
507 * The SAX callback must call mxmlRetain() for any nodes that need to
508 * be kept for later use. Otherwise, nodes are deleted when the parent
509 * node is closed or after each data, comment, CDATA, or directive node.
511 * @since Mini-XML 2.3@
514 mxml_node_t * /* O - First node or NULL if the string has errors. */
516 mxml_node_t *top, /* I - Top node */
517 const char *s, /* I - String to load */
518 mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
519 mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
520 void *sax_data) /* I - SAX user data */
523 * Read the XML data...
526 return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, sax_cb, sax_data));
531 * 'mxmlSetCustomHandlers()' - Set the handling functions for custom data.
533 * The load function accepts a node pointer and a data string and must
534 * return 0 on success and non-zero on error.
536 * The save function accepts a node pointer and must return a malloc'd
537 * string on success and NULL on error.
542 mxmlSetCustomHandlers(
543 mxml_custom_load_cb_t load, /* I - Load function */
544 mxml_custom_save_cb_t save) /* I - Save function */
546 _mxml_global_t *global = _mxml_global();
550 global->custom_load_cb = load;
551 global->custom_save_cb = save;
556 * 'mxmlSetErrorCallback()' - Set the error message callback.
560 mxmlSetErrorCallback(mxml_error_cb_t cb)/* I - Error callback function */
562 _mxml_global_t *global = _mxml_global();
566 global->error_cb = cb;
571 * 'mxmlSetWrapMargin()' - Set the wrap margin when saving XML data.
573 * Wrapping is disabled when "column" is 0.
575 * @since Mini-XML 2.3@
579 mxmlSetWrapMargin(int column) /* I - Column for wrapping, 0 to disable wrapping */
581 _mxml_global_t *global = _mxml_global();
585 global->wrap = column;
590 * 'mxml_add_char()' - Add a character to a buffer, expanding as needed.
593 static int /* O - 0 on success, -1 on error */
594 mxml_add_char(int ch, /* I - Character to add */
595 char **bufptr, /* IO - Current position in buffer */
596 char **buffer, /* IO - Current buffer */
597 int *bufsize) /* IO - Current buffer size */
599 char *newbuffer; /* New buffer value */
602 if (*bufptr >= (*buffer + *bufsize - 4))
605 * Increase the size of the buffer...
613 if ((newbuffer = realloc(*buffer, *bufsize)) == NULL)
617 mxml_error("Unable to expand string buffer to %d bytes!", *bufsize);
622 *bufptr = newbuffer + (*bufptr - *buffer);
629 * Single byte ASCII...
640 *(*bufptr)++ = 0xc0 | (ch >> 6);
641 *(*bufptr)++ = 0x80 | (ch & 0x3f);
643 else if (ch < 0x10000)
646 * Three-byte UTF-8...
649 *(*bufptr)++ = 0xe0 | (ch >> 12);
650 *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
651 *(*bufptr)++ = 0x80 | (ch & 0x3f);
659 *(*bufptr)++ = 0xf0 | (ch >> 18);
660 *(*bufptr)++ = 0x80 | ((ch >> 12) & 0x3f);
661 *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
662 *(*bufptr)++ = 0x80 | (ch & 0x3f);
670 * 'mxml_fd_getc()' - Read a character from a file descriptor.
673 static int /* O - Character or EOF */
674 mxml_fd_getc(void *p, /* I - File descriptor buffer */
675 int *encoding) /* IO - Encoding */
677 _mxml_fdbuf_t *buf; /* File descriptor buffer */
678 int ch, /* Current character */
679 temp; /* Temporary character */
683 * Grab the next character in the buffer...
686 buf = (_mxml_fdbuf_t *)p;
688 if (buf->current >= buf->end)
689 if (mxml_fd_read(buf) < 0)
692 ch = *(buf->current)++;
698 * Got a UTF-8 character; convert UTF-8 to Unicode and return...
704 printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
705 #endif /* DEBUG > 1 */
707 if (mxml_bad_char(ch))
709 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
719 * UTF-16 big-endian BOM?
722 if (buf->current >= buf->end)
723 if (mxml_fd_read(buf) < 0)
726 ch = *(buf->current)++;
731 *encoding = ENCODE_UTF16BE;
733 return (mxml_fd_getc(p, encoding));
738 * UTF-16 little-endian BOM?
741 if (buf->current >= buf->end)
742 if (mxml_fd_read(buf) < 0)
745 ch = *(buf->current)++;
750 *encoding = ENCODE_UTF16LE;
752 return (mxml_fd_getc(p, encoding));
754 else if ((ch & 0xe0) == 0xc0)
760 if (buf->current >= buf->end)
761 if (mxml_fd_read(buf) < 0)
764 temp = *(buf->current)++;
766 if ((temp & 0xc0) != 0x80)
769 ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
773 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
777 else if ((ch & 0xf0) == 0xe0)
780 * Three-byte value...
783 if (buf->current >= buf->end)
784 if (mxml_fd_read(buf) < 0)
787 temp = *(buf->current)++;
789 if ((temp & 0xc0) != 0x80)
792 ch = ((ch & 0x0f) << 6) | (temp & 0x3f);
794 if (buf->current >= buf->end)
795 if (mxml_fd_read(buf) < 0)
798 temp = *(buf->current)++;
800 if ((temp & 0xc0) != 0x80)
803 ch = (ch << 6) | (temp & 0x3f);
807 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
812 * Ignore (strip) Byte Order Mark (BOM)...
816 return (mxml_fd_getc(p, encoding));
818 else if ((ch & 0xf8) == 0xf0)
824 if (buf->current >= buf->end)
825 if (mxml_fd_read(buf) < 0)
828 temp = *(buf->current)++;
830 if ((temp & 0xc0) != 0x80)
833 ch = ((ch & 0x07) << 6) | (temp & 0x3f);
835 if (buf->current >= buf->end)
836 if (mxml_fd_read(buf) < 0)
839 temp = *(buf->current)++;
841 if ((temp & 0xc0) != 0x80)
844 ch = (ch << 6) | (temp & 0x3f);
846 if (buf->current >= buf->end)
847 if (mxml_fd_read(buf) < 0)
850 temp = *(buf->current)++;
852 if ((temp & 0xc0) != 0x80)
855 ch = (ch << 6) | (temp & 0x3f);
859 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
867 case ENCODE_UTF16BE :
869 * Read UTF-16 big-endian char...
872 if (buf->current >= buf->end)
873 if (mxml_fd_read(buf) < 0)
876 temp = *(buf->current)++;
878 ch = (ch << 8) | temp;
880 if (mxml_bad_char(ch))
882 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
886 else if (ch >= 0xd800 && ch <= 0xdbff)
889 * Multi-word UTF-16 char...
894 if (buf->current >= buf->end)
895 if (mxml_fd_read(buf) < 0)
898 lch = *(buf->current)++;
900 if (buf->current >= buf->end)
901 if (mxml_fd_read(buf) < 0)
904 temp = *(buf->current)++;
906 lch = (lch << 8) | temp;
908 if (lch < 0xdc00 || lch >= 0xdfff)
911 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
915 case ENCODE_UTF16LE :
917 * Read UTF-16 little-endian char...
920 if (buf->current >= buf->end)
921 if (mxml_fd_read(buf) < 0)
924 temp = *(buf->current)++;
928 if (mxml_bad_char(ch))
930 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
934 else if (ch >= 0xd800 && ch <= 0xdbff)
937 * Multi-word UTF-16 char...
942 if (buf->current >= buf->end)
943 if (mxml_fd_read(buf) < 0)
946 lch = *(buf->current)++;
948 if (buf->current >= buf->end)
949 if (mxml_fd_read(buf) < 0)
952 temp = *(buf->current)++;
956 if (lch < 0xdc00 || lch >= 0xdfff)
959 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
965 printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
966 #endif /* DEBUG > 1 */
973 * 'mxml_fd_putc()' - Write a character to a file descriptor.
976 static int /* O - 0 on success, -1 on error */
977 mxml_fd_putc(int ch, /* I - Character */
978 void *p) /* I - File descriptor buffer */
980 _mxml_fdbuf_t *buf; /* File descriptor buffer */
984 * Flush the write buffer as needed...
987 buf = (_mxml_fdbuf_t *)p;
989 if (buf->current >= buf->end)
990 if (mxml_fd_write(buf) < 0)
993 *(buf->current)++ = ch;
996 * Return successfully...
1004 * 'mxml_fd_read()' - Read a buffer of data from a file descriptor.
1007 static int /* O - 0 on success, -1 on error */
1008 mxml_fd_read(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */
1010 int bytes; /* Bytes read... */
1014 * Range check input...
1021 * Read from the file descriptor...
1024 while ((bytes = read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)
1026 if (errno != EAGAIN && errno != EINTR)
1028 if (errno != EAGAIN)
1036 * Update the pointers and return success...
1039 buf->current = buf->buffer;
1040 buf->end = buf->buffer + bytes;
1047 * 'mxml_fd_write()' - Write a buffer of data to a file descriptor.
1050 static int /* O - 0 on success, -1 on error */
1051 mxml_fd_write(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */
1053 int bytes; /* Bytes written */
1054 unsigned char *ptr; /* Pointer into buffer */
1065 * Return 0 if there is nothing to write...
1068 if (buf->current == buf->buffer)
1072 * Loop until we have written everything...
1075 for (ptr = buf->buffer; ptr < buf->current; ptr += bytes)
1076 if ((bytes = write(buf->fd, ptr, buf->current - ptr)) < 0)
1080 * All done, reset pointers and return success...
1083 buf->current = buf->buffer;
1090 * 'mxml_file_getc()' - Get a character from a file.
1093 static int /* O - Character or EOF */
1094 mxml_file_getc(void *p, /* I - Pointer to file */
1095 int *encoding) /* IO - Encoding */
1097 int ch, /* Character from file */
1098 temp; /* Temporary character */
1099 FILE *fp; /* Pointer to file */
1103 * Read a character from the file and see if it is EOF or ASCII...
1116 * Got a UTF-8 character; convert UTF-8 to Unicode and return...
1121 if (mxml_bad_char(ch))
1123 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
1129 printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
1130 #endif /* DEBUG > 1 */
1134 else if (ch == 0xfe)
1137 * UTF-16 big-endian BOM?
1144 *encoding = ENCODE_UTF16BE;
1146 return (mxml_file_getc(p, encoding));
1148 else if (ch == 0xff)
1151 * UTF-16 little-endian BOM?
1158 *encoding = ENCODE_UTF16LE;
1160 return (mxml_file_getc(p, encoding));
1162 else if ((ch & 0xe0) == 0xc0)
1168 if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
1171 ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
1175 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1179 else if ((ch & 0xf0) == 0xe0)
1182 * Three-byte value...
1185 if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
1188 ch = ((ch & 0x0f) << 6) | (temp & 0x3f);
1190 if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
1193 ch = (ch << 6) | (temp & 0x3f);
1197 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1202 * Ignore (strip) Byte Order Mark (BOM)...
1206 return (mxml_file_getc(p, encoding));
1208 else if ((ch & 0xf8) == 0xf0)
1211 * Four-byte value...
1214 if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
1217 ch = ((ch & 0x07) << 6) | (temp & 0x3f);
1219 if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
1222 ch = (ch << 6) | (temp & 0x3f);
1224 if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
1227 ch = (ch << 6) | (temp & 0x3f);
1231 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1239 case ENCODE_UTF16BE :
1241 * Read UTF-16 big-endian char...
1244 ch = (ch << 8) | getc(fp);
1246 if (mxml_bad_char(ch))
1248 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
1252 else if (ch >= 0xd800 && ch <= 0xdbff)
1255 * Multi-word UTF-16 char...
1259 lch = (lch << 8) | getc(fp);
1261 if (lch < 0xdc00 || lch >= 0xdfff)
1264 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
1268 case ENCODE_UTF16LE :
1270 * Read UTF-16 little-endian char...
1273 ch |= (getc(fp) << 8);
1275 if (mxml_bad_char(ch))
1277 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
1281 else if (ch >= 0xd800 && ch <= 0xdbff)
1284 * Multi-word UTF-16 char...
1288 lch |= (getc(fp) << 8);
1290 if (lch < 0xdc00 || lch >= 0xdfff)
1293 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
1299 printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
1300 #endif /* DEBUG > 1 */
1307 * 'mxml_file_putc()' - Write a character to a file.
1310 static int /* O - 0 on success, -1 on failure */
1311 mxml_file_putc(int ch, /* I - Character to write */
1312 void *p) /* I - Pointer to file */
1314 return (putc(ch, (FILE *)p) == EOF ? -1 : 0);
1319 * 'mxml_get_entity()' - Get the character corresponding to an entity...
1322 static int /* O - Character value or EOF on error */
1323 mxml_get_entity(mxml_node_t *parent, /* I - Parent node */
1324 void *p, /* I - Pointer to source */
1325 int *encoding, /* IO - Character encoding */
1326 int (*getc_cb)(void *, int *))
1327 /* I - Get character function */
1329 int ch; /* Current character */
1330 char entity[64], /* Entity string */
1331 *entptr; /* Pointer into entity */
1336 while ((ch = (*getc_cb)(p, encoding)) != EOF)
1337 if (ch > 126 || (!isalnum(ch) && ch != '#'))
1339 else if (entptr < (entity + sizeof(entity) - 1))
1343 mxml_error("Entity name too long under parent <%s>!",
1344 parent ? parent->value.element.name : "null");
1352 mxml_error("Character entity \"%s\" not terminated under parent <%s>!",
1353 entity, parent ? parent->value.element.name : "null");
1357 if (entity[0] == '#')
1359 if (entity[1] == 'x')
1360 ch = strtol(entity + 2, NULL, 16);
1362 ch = strtol(entity + 1, NULL, 10);
1364 else if ((ch = mxmlEntityGetValue(entity)) < 0)
1365 mxml_error("Entity name \"%s;\" not supported under parent <%s>!",
1366 entity, parent ? parent->value.element.name : "null");
1368 if (mxml_bad_char(ch))
1370 mxml_error("Bad control character 0x%02x under parent <%s> not allowed by XML standard!",
1371 ch, parent ? parent->value.element.name : "null");
1380 * 'mxml_load_data()' - Load data into an XML node tree.
1383 static mxml_node_t * /* O - First node or NULL if the file could not be read. */
1385 mxml_node_t *top, /* I - Top node */
1386 void *p, /* I - Pointer to data */
1387 mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
1388 _mxml_getc_cb_t getc_cb, /* I - Read function */
1389 mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
1390 void *sax_data) /* I - SAX user data */
1392 mxml_node_t *node, /* Current node */
1393 *first, /* First node added */
1394 *parent; /* Current parent node */
1395 int ch, /* Character from file */
1396 whitespace; /* Non-zero if whitespace seen */
1397 char *buffer, /* String buffer */
1398 *bufptr; /* Pointer into buffer */
1399 int bufsize; /* Size of buffer */
1400 mxml_type_t type; /* Current node type */
1401 int encoding; /* Character encoding */
1402 _mxml_global_t *global = _mxml_global();
1404 static const char * const types[] = /* Type strings... */
1406 "MXML_ELEMENT", /* XML element with attributes */
1407 "MXML_INTEGER", /* Integer value */
1408 "MXML_OPAQUE", /* Opaque string */
1409 "MXML_REAL", /* Real value */
1410 "MXML_TEXT", /* Text fragment */
1411 "MXML_CUSTOM" /* Custom data */
1416 * Read elements and other nodes from the file...
1419 if ((buffer = malloc(64)) == NULL)
1421 mxml_error("Unable to allocate string buffer!");
1430 encoding = ENCODE_UTF8;
1433 type = (*cb)(parent);
1439 while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1442 (mxml_isspace(ch) && type != MXML_OPAQUE && type != MXML_CUSTOM)) &&
1446 * Add a new value node...
1454 node = mxmlNewInteger(parent, strtol(buffer, &bufptr, 0));
1458 node = mxmlNewOpaque(parent, buffer);
1462 node = mxmlNewReal(parent, strtod(buffer, &bufptr));
1466 node = mxmlNewText(parent, whitespace, buffer);
1470 if (global->custom_load_cb)
1473 * Use the callback to fill in the custom data...
1476 node = mxmlNewCustom(parent, NULL, NULL);
1478 if ((*global->custom_load_cb)(node, buffer))
1480 mxml_error("Bad custom value '%s' in parent <%s>!",
1481 buffer, parent ? parent->value.element.name : "null");
1488 default : /* Ignore... */
1496 * Bad integer/real number value...
1499 mxml_error("Bad %s value '%s' in parent <%s>!",
1500 type == MXML_INTEGER ? "integer" : "real", buffer,
1501 parent ? parent->value.element.name : "null");
1506 whitespace = mxml_isspace(ch) && type == MXML_TEXT;
1508 if (!node && type != MXML_IGNORE)
1511 * Print error and return...
1514 mxml_error("Unable to add value node of type %s to parent <%s>!",
1515 types[type], parent ? parent->value.element.name : "null");
1521 (*sax_cb)(node, MXML_SAX_DATA, sax_data);
1523 if (!mxmlRelease(node))
1530 else if (mxml_isspace(ch) && type == MXML_TEXT)
1534 * Add lone whitespace node if we have an element and existing
1538 if (ch == '<' && whitespace && type == MXML_TEXT)
1542 node = mxmlNewText(parent, whitespace, "");
1546 (*sax_cb)(node, MXML_SAX_DATA, sax_data);
1548 if (!mxmlRelease(node))
1562 * Start of open/close tag...
1567 while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1568 if (mxml_isspace(ch) || ch == '>' || (ch == '/' && bufptr > buffer))
1572 mxml_error("Bare < in element!");
1577 if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb)) == EOF)
1580 if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
1583 else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
1585 else if (((bufptr - buffer) == 1 && buffer[0] == '?') ||
1586 ((bufptr - buffer) == 3 && !strncmp(buffer, "!--", 3)) ||
1587 ((bufptr - buffer) == 8 && !strncmp(buffer, "![CDATA[", 8)))
1592 if (!strcmp(buffer, "!--"))
1595 * Gather rest of comment...
1598 while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1600 if (ch == '>' && bufptr > (buffer + 4) &&
1601 bufptr[-3] != '-' && bufptr[-2] == '-' && bufptr[-1] == '-')
1603 else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
1608 * Error out if we didn't get the whole comment...
1614 * Print error and return...
1617 mxml_error("Early EOF in comment node!");
1623 * Otherwise add this as an element under the current parent...
1628 if (!parent && first)
1631 * There can only be one root element!
1634 mxml_error("<%s> cannot be a second root node after <%s>",
1635 buffer, first->value.element.name);
1639 if ((node = mxmlNewElement(parent, buffer)) == NULL)
1642 * Just print error for now...
1645 mxml_error("Unable to add comment node to parent <%s>!",
1646 parent ? parent->value.element.name : "null");
1652 (*sax_cb)(node, MXML_SAX_COMMENT, sax_data);
1654 if (!mxmlRelease(node))
1661 else if (!strcmp(buffer, "![CDATA["))
1664 * Gather CDATA section...
1667 while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1669 if (ch == '>' && !strncmp(bufptr - 2, "]]", 2))
1671 else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
1676 * Error out if we didn't get the whole comment...
1682 * Print error and return...
1685 mxml_error("Early EOF in CDATA node!");
1691 * Otherwise add this as an element under the current parent...
1696 if (!parent && first)
1699 * There can only be one root element!
1702 mxml_error("<%s> cannot be a second root node after <%s>",
1703 buffer, first->value.element.name);
1707 if ((node = mxmlNewElement(parent, buffer)) == NULL)
1710 * Print error and return...
1713 mxml_error("Unable to add CDATA node to parent <%s>!",
1714 parent ? parent->value.element.name : "null");
1720 (*sax_cb)(node, MXML_SAX_CDATA, sax_data);
1722 if (!mxmlRelease(node))
1729 else if (buffer[0] == '?')
1732 * Gather rest of processing instruction...
1735 while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1737 if (ch == '>' && bufptr > buffer && bufptr[-1] == '?')
1739 else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
1744 * Error out if we didn't get the whole processing instruction...
1750 * Print error and return...
1753 mxml_error("Early EOF in processing instruction node!");
1758 * Otherwise add this as an element under the current parent...
1763 if (!parent && first)
1766 * There can only be one root element!
1769 mxml_error("<%s> cannot be a second root node after <%s>",
1770 buffer, first->value.element.name);
1774 if ((node = mxmlNewElement(parent, buffer)) == NULL)
1777 * Print error and return...
1780 mxml_error("Unable to add processing instruction node to parent <%s>!",
1781 parent ? parent->value.element.name : "null");
1787 (*sax_cb)(node, MXML_SAX_DIRECTIVE, sax_data);
1789 if (!mxmlRelease(node))
1803 type = (*cb)(parent);
1807 else if (buffer[0] == '!')
1810 * Gather rest of declaration...
1820 if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb)) == EOF)
1823 if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
1827 while ((ch = (*getc_cb)(p, &encoding)) != EOF);
1830 * Error out if we didn't get the whole declaration...
1836 * Print error and return...
1839 mxml_error("Early EOF in declaration node!");
1844 * Otherwise add this as an element under the current parent...
1849 if (!parent && first)
1852 * There can only be one root element!
1855 mxml_error("<%s> cannot be a second root node after <%s>",
1856 buffer, first->value.element.name);
1860 if ((node = mxmlNewElement(parent, buffer)) == NULL)
1863 * Print error and return...
1866 mxml_error("Unable to add declaration node to parent <%s>!",
1867 parent ? parent->value.element.name : "null");
1873 (*sax_cb)(node, MXML_SAX_DIRECTIVE, sax_data);
1875 if (!mxmlRelease(node))
1889 type = (*cb)(parent);
1893 else if (buffer[0] == '/')
1896 * Handle close tag...
1899 if (!parent || strcmp(buffer + 1, parent->value.element.name))
1902 * Close tag doesn't match tree; print an error for now...
1905 mxml_error("Mismatched close tag <%s> under parent <%s>!",
1906 buffer, parent ? parent->value.element.name : "(null)");
1911 * Keep reading until we see >...
1914 while (ch != '>' && ch != EOF)
1915 ch = (*getc_cb)(p, &encoding);
1918 parent = parent->parent;
1922 (*sax_cb)(node, MXML_SAX_ELEMENT_CLOSE, sax_data);
1924 if (!mxmlRelease(node) && first == node)
1929 * Ascend into the parent and set the value type as needed...
1933 type = (*cb)(parent);
1938 * Handle open tag...
1941 if (!parent && first)
1944 * There can only be one root element!
1947 mxml_error("<%s> cannot be a second root node after <%s>",
1948 buffer, first->value.element.name);
1952 if ((node = mxmlNewElement(parent, buffer)) == NULL)
1955 * Just print error for now...
1958 mxml_error("Unable to add element node to parent <%s>!",
1959 parent ? parent->value.element.name : "null");
1963 if (mxml_isspace(ch))
1965 if ((ch = mxml_parse_element(node, p, &encoding, getc_cb)) == EOF)
1970 if ((ch = (*getc_cb)(p, &encoding)) != '>')
1972 mxml_error("Expected > but got '%c' instead for element <%s/>!",
1982 (*sax_cb)(node, MXML_SAX_ELEMENT_OPEN, sax_data);
1993 * Descend into this node, setting the value type as needed...
1999 type = (*cb)(parent);
2003 (*sax_cb)(node, MXML_SAX_ELEMENT_CLOSE, sax_data);
2005 if (!mxmlRelease(node) && first == node)
2015 * Add character entity to current buffer...
2018 if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb)) == EOF)
2021 if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
2024 else if (type == MXML_OPAQUE || type == MXML_CUSTOM || !mxml_isspace(ch))
2027 * Add character to current buffer...
2030 if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
2036 * Free the string buffer - we don't need it anymore...
2042 * Find the top element and return it...
2049 while (parent != top && parent->parent)
2050 parent = parent->parent;
2054 mxml_error("Missing close tag </%s> under parent <%s>!",
2055 node->value.element.name,
2056 node->parent ? node->parent->value.element.name : "(null)");
2070 * Common error return...
2084 * 'mxml_parse_element()' - Parse an element for any attributes...
2087 static int /* O - Terminating character */
2089 mxml_node_t *node, /* I - Element node */
2090 void *p, /* I - Data to read from */
2091 int *encoding, /* IO - Encoding */
2092 _mxml_getc_cb_t getc_cb) /* I - Data callback */
2094 int ch, /* Current character in file */
2095 quote; /* Quoting character */
2096 char *name, /* Attribute name */
2097 *value, /* Attribute value */
2098 *ptr; /* Pointer into name/value */
2099 int namesize, /* Size of name string */
2100 valsize; /* Size of value string */
2104 * Initialize the name and value buffers...
2107 if ((name = malloc(64)) == NULL)
2109 mxml_error("Unable to allocate memory for name!");
2115 if ((value = malloc(64)) == NULL)
2118 mxml_error("Unable to allocate memory for value!");
2125 * Loop until we hit a >, /, ?, or EOF...
2128 while ((ch = (*getc_cb)(p, encoding)) != EOF)
2131 fprintf(stderr, "parse_element: ch='%c'\n", ch);
2132 #endif /* DEBUG > 1 */
2135 * Skip leading whitespace...
2138 if (mxml_isspace(ch))
2142 * Stop at /, ?, or >...
2145 if (ch == '/' || ch == '?')
2148 * Grab the > character and print an error if it isn't there...
2151 quote = (*getc_cb)(p, encoding);
2155 mxml_error("Expected '>' after '%c' for element %s, but got '%c'!",
2156 ch, node->value.element.name, quote);
2164 mxml_error("Bare < in element %s!", node->value.element.name);
2171 * Read the attribute name...
2177 if (ch == '\"' || ch == '\'')
2180 * Name is in quotes, so get a quoted string...
2185 while ((ch = (*getc_cb)(p, encoding)) != EOF)
2188 if ((ch = mxml_get_entity(node, p, encoding, getc_cb)) == EOF)
2191 if (mxml_add_char(ch, &ptr, &name, &namesize))
2201 * Grab an normal, non-quoted name...
2204 while ((ch = (*getc_cb)(p, encoding)) != EOF)
2205 if (mxml_isspace(ch) || ch == '=' || ch == '/' || ch == '>' ||
2211 if ((ch = mxml_get_entity(node, p, encoding, getc_cb)) == EOF)
2214 if (mxml_add_char(ch, &ptr, &name, &namesize))
2221 if (mxmlElementGetAttr(node, name))
2224 while (ch != EOF && mxml_isspace(ch))
2225 ch = (*getc_cb)(p, encoding);
2230 * Read the attribute value...
2233 while ((ch = (*getc_cb)(p, encoding)) != EOF && mxml_isspace(ch));
2237 mxml_error("Missing value for attribute '%s' in element %s!",
2238 name, node->value.element.name);
2242 if (ch == '\'' || ch == '\"')
2245 * Read quoted value...
2251 while ((ch = (*getc_cb)(p, encoding)) != EOF)
2257 if ((ch = mxml_get_entity(node, p, encoding, getc_cb)) == EOF)
2260 if (mxml_add_char(ch, &ptr, &value, &valsize))
2269 * Read unquoted value...
2275 while ((ch = (*getc_cb)(p, encoding)) != EOF)
2276 if (mxml_isspace(ch) || ch == '=' || ch == '/' || ch == '>')
2281 if ((ch = mxml_get_entity(node, p, encoding, getc_cb)) == EOF)
2284 if (mxml_add_char(ch, &ptr, &value, &valsize))
2292 * Set the attribute with the given string value...
2295 mxmlElementSetAttr(node, name, value);
2299 mxml_error("Missing value for attribute '%s' in element %s!",
2300 name, node->value.element.name);
2305 * Check the end character...
2308 if (ch == '/' || ch == '?')
2311 * Grab the > character and print an error if it isn't there...
2314 quote = (*getc_cb)(p, encoding);
2318 mxml_error("Expected '>' after '%c' for element %s, but got '%c'!",
2319 ch, node->value.element.name, quote);
2330 * Free the name and value buffers and return...
2339 * Common error return point...
2352 * 'mxml_string_getc()' - Get a character from a string.
2355 static int /* O - Character or EOF */
2356 mxml_string_getc(void *p, /* I - Pointer to file */
2357 int *encoding) /* IO - Encoding */
2359 int ch; /* Character */
2360 const char **s; /* Pointer to string pointer */
2363 s = (const char **)p;
2365 if ((ch = (*s)[0] & 255) != 0 || *encoding == ENCODE_UTF16LE)
2368 * Got character; convert UTF-8 to integer and return...
2379 printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
2380 #endif /* DEBUG > 1 */
2382 if (mxml_bad_char(ch))
2384 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
2391 else if (ch == 0xfe)
2394 * UTF-16 big-endian BOM?
2397 if (((*s)[0] & 255) != 0xff)
2400 *encoding = ENCODE_UTF16BE;
2403 return (mxml_string_getc(p, encoding));
2405 else if (ch == 0xff)
2408 * UTF-16 little-endian BOM?
2411 if (((*s)[0] & 255) != 0xfe)
2414 *encoding = ENCODE_UTF16LE;
2417 return (mxml_string_getc(p, encoding));
2419 else if ((ch & 0xe0) == 0xc0)
2425 if (((*s)[0] & 0xc0) != 0x80)
2428 ch = ((ch & 0x1f) << 6) | ((*s)[0] & 0x3f);
2434 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
2439 printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
2440 #endif /* DEBUG > 1 */
2444 else if ((ch & 0xf0) == 0xe0)
2447 * Three-byte value...
2450 if (((*s)[0] & 0xc0) != 0x80 ||
2451 ((*s)[1] & 0xc0) != 0x80)
2454 ch = ((((ch & 0x0f) << 6) | ((*s)[0] & 0x3f)) << 6) | ((*s)[1] & 0x3f);
2460 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
2465 * Ignore (strip) Byte Order Mark (BOM)...
2469 return (mxml_string_getc(p, encoding));
2472 printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
2473 #endif /* DEBUG > 1 */
2477 else if ((ch & 0xf8) == 0xf0)
2480 * Four-byte value...
2483 if (((*s)[0] & 0xc0) != 0x80 ||
2484 ((*s)[1] & 0xc0) != 0x80 ||
2485 ((*s)[2] & 0xc0) != 0x80)
2488 ch = ((((((ch & 0x07) << 6) | ((*s)[0] & 0x3f)) << 6) |
2489 ((*s)[1] & 0x3f)) << 6) | ((*s)[2] & 0x3f);
2495 mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
2500 printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
2501 #endif /* DEBUG > 1 */
2508 case ENCODE_UTF16BE :
2510 * Read UTF-16 big-endian char...
2513 ch = (ch << 8) | ((*s)[0] & 255);
2516 if (mxml_bad_char(ch))
2518 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
2522 else if (ch >= 0xd800 && ch <= 0xdbff)
2525 * Multi-word UTF-16 char...
2528 int lch; /* Lower word */
2534 lch = (((*s)[0] & 255) << 8) | ((*s)[1] & 255);
2537 if (lch < 0xdc00 || lch >= 0xdfff)
2540 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
2544 printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
2545 #endif /* DEBUG > 1 */
2549 case ENCODE_UTF16LE :
2551 * Read UTF-16 little-endian char...
2554 ch = ch | (((*s)[0] & 255) << 8);
2564 if (mxml_bad_char(ch))
2566 mxml_error("Bad control character 0x%02x not allowed by XML standard!",
2570 else if (ch >= 0xd800 && ch <= 0xdbff)
2573 * Multi-word UTF-16 char...
2576 int lch; /* Lower word */
2582 lch = (((*s)[1] & 255) << 8) | ((*s)[0] & 255);
2585 if (lch < 0xdc00 || lch >= 0xdfff)
2588 ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
2592 printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
2593 #endif /* DEBUG > 1 */
2604 * 'mxml_string_putc()' - Write a character to a string.
2607 static int /* O - 0 on success, -1 on failure */
2608 mxml_string_putc(int ch, /* I - Character to write */
2609 void *p) /* I - Pointer to string pointers */
2611 char **pp; /* Pointer to string pointers */
2630 * 'mxml_write_name()' - Write a name string.
2633 static int /* O - 0 on success, -1 on failure */
2634 mxml_write_name(const char *s, /* I - Name to write */
2635 void *p, /* I - Write pointer */
2636 int (*putc_cb)(int, void *))
2637 /* I - Write callback */
2639 char quote; /* Quote character */
2640 const char *name; /* Entity name */
2643 if (*s == '\"' || *s == '\'')
2646 * Write a quoted name string...
2649 if ((*putc_cb)(*s, p) < 0)
2654 while (*s && *s != quote)
2656 if ((name = mxmlEntityGetName(*s)) != NULL)
2658 if ((*putc_cb)('&', p) < 0)
2663 if ((*putc_cb)(*name, p) < 0)
2669 if ((*putc_cb)(';', p) < 0)
2672 else if ((*putc_cb)(*s, p) < 0)
2679 * Write the end quote...
2682 if ((*putc_cb)(quote, p) < 0)
2688 * Write a non-quoted name string...
2693 if ((*putc_cb)(*s, p) < 0)
2705 * 'mxml_write_node()' - Save an XML node to a file.
2708 static int /* O - Column or -1 on error */
2709 mxml_write_node(mxml_node_t *node, /* I - Node to write */
2710 void *p, /* I - File to write to */
2711 mxml_save_cb_t cb, /* I - Whitespace callback */
2712 int col, /* I - Current column */
2713 _mxml_putc_cb_t putc_cb,/* I - Output callback */
2714 _mxml_global_t *global)/* I - Global data */
2716 int i, /* Looping var */
2717 width; /* Width of attr + value */
2718 mxml_attr_t *attr; /* Current attribute */
2719 char s[255]; /* Temporary string */
2723 * Print the node value...
2729 col = mxml_write_ws(node, p, cb, MXML_WS_BEFORE_OPEN, col, putc_cb);
2731 if ((*putc_cb)('<', p) < 0)
2733 if (node->value.element.name[0] == '?' ||
2734 !strncmp(node->value.element.name, "!--", 3) ||
2735 !strncmp(node->value.element.name, "![CDATA[", 8))
2738 * Comments, CDATA, and processing instructions do not
2739 * use character entities.
2742 const char *ptr; /* Pointer into name */
2745 for (ptr = node->value.element.name; *ptr; ptr ++)
2746 if ((*putc_cb)(*ptr, p) < 0)
2749 else if (mxml_write_name(node->value.element.name, p, putc_cb) < 0)
2752 col += strlen(node->value.element.name) + 1;
2754 for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
2758 width = strlen(attr->name);
2761 width += strlen(attr->value) + 3;
2763 if (global->wrap > 0 && (col + width) > global->wrap)
2765 if ((*putc_cb)('\n', p) < 0)
2772 if ((*putc_cb)(' ', p) < 0)
2778 if (mxml_write_name(attr->name, p, putc_cb) < 0)
2783 if ((*putc_cb)('=', p) < 0)
2785 if ((*putc_cb)('\"', p) < 0)
2787 if (mxml_write_string(attr->value, p, putc_cb) < 0)
2789 if ((*putc_cb)('\"', p) < 0)
2802 mxml_node_t *child; /* Current child */
2805 if ((*putc_cb)('>', p) < 0)
2810 col = mxml_write_ws(node, p, cb, MXML_WS_AFTER_OPEN, col, putc_cb);
2812 for (child = node->child; child; child = child->next)
2814 if ((col = mxml_write_node(child, p, cb, col, putc_cb, global)) < 0)
2819 * The ? and ! elements are special-cases and have no end tags...
2822 if (node->value.element.name[0] != '!' &&
2823 node->value.element.name[0] != '?')
2825 col = mxml_write_ws(node, p, cb, MXML_WS_BEFORE_CLOSE, col, putc_cb);
2827 if ((*putc_cb)('<', p) < 0)
2829 if ((*putc_cb)('/', p) < 0)
2831 if (mxml_write_string(node->value.element.name, p, putc_cb) < 0)
2833 if ((*putc_cb)('>', p) < 0)
2836 col += strlen(node->value.element.name) + 3;
2838 col = mxml_write_ws(node, p, cb, MXML_WS_AFTER_CLOSE, col, putc_cb);
2841 else if (node->value.element.name[0] == '!' ||
2842 node->value.element.name[0] == '?')
2845 * The ? and ! elements are special-cases...
2848 if ((*putc_cb)('>', p) < 0)
2853 col = mxml_write_ws(node, p, cb, MXML_WS_AFTER_OPEN, col, putc_cb);
2857 if ((*putc_cb)(' ', p) < 0)
2859 if ((*putc_cb)('/', p) < 0)
2861 if ((*putc_cb)('>', p) < 0)
2866 col = mxml_write_ws(node, p, cb, MXML_WS_AFTER_OPEN, col, putc_cb);
2873 if (global->wrap > 0 && col > global->wrap)
2875 if ((*putc_cb)('\n', p) < 0)
2880 else if ((*putc_cb)(' ', p) < 0)
2886 sprintf(s, "%d", node->value.integer);
2887 if (mxml_write_string(s, p, putc_cb) < 0)
2894 if (mxml_write_string(node->value.opaque, p, putc_cb) < 0)
2897 col += strlen(node->value.opaque);
2903 if (global->wrap > 0 && col > global->wrap)
2905 if ((*putc_cb)('\n', p) < 0)
2910 else if ((*putc_cb)(' ', p) < 0)
2916 sprintf(s, "%f", node->value.real);
2917 if (mxml_write_string(s, p, putc_cb) < 0)
2924 if (node->value.text.whitespace && col > 0)
2926 if (global->wrap > 0 && col > global->wrap)
2928 if ((*putc_cb)('\n', p) < 0)
2933 else if ((*putc_cb)(' ', p) < 0)
2939 if (mxml_write_string(node->value.text.string, p, putc_cb) < 0)
2942 col += strlen(node->value.text.string);
2946 if (global->custom_save_cb)
2948 char *data; /* Custom data string */
2949 const char *newline; /* Last newline in string */
2952 if ((data = (*global->custom_save_cb)(node)) == NULL)
2955 if (mxml_write_string(data, p, putc_cb) < 0)
2958 if ((newline = strrchr(data, '\n')) == NULL)
2959 col += strlen(data);
2961 col = strlen(newline);
2967 default : /* Should never happen */
2976 * 'mxml_write_string()' - Write a string, escaping & and < as needed.
2979 static int /* O - 0 on success, -1 on failure */
2981 const char *s, /* I - String to write */
2982 void *p, /* I - Write pointer */
2983 _mxml_putc_cb_t putc_cb) /* I - Write callback */
2985 const char *name; /* Entity name, if any */
2990 if ((name = mxmlEntityGetName(*s)) != NULL)
2992 if ((*putc_cb)('&', p) < 0)
2997 if ((*putc_cb)(*name, p) < 0)
3002 if ((*putc_cb)(';', p) < 0)
3005 else if ((*putc_cb)(*s, p) < 0)
3016 * 'mxml_write_ws()' - Do whitespace callback...
3019 static int /* O - New column */
3020 mxml_write_ws(mxml_node_t *node, /* I - Current node */
3021 void *p, /* I - Write pointer */
3022 mxml_save_cb_t cb, /* I - Callback function */
3023 int ws, /* I - Where value */
3024 int col, /* I - Current column */
3025 _mxml_putc_cb_t putc_cb) /* I - Write callback */
3027 const char *s; /* Whitespace string */
3030 if (cb && (s = (*cb)(node, ws)) != NULL)
3034 if ((*putc_cb)(*s, p) < 0)
3036 else if (*s == '\n')
3038 else if (*s == '\t')
3041 col = col - (col % MXML_TAB);
3055 * End of "$Id: mxml-file.c 455 2014-01-05 03:28:03Z msweet $".