mxml-file.c 65.5 KB
Newer Older
Michael R Sweet's avatar
Michael R Sweet committed
1
/*
Michael Sweet's avatar
Michael Sweet committed
2
 * File loading code for Mini-XML, a small XML file parsing library.
Michael R Sweet's avatar
Michael R Sweet committed
3
 *
4
 * Copyright 2003-2018 by Michael R Sweet.
Michael R Sweet's avatar
Michael R Sweet committed
5
 *
6 7 8 9 10
 * These coded instructions, statements, and computer programs are the
 * property of Michael R Sweet and are protected by Federal copyright
 * law.  Distribution and use rights are outlined in the file "COPYING"
 * which should have been included with this file.  If this file is
 * missing or damaged, see the license at:
Michael R Sweet's avatar
Michael R Sweet committed
11
 *
Michael Sweet's avatar
Michael Sweet committed
12
 *     https://michaelrsweet.github.io/mxml
Michael R Sweet's avatar
Michael R Sweet committed
13 14 15 16 17 18
 */

/*
 * Include necessary headers...
 */

19
#ifndef WIN32
20
#  include <unistd.h>
21 22
#endif /* !WIN32 */
#include "mxml-private.h"
Michael R Sweet's avatar
Michael R Sweet committed
23 24


Michael R Sweet's avatar
Michael R Sweet committed
25 26 27 28 29 30 31 32 33
/*
 * Character encoding...
 */

#define ENCODE_UTF8	0		/* UTF-8 */
#define ENCODE_UTF16BE	1		/* UTF-16 Big-Endian */
#define ENCODE_UTF16LE	2		/* UTF-16 Little-Endian */


Michael R Sweet's avatar
Michael R Sweet committed
34 35 36 37 38 39 40
/*
 * Macro to test for a bad XML character...
 */

#define mxml_bad_char(ch) ((ch) < ' ' && (ch) != '\n' && (ch) != '\r' && (ch) != '\t')


41
/*
42
 * Types and structures...
43 44
 */

45 46 47 48
typedef int (*_mxml_getc_cb_t)(void *, int *);
typedef int (*_mxml_putc_cb_t)(int, void *);

typedef struct _mxml_fdbuf_s		/**** File descriptor buffer ****/
49 50 51 52 53
{
  int		fd;			/* File descriptor */
  unsigned char	*current,		/* Current position in buffer */
		*end,			/* End of buffer */
		buffer[8192];		/* Character buffer */
54
} _mxml_fdbuf_t;
55 56


Michael R Sweet's avatar
Michael R Sweet committed
57 58 59 60
/*
 * Local functions...
 */

61 62
static int		mxml_add_char(int ch, char **ptr, char **buffer,
			              int *bufsize);
63 64
static int		mxml_fd_getc(void *p, int *encoding);
static int		mxml_fd_putc(int ch, void *p);
65 66
static int		mxml_fd_read(_mxml_fdbuf_t *buf);
static int		mxml_fd_write(_mxml_fdbuf_t *buf);
67 68
static int		mxml_file_getc(void *p, int *encoding);
static int		mxml_file_putc(int ch, void *p);
69
static int		mxml_get_entity(mxml_node_t *parent, void *p,
Michael R Sweet's avatar
Michael R Sweet committed
70
			                int *encoding,
71
					_mxml_getc_cb_t getc_cb);
72 73 74
static inline int	mxml_isspace(int ch)
			{
			  return (ch == ' ' || ch == '\t' || ch == '\r' ||
75
			          ch == '\n');
76
			}
77
static mxml_node_t	*mxml_load_data(mxml_node_t *top, void *p,
78 79 80
			                mxml_load_cb_t cb,
			                _mxml_getc_cb_t getc_cb,
                                        mxml_sax_cb_t sax_cb, void *sax_data);
81
static int		mxml_parse_element(mxml_node_t *node, void *p,
Michael R Sweet's avatar
Michael R Sweet committed
82
			                   int *encoding,
83
					   _mxml_getc_cb_t getc_cb);
Michael R Sweet's avatar
Michael R Sweet committed
84
static int		mxml_string_getc(void *p, int *encoding);
85
static int		mxml_string_putc(int ch, void *p);
86
static int		mxml_write_name(const char *s, void *p,
87
					_mxml_putc_cb_t putc_cb);
88
static int		mxml_write_node(mxml_node_t *node, void *p,
89
			                mxml_save_cb_t cb, int col,
90 91
					_mxml_putc_cb_t putc_cb,
					_mxml_global_t *global);
92
static int		mxml_write_string(const char *s, void *p,
93
					  _mxml_putc_cb_t putc_cb);
94
static int		mxml_write_ws(mxml_node_t *node, void *p,
95 96
			              mxml_save_cb_t cb, int ws,
				      int col, _mxml_putc_cb_t putc_cb);
97 98


99 100 101 102 103 104 105
/*
 * 'mxmlLoadFd()' - Load a file descriptor into an XML node tree.
 *
 * The nodes in the specified file are added to the specified top node.
 * If no top node is provided, the XML file MUST be well-formed with a
 * single parent node like <?xml> for the entire file. The callback
 * function returns the value type that should be used for child nodes.
106 107 108
 * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
 * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
 * loading child (data) nodes of the specified type.
109 110 111 112 113 114
 *
 * Note: The most common programming error when using the Mini-XML library is
 * to load an XML file using the @code MXML_TEXT_CALLBACK@, which returns inline
 * text as a series of whitespace-delimited words, instead of using the
 * @code MXML_OPAQUE_CALLBACK@ which returns the inline text as a single string
 * (including whitespace).
115 116
 */

117
mxml_node_t *				/* O - First node or @code NULL@ if the file could not be read. */
118 119
mxmlLoadFd(mxml_node_t    *top,		/* I - Top node */
           int            fd,		/* I - File descriptor to read from */
120
           mxml_load_cb_t cb)		/* I - Callback function or constant */
121
{
122
  _mxml_fdbuf_t	buf;			/* File descriptor buffer */
123 124 125 126 127 128 129 130 131 132 133 134 135 136


 /*
  * Initialize the file descriptor buffer...
  */

  buf.fd      = fd;
  buf.current = buf.buffer;
  buf.end     = buf.buffer;

 /*
  * Read the XML data...
  */

137
  return (mxml_load_data(top, &buf, cb, mxml_fd_getc, MXML_NO_CALLBACK, NULL));
138 139 140
}


141
/*
142
 * 'mxmlLoadFile()' - Load a file into an XML node tree.
143
 *
144 145 146
 * The nodes in the specified file are added to the specified top node.
 * If no top node is provided, the XML file MUST be well-formed with a
 * single parent node like <?xml> for the entire file. The callback
147
 * function returns the value type that should be used for child nodes.
148 149 150
 * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
 * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
 * loading child (data) nodes of the specified type.
151 152 153 154 155 156
 *
 * Note: The most common programming error when using the Mini-XML library is
 * to load an XML file using the @code MXML_TEXT_CALLBACK@, which returns inline
 * text as a series of whitespace-delimited words, instead of using the
 * @code MXML_OPAQUE_CALLBACK@ which returns the inline text as a single string
 * (including whitespace).
157 158
 */

159
mxml_node_t *				/* O - First node or @code NULL@ if the file could not be read. */
160 161
mxmlLoadFile(mxml_node_t    *top,	/* I - Top node */
             FILE           *fp,	/* I - File to read from */
162
             mxml_load_cb_t cb)		/* I - Callback function or constant */
163
{
164 165 166 167
 /*
  * Read the XML data...
  */

168
  return (mxml_load_data(top, fp, cb, mxml_file_getc, MXML_NO_CALLBACK, NULL));
169
}
Michael R Sweet's avatar
Michael R Sweet committed
170 171 172


/*
173
 * 'mxmlLoadString()' - Load a string into an XML node tree.
174
 *
175 176 177
 * The nodes in the specified string are added to the specified top node.
 * If no top node is provided, the XML string MUST be well-formed with a
 * single parent node like <?xml> for the entire string. The callback
178
 * function returns the value type that should be used for child nodes.
179 180 181
 * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
 * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
 * loading child (data) nodes of the specified type.
182 183 184 185 186 187
 *
 * Note: The most common programming error when using the Mini-XML library is
 * to load an XML file using the @code MXML_TEXT_CALLBACK@, which returns inline
 * text as a series of whitespace-delimited words, instead of using the
 * @code MXML_OPAQUE_CALLBACK@ which returns the inline text as a single string
 * (including whitespace).
Michael R Sweet's avatar
Michael R Sweet committed
188 189
 */

190
mxml_node_t *				/* O - First node or @code NULL@ if the string has errors. */
191 192
mxmlLoadString(mxml_node_t    *top,	/* I - Top node */
               const char     *s,	/* I - String to load */
193
               mxml_load_cb_t cb)	/* I - Callback function or constant */
194
{
195 196 197 198
 /*
  * Read the XML data...
  */

Michael R Sweet's avatar
Michael R Sweet committed
199
  return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, MXML_NO_CALLBACK,
200
                         NULL));
201 202 203
}


204
/*
205
 * 'mxmlSaveAllocString()' - Save an XML tree to an allocated string.
206 207 208
 *
 * This function returns a pointer to a string containing the textual
 * representation of the XML node tree.  The string should be freed
209
 * using the free() function when you are done with it.  @code NULL@ is returned
210 211
 * if the node would produce an empty string or if the string cannot be
 * allocated.
212 213
 *
 * The callback argument specifies a function that returns a whitespace
214 215
 * string or NULL before and after each element.  If @code MXML_NO_CALLBACK@
 * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
216 217
 * with leading whitespace and before attribute names inside opening
 * element tags.
218 219
 */

220
char *					/* O - Allocated string or @code NULL@ */
221 222
mxmlSaveAllocString(
    mxml_node_t    *node,		/* I - Node to write */
223
    mxml_save_cb_t cb)			/* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
  int	bytes;				/* Required bytes */
  char	buffer[8192];			/* Temporary buffer */
  char	*s;				/* Allocated string */


 /*
  * Write the node to the temporary buffer...
  */

  bytes = mxmlSaveString(node, buffer, sizeof(buffer), cb);

  if (bytes <= 0)
    return (NULL);

  if (bytes < (int)(sizeof(buffer) - 1))
  {
   /*
    * Node fit inside the buffer, so just duplicate that string and
    * return...
    */

    return (strdup(buffer));
  }

 /*
  * Allocate a buffer of the required size and save the node to the
  * new buffer...
  */

  if ((s = malloc(bytes + 1)) == NULL)
    return (NULL);

  mxmlSaveString(node, s, bytes + 1, cb);

 /*
  * Return the allocated string...
  */

  return (s);
}


267 268 269 270
/*
 * 'mxmlSaveFd()' - Save an XML tree to a file descriptor.
 *
 * The callback argument specifies a function that returns a whitespace
271 272
 * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
 * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
273 274 275 276 277
 * with leading whitespace and before attribute names inside opening
 * element tags.
 */

int					/* O - 0 on success, -1 on error. */
278 279
mxmlSaveFd(mxml_node_t    *node,	/* I - Node to write */
           int            fd,		/* I - File descriptor to write to */
280
	   mxml_save_cb_t cb)		/* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
281 282
{
  int		col;			/* Final column */
283
  _mxml_fdbuf_t	buf;			/* File descriptor buffer */
284 285
  _mxml_global_t *global = _mxml_global();
					/* Global data */
286 287 288 289 290 291 292 293


 /*
  * Initialize the file descriptor buffer...
  */

  buf.fd      = fd;
  buf.current = buf.buffer;
294
  buf.end     = buf.buffer + sizeof(buf.buffer);
295 296 297 298 299

 /*
  * Write the node...
  */

300
  if ((col = mxml_write_node(node, &buf, cb, 0, mxml_fd_putc, global)) < 0)
301 302 303 304 305 306 307 308 309 310 311 312 313 314
    return (-1);

  if (col > 0)
    if (mxml_fd_putc('\n', &buf) < 0)
      return (-1);

 /*
  * Flush and return...
  */

  return (mxml_fd_write(&buf));
}


315 316 317 318
/*
 * 'mxmlSaveFile()' - Save an XML tree to a file.
 *
 * The callback argument specifies a function that returns a whitespace
319 320
 * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
 * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
321 322 323 324 325
 * with leading whitespace and before attribute names inside opening
 * element tags.
 */

int					/* O - 0 on success, -1 on error. */
326 327
mxmlSaveFile(mxml_node_t    *node,	/* I - Node to write */
             FILE           *fp,	/* I - File to write to */
328
	     mxml_save_cb_t cb)		/* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
329 330
{
  int	col;				/* Final column */
331 332
  _mxml_global_t *global = _mxml_global();
					/* Global data */
333 334 335 336 337 338


 /*
  * Write the node...
  */

339
  if ((col = mxml_write_node(node, fp, cb, 0, mxml_file_putc, global)) < 0)
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    return (-1);

  if (col > 0)
    if (putc('\n', fp) < 0)
      return (-1);

 /*
  * Return 0 (success)...
  */

  return (0);
}


/*
 * 'mxmlSaveString()' - Save an XML node tree to a string.
 *
 * This function returns the total number of bytes that would be
 * required for the string but only copies (bufsize - 1) characters
 * into the specified buffer.
360 361
 *
 * The callback argument specifies a function that returns a whitespace
362 363
 * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
 * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
364 365
 * with leading whitespace and before attribute names inside opening
 * element tags.
366 367 368
 */

int					/* O - Size of string */
369 370 371
mxmlSaveString(mxml_node_t    *node,	/* I - Node to write */
               char           *buffer,	/* I - String buffer */
               int            bufsize,	/* I - Size of string buffer */
372
               mxml_save_cb_t cb)	/* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
373
{
374 375
  int	col;				/* Final column */
  char	*ptr[2];			/* Pointers for putc_cb */
376 377
  _mxml_global_t *global = _mxml_global();
					/* Global data */
378 379 380 381 382 383 384 385 386


 /*
  * Write the node...
  */

  ptr[0] = buffer;
  ptr[1] = buffer + bufsize;

387
  if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc, global)) < 0)
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    return (-1);

  if (col > 0)
    mxml_string_putc('\n', ptr);

 /*
  * Nul-terminate the buffer...
  */

  if (ptr[0] >= ptr[1])
    buffer[bufsize - 1] = '\0';
  else
    ptr[0][0] = '\0';

 /*
  * Return the number of characters...
  */

406
  return ((int)(ptr[0] - buffer));
407 408 409
}


410 411 412 413 414 415 416 417
/*
 * 'mxmlSAXLoadFd()' - Load a file descriptor into an XML node tree
 *                     using a SAX callback.
 *
 * The nodes in the specified file are added to the specified top node.
 * If no top node is provided, the XML file MUST be well-formed with a
 * single parent node like <?xml> for the entire file. The callback
 * function returns the value type that should be used for child nodes.
418 419 420
 * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
 * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
 * loading child nodes of the specified type.
421
 *
422
 * The SAX callback must call @link mxmlRetain@ for any nodes that need to
423 424 425 426 427 428
 * be kept for later use. Otherwise, nodes are deleted when the parent
 * node is closed or after each data, comment, CDATA, or directive node.
 *
 * @since Mini-XML 2.3@
 */

429
mxml_node_t *				/* O - First node or @code NULL@ if the file could not be read. */
430 431
mxmlSAXLoadFd(mxml_node_t    *top,	/* I - Top node */
              int            fd,	/* I - File descriptor to read from */
432 433
              mxml_load_cb_t cb,	/* I - Callback function or constant */
              mxml_sax_cb_t  sax_cb,	/* I - SAX callback or @code MXML_NO_CALLBACK@ */
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
              void           *sax_data)	/* I - SAX user data */
{
  _mxml_fdbuf_t	buf;			/* File descriptor buffer */


 /*
  * Initialize the file descriptor buffer...
  */

  buf.fd      = fd;
  buf.current = buf.buffer;
  buf.end     = buf.buffer;

 /*
  * Read the XML data...
  */

  return (mxml_load_data(top, &buf, cb, mxml_fd_getc, sax_cb, sax_data));
}


/*
 * 'mxmlSAXLoadFile()' - Load a file into an XML node tree
 *                       using a SAX callback.
 *
 * The nodes in the specified file are added to the specified top node.
 * If no top node is provided, the XML file MUST be well-formed with a
 * single parent node like <?xml> for the entire file. The callback
 * function returns the value type that should be used for child nodes.
463 464 465
 * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
 * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
 * loading child nodes of the specified type.
466
 *
467
 * The SAX callback must call @link mxmlRetain@ for any nodes that need to
468 469 470 471 472 473
 * be kept for later use. Otherwise, nodes are deleted when the parent
 * node is closed or after each data, comment, CDATA, or directive node.
 *
 * @since Mini-XML 2.3@
 */

474
mxml_node_t *				/* O - First node or @code NULL@ if the file could not be read. */
475 476 477
mxmlSAXLoadFile(
    mxml_node_t    *top,		/* I - Top node */
    FILE           *fp,			/* I - File to read from */
478 479
    mxml_load_cb_t cb,			/* I - Callback function or constant */
    mxml_sax_cb_t  sax_cb,		/* I - SAX callback or @code MXML_NO_CALLBACK@ */
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    void           *sax_data)		/* I - SAX user data */
{
 /*
  * Read the XML data...
  */

  return (mxml_load_data(top, fp, cb, mxml_file_getc, sax_cb, sax_data));
}


/*
 * 'mxmlSAXLoadString()' - Load a string into an XML node tree
 *                         using a SAX callback.
 *
 * The nodes in the specified string are added to the specified top node.
 * If no top node is provided, the XML string MUST be well-formed with a
 * single parent node like <?xml> for the entire string. The callback
 * function returns the value type that should be used for child nodes.
498 499 500
 * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
 * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
 * loading child nodes of the specified type.
501
 *
502
 * The SAX callback must call @link mxmlRetain@ for any nodes that need to
503 504 505 506 507 508
 * be kept for later use. Otherwise, nodes are deleted when the parent
 * node is closed or after each data, comment, CDATA, or directive node.
 *
 * @since Mini-XML 2.3@
 */

509
mxml_node_t *				/* O - First node or @code NULL@ if the string has errors. */
510 511 512
mxmlSAXLoadString(
    mxml_node_t    *top,		/* I - Top node */
    const char     *s,			/* I - String to load */
513 514
    mxml_load_cb_t cb,			/* I - Callback function or constant */
    mxml_sax_cb_t  sax_cb,		/* I - SAX callback or @code MXML_NO_CALLBACK@ */
515 516 517 518 519 520
    void           *sax_data)		/* I - SAX user data */
{
 /*
  * Read the XML data...
  */

Michael R Sweet's avatar
Michael R Sweet committed
521
  return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, sax_cb, sax_data));
522 523 524
}


525 526 527 528 529 530 531
/*
 * 'mxmlSetCustomHandlers()' - Set the handling functions for custom data.
 *
 * The load function accepts a node pointer and a data string and must
 * return 0 on success and non-zero on error.
 *
 * The save function accepts a node pointer and must return a malloc'd
532
 * string on success and @code NULL@ on error.
533
 *
534 535 536
 */

void
537 538 539
mxmlSetCustomHandlers(
    mxml_custom_load_cb_t load,		/* I - Load function */
    mxml_custom_save_cb_t save)		/* I - Save function */
540
{
541 542 543 544 545 546
  _mxml_global_t *global = _mxml_global();
					/* Global data */


  global->custom_load_cb = load;
  global->custom_save_cb = save;
547 548 549
}


Michael R Sweet's avatar
Michael R Sweet committed
550 551 552 553 554
/*
 * 'mxmlSetErrorCallback()' - Set the error message callback.
 */

void
555
mxmlSetErrorCallback(mxml_error_cb_t cb)/* I - Error callback function */
Michael R Sweet's avatar
Michael R Sweet committed
556
{
557 558 559 560 561
  _mxml_global_t *global = _mxml_global();
					/* Global data */


  global->error_cb = cb;
Michael R Sweet's avatar
Michael R Sweet committed
562 563 564
}


565
/*
566
 * 'mxmlSetWrapMargin()' - Set the wrap margin when saving XML data.
567
 *
568
 * Wrapping is disabled when "column" is 0.
569 570
 *
 * @since Mini-XML 2.3@
571 572 573
 */

void
574
mxmlSetWrapMargin(int column)		/* I - Column for wrapping, 0 to disable wrapping */
575
{
576 577 578 579
  _mxml_global_t *global = _mxml_global();
					/* Global data */


580
  global->wrap = column;
581 582 583
}


584 585 586 587 588 589 590 591 592 593 594 595 596
/*
 * 'mxml_add_char()' - Add a character to a buffer, expanding as needed.
 */

static int				/* O  - 0 on success, -1 on error */
mxml_add_char(int  ch,			/* I  - Character to add */
              char **bufptr,		/* IO - Current position in buffer */
	      char **buffer,		/* IO - Current buffer */
	      int  *bufsize)		/* IO - Current buffer size */
{
  char	*newbuffer;			/* New buffer value */


597
  if (*bufptr >= (*buffer + *bufsize - 4))
598 599 600 601 602 603 604 605 606 607 608 609 610 611
  {
   /*
    * Increase the size of the buffer...
    */

    if (*bufsize < 1024)
      (*bufsize) *= 2;
    else
      (*bufsize) += 1024;

    if ((newbuffer = realloc(*buffer, *bufsize)) == NULL)
    {
      free(*buffer);

Michael R Sweet's avatar
Michael R Sweet committed
612
      mxml_error("Unable to expand string buffer to %d bytes!", *bufsize);
613 614 615 616 617

      return (-1);
    }

    *bufptr = newbuffer + (*bufptr - *buffer);
618
    *buffer = newbuffer;
619 620
  }

621
  if (ch < 0x80)
622 623 624 625 626 627 628
  {
   /*
    * Single byte ASCII...
    */

    *(*bufptr)++ = ch;
  }
629
  else if (ch < 0x800)
630 631 632 633 634 635 636 637
  {
   /*
    * Two-byte UTF-8...
    */

    *(*bufptr)++ = 0xc0 | (ch >> 6);
    *(*bufptr)++ = 0x80 | (ch & 0x3f);
  }
638
  else if (ch < 0x10000)
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
  {
   /*
    * Three-byte UTF-8...
    */

    *(*bufptr)++ = 0xe0 | (ch >> 12);
    *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
    *(*bufptr)++ = 0x80 | (ch & 0x3f);
  }
  else
  {
   /*
    * Four-byte UTF-8...
    */

    *(*bufptr)++ = 0xf0 | (ch >> 18);
    *(*bufptr)++ = 0x80 | ((ch >> 12) & 0x3f);
    *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
    *(*bufptr)++ = 0x80 | (ch & 0x3f);
  }
659 660 661 662 663

  return (0);
}


664
/*
665
 * 'mxml_fd_getc()' - Read a character from a file descriptor.
666 667
 */

668 669 670
static int				/* O  - Character or EOF */
mxml_fd_getc(void *p,			/* I  - File descriptor buffer */
             int  *encoding)		/* IO - Encoding */
671
{
672
  _mxml_fdbuf_t	*buf;			/* File descriptor buffer */
673 674
  int		ch,			/* Current character */
		temp;			/* Temporary character */
675 676


677 678 679
 /*
  * Grab the next character in the buffer...
  */
680

681
  buf = (_mxml_fdbuf_t *)p;
682

683 684 685
  if (buf->current >= buf->end)
    if (mxml_fd_read(buf) < 0)
      return (EOF);
686

687 688 689
  ch = *(buf->current)++;

  switch (*encoding)
690
  {
691 692 693 694 695 696
    case ENCODE_UTF8 :
       /*
	* Got a UTF-8 character; convert UTF-8 to Unicode and return...
	*/

	if (!(ch & 0x80))
Michael R Sweet's avatar
Michael R Sweet committed
697 698 699 700 701 702 703 704 705 706 707 708
	{
#if DEBUG > 1
          printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */

	  if (mxml_bad_char(ch))
	  {
	    mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	       ch);
	    return (EOF);
	  }

709
	  return (ch);
Michael R Sweet's avatar
Michael R Sweet committed
710 711
        }
	else if (ch == 0xfe)
712 713 714 715 716 717 718 719 720 721
	{
	 /*
	  * UTF-16 big-endian BOM?
	  */

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  ch = *(buf->current)++;
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	  if (ch != 0xff)
	    return (EOF);

	  *encoding = ENCODE_UTF16BE;

	  return (mxml_fd_getc(p, encoding));
	}
	else if (ch == 0xff)
	{
	 /*
	  * UTF-16 little-endian BOM?
	  */

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  ch = *(buf->current)++;
741

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	  if (ch != 0xfe)
	    return (EOF);

	  *encoding = ENCODE_UTF16LE;

	  return (mxml_fd_getc(p, encoding));
	}
	else if ((ch & 0xe0) == 0xc0)
	{
	 /*
	  * Two-byte value...
	  */

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  if ((temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
765 766

	  if (ch < 0x80)
767 768
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
769
	    return (EOF);
770
	  }
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	}
	else if ((ch & 0xf0) == 0xe0)
	{
	 /*
	  * Three-byte value...
	  */

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  if ((temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = ((ch & 0x0f) << 6) | (temp & 0x3f);

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  if ((temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = (ch << 6) | (temp & 0x3f);
799 800

	  if (ch < 0x800)
801 802 803 804 805
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
	    return (EOF);
	  }

806 807 808 809 810 811
         /*
	  * Ignore (strip) Byte Order Mark (BOM)...
	  */

	  if (ch == 0xfeff)
	    return (mxml_fd_getc(p, encoding));
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
	}
	else if ((ch & 0xf8) == 0xf0)
	{
	 /*
	  * Four-byte value...
	  */

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  if ((temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = ((ch & 0x07) << 6) | (temp & 0x3f);

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  if ((temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = (ch << 6) | (temp & 0x3f);

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  if ((temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = (ch << 6) | (temp & 0x3f);
851 852

	  if (ch < 0x10000)
853 854
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
855
	    return (EOF);
856
	  }
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
	}
	else
	  return (EOF);
	break;

    case ENCODE_UTF16BE :
       /*
        * Read UTF-16 big-endian char...
	*/

	if (buf->current >= buf->end)
	  if (mxml_fd_read(buf) < 0)
	    return (EOF);

	temp = *(buf->current)++;

	ch = (ch << 8) | temp;

Michael R Sweet's avatar
Michael R Sweet committed
875 876 877 878 879 880 881
	if (mxml_bad_char(ch))
	{
	  mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	     ch);
	  return (EOF);
	}
        else if (ch >= 0xd800 && ch <= 0xdbff)
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
	{
	 /*
	  * Multi-word UTF-16 char...
	  */

          int lch;

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  lch = *(buf->current)++;

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  lch = (lch << 8) | temp;

          if (lch < 0xdc00 || lch >= 0xdfff)
	    return (EOF);

          ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
	}
	break;

    case ENCODE_UTF16LE :
       /*
        * Read UTF-16 little-endian char...
	*/

	if (buf->current >= buf->end)
	  if (mxml_fd_read(buf) < 0)
	    return (EOF);

	temp = *(buf->current)++;

	ch |= (temp << 8);

Michael R Sweet's avatar
Michael R Sweet committed
923 924 925 926 927 928 929
        if (mxml_bad_char(ch))
	{
	  mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	     ch);
	  return (EOF);
	}
        else if (ch >= 0xd800 && ch <= 0xdbff)
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	{
	 /*
	  * Multi-word UTF-16 char...
	  */

          int lch;

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  lch = *(buf->current)++;

	  if (buf->current >= buf->end)
	    if (mxml_fd_read(buf) < 0)
	      return (EOF);

	  temp = *(buf->current)++;

	  lch |= (temp << 8);

          if (lch < 0xdc00 || lch >= 0xdfff)
	    return (EOF);

          ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
	}
	break;
957 958
  }

Michael R Sweet's avatar
Michael R Sweet committed
959 960 961 962
#if DEBUG > 1
  printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */

963 964 965 966 967 968 969 970 971 972 973 974
  return (ch);
}


/*
 * 'mxml_fd_putc()' - Write a character to a file descriptor.
 */

static int				/* O - 0 on success, -1 on error */
mxml_fd_putc(int  ch,			/* I - Character */
             void *p)			/* I - File descriptor buffer */
{
975
  _mxml_fdbuf_t	*buf;			/* File descriptor buffer */
976 977 978


 /*
979
  * Flush the write buffer as needed...
980 981
  */

982
  buf = (_mxml_fdbuf_t *)p;
983 984 985 986 987

  if (buf->current >= buf->end)
    if (mxml_fd_write(buf) < 0)
      return (-1);

988
  *(buf->current)++ = ch;
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

 /*
  * Return successfully...
  */

  return (0);
}


/*
 * 'mxml_fd_read()' - Read a buffer of data from a file descriptor.
 */

static int				/* O - 0 on success, -1 on error */
1003
mxml_fd_read(_mxml_fdbuf_t *buf)		/* I - File descriptor buffer */
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
{
  int	bytes;				/* Bytes read... */


 /*
  * Range check input...
  */

  if (!buf)
    return (-1);

 /*
  * Read from the file descriptor...
  */

1019
  while ((bytes = (int)read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)
1020
#ifdef EINTR
1021
    if (errno != EAGAIN && errno != EINTR)
1022 1023 1024
#else
    if (errno != EAGAIN)
#endif /* EINTR */
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
      return (-1);

  if (bytes == 0)
    return (-1);

 /*
  * Update the pointers and return success...
  */

  buf->current = buf->buffer;
  buf->end     = buf->buffer + bytes;

  return (0);
}


/*
 * 'mxml_fd_write()' - Write a buffer of data to a file descriptor.
 */

static int				/* O - 0 on success, -1 on error */
1046
mxml_fd_write(_mxml_fdbuf_t *buf)	/* I - File descriptor buffer */
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
{
  int		bytes;			/* Bytes written */
  unsigned char	*ptr;			/* Pointer into buffer */


 /*
  * Range check...
  */

  if (!buf)
    return (-1);

 /*
  * Return 0 if there is nothing to write...
  */

  if (buf->current == buf->buffer)
    return (0);

 /*
  * Loop until we have written everything...
  */

  for (ptr = buf->buffer; ptr < buf->current; ptr += bytes)
1071
    if ((bytes = (int)write(buf->fd, ptr, buf->current - ptr)) < 0)
1072 1073 1074 1075 1076 1077 1078 1079 1080
      return (-1);

 /*
  * All done, reset pointers and return success...
  */

  buf->current = buf->buffer;

  return (0);
1081 1082 1083
}


1084 1085 1086 1087
/*
 * 'mxml_file_getc()' - Get a character from a file.
 */

Michael R Sweet's avatar
Michael R Sweet committed
1088 1089 1090
static int				/* O  - Character or EOF */
mxml_file_getc(void *p,			/* I  - Pointer to file */
               int  *encoding)		/* IO - Encoding */
1091
{
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
  int	ch,				/* Character from file */
	temp;				/* Temporary character */
  FILE	*fp;				/* Pointer to file */


 /*
  * Read a character from the file and see if it is EOF or ASCII...
  */

  fp = (FILE *)p;
  ch = getc(fp);

Michael R Sweet's avatar
Michael R Sweet committed
1104 1105
  if (ch == EOF)
    return (EOF);
1106

Michael R Sweet's avatar
Michael R Sweet committed
1107
  switch (*encoding)
1108
  {
Michael R Sweet's avatar
Michael R Sweet committed
1109 1110 1111 1112
    case ENCODE_UTF8 :
       /*
	* Got a UTF-8 character; convert UTF-8 to Unicode and return...
	*/
1113

Michael R Sweet's avatar
Michael R Sweet committed
1114
	if (!(ch & 0x80))
1115
	{
Michael R Sweet's avatar
Michael R Sweet committed
1116 1117 1118 1119 1120 1121 1122
	  if (mxml_bad_char(ch))
	  {
	    mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	       ch);
	    return (EOF);
	  }

1123 1124 1125 1126
#if DEBUG > 1
          printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */

Michael R Sweet's avatar
Michael R Sweet committed
1127
	  return (ch);
1128 1129
        }
	else if (ch == 0xfe)
Michael R Sweet's avatar
Michael R Sweet committed
1130 1131 1132 1133
	{
	 /*
	  * UTF-16 big-endian BOM?
	  */
1134

Michael R Sweet's avatar
Michael R Sweet committed
1135 1136 1137 1138 1139
          ch = getc(fp);
	  if (ch != 0xff)
	    return (EOF);

	  *encoding = ENCODE_UTF16BE;
1140

Michael R Sweet's avatar
Michael R Sweet committed
1141 1142 1143 1144 1145 1146 1147
	  return (mxml_file_getc(p, encoding));
	}
	else if (ch == 0xff)
	{
	 /*
	  * UTF-16 little-endian BOM?
	  */
1148

Michael R Sweet's avatar
Michael R Sweet committed
1149 1150 1151
          ch = getc(fp);
	  if (ch != 0xfe)
	    return (EOF);
1152

Michael R Sweet's avatar
Michael R Sweet committed
1153
	  *encoding = ENCODE_UTF16LE;
1154

Michael R Sweet's avatar
Michael R Sweet committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	  return (mxml_file_getc(p, encoding));
	}
	else if ((ch & 0xe0) == 0xc0)
	{
	 /*
	  * Two-byte value...
	  */

	  if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
1167 1168

	  if (ch < 0x80)
1169 1170
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1171
	    return (EOF);
1172
	  }
Michael R Sweet's avatar
Michael R Sweet committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	}
	else if ((ch & 0xf0) == 0xe0)
	{
	 /*
	  * Three-byte value...
	  */

	  if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = ((ch & 0x0f) << 6) | (temp & 0x3f);

	  if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = (ch << 6) | (temp & 0x3f);
1189 1190

	  if (ch < 0x800)
1191 1192 1193 1194 1195
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
	    return (EOF);
	  }

1196 1197 1198 1199 1200 1201
         /*
	  * Ignore (strip) Byte Order Mark (BOM)...
	  */

	  if (ch == 0xfeff)
	    return (mxml_file_getc(p, encoding));
Michael R Sweet's avatar
Michael R Sweet committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	}
	else if ((ch & 0xf8) == 0xf0)
	{
	 /*
	  * Four-byte value...
	  */

	  if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = ((ch & 0x07) << 6) | (temp & 0x3f);

	  if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = (ch << 6) | (temp & 0x3f);

	  if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
	    return (EOF);

	  ch = (ch << 6) | (temp & 0x3f);
1223 1224

	  if (ch < 0x10000)
1225 1226
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1227
	    return (EOF);
1228
	  }
Michael R Sweet's avatar
Michael R Sweet committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	}
	else
	  return (EOF);
	break;

    case ENCODE_UTF16BE :
       /*
        * Read UTF-16 big-endian char...
	*/

	ch = (ch << 8) | getc(fp);
1240

Michael R Sweet's avatar
Michael R Sweet committed
1241 1242 1243 1244 1245 1246 1247
	if (mxml_bad_char(ch))
	{
	  mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	     ch);
	  return (EOF);
	}
        else if (ch >= 0xd800 && ch <= 0xdbff)
Michael R Sweet's avatar
Michael R Sweet committed
1248 1249 1250 1251
	{
	 /*
	  * Multi-word UTF-16 char...
	  */
1252

1253 1254
          int lch = getc(fp);
          lch = (lch << 8) | getc(fp);
1255

1256
          if (lch < 0xdc00 || lch >= 0xdfff)
Michael R Sweet's avatar
Michael R Sweet committed
1257
	    return (EOF);
1258

Michael R Sweet's avatar
Michael R Sweet committed
1259 1260 1261
          ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
	}
	break;
1262

Michael R Sweet's avatar
Michael R Sweet committed
1263 1264 1265 1266
    case ENCODE_UTF16LE :
       /*
        * Read UTF-16 little-endian char...
	*/
1267

Michael R Sweet's avatar
Michael R Sweet committed
1268 1269
	ch |= (getc(fp) << 8);

Michael R Sweet's avatar
Michael R Sweet committed
1270 1271 1272 1273 1274 1275 1276
        if (mxml_bad_char(ch))
	{
	  mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	     ch);
	  return (EOF);
	}
        else if (ch >= 0xd800 && ch <= 0xdbff)
Michael R Sweet's avatar
Michael R Sweet committed
1277 1278 1279 1280 1281
	{
	 /*
	  * Multi-word UTF-16 char...
	  */

1282 1283
          int lch = getc(fp);
          lch |= (getc(fp) << 8);
Michael R Sweet's avatar
Michael R Sweet committed
1284

1285
          if (lch < 0xdc00 || lch >= 0xdfff)
Michael R Sweet's avatar
Michael R Sweet committed
1286 1287 1288 1289 1290
	    return (EOF);

          ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
	}
	break;
1291 1292
  }

1293 1294 1295 1296
#if DEBUG > 1
  printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */

1297
  return (ch);
1298 1299 1300
}


1301 1302 1303 1304 1305 1306 1307 1308
/*
 * 'mxml_file_putc()' - Write a character to a file.
 */

static int				/* O - 0 on success, -1 on failure */
mxml_file_putc(int  ch,			/* I - Character to write */
               void *p)			/* I - Pointer to file */
{
1309
  return (putc(ch, (FILE *)p) == EOF ? -1 : 0);
1310 1311 1312
}


1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
/*
 * 'mxml_get_entity()' - Get the character corresponding to an entity...
 */

static int				/* O  - Character value or EOF on error */
mxml_get_entity(mxml_node_t *parent,	/* I  - Parent node */
		void        *p,		/* I  - Pointer to source */
		int         *encoding,	/* IO - Character encoding */
                int         (*getc_cb)(void *, int *))
					/* I  - Get character function */
{
  int	ch;				/* Current character */
  char	entity[64],			/* Entity string */
	*entptr;			/* Pointer into entity */


  entptr = entity;

  while ((ch = (*getc_cb)(p, encoding)) != EOF)
    if (ch > 126 || (!isalnum(ch) && ch != '#'))
      break;
    else if (entptr < (entity + sizeof(entity) - 1))
      *entptr++ = ch;
    else
    {
      mxml_error("Entity name too long under parent <%s>!",
	         parent ? parent->value.element.name : "null");
      break;
    }

  *entptr = '\0';

  if (ch != ';')
  {
    mxml_error("Character entity \"%s\" not terminated under parent <%s>!",
	       entity, parent ? parent->value.element.name : "null");
    return (EOF);
  }

1352
  if (entity[0] == '#')
1353
  {
1354
    if (entity[1] == 'x')
1355
      ch = (int)strtol(entity + 2, NULL, 16);
1356
    else
1357
      ch = (int)strtol(entity + 1, NULL, 10);
1358 1359 1360 1361 1362
  }
  else if ((ch = mxmlEntityGetValue(entity)) < 0)
    mxml_error("Entity name \"%s;\" not supported under parent <%s>!",
	       entity, parent ? parent->value.element.name : "null");

Michael R Sweet's avatar
Michael R Sweet committed
1363 1364 1365 1366 1367 1368 1369
  if (mxml_bad_char(ch))
  {
    mxml_error("Bad control character 0x%02x under parent <%s> not allowed by XML standard!",
               ch, parent ? parent->value.element.name : "null");
    return (EOF);
  }

1370 1371 1372 1373
  return (ch);
}


1374 1375 1376 1377 1378
/*
 * 'mxml_load_data()' - Load data into an XML node tree.
 */

static mxml_node_t *			/* O - First node or NULL if the file could not be read. */
1379 1380 1381 1382 1383 1384 1385
mxml_load_data(
    mxml_node_t     *top,		/* I - Top node */
    void            *p,			/* I - Pointer to data */
    mxml_load_cb_t  cb,			/* I - Callback function or MXML_NO_CALLBACK */
    _mxml_getc_cb_t getc_cb,		/* I - Read function */
    mxml_sax_cb_t   sax_cb,		/* I - SAX callback or MXML_NO_CALLBACK */
    void            *sax_data)		/* I - SAX user data */
Michael R Sweet's avatar
Michael R Sweet committed
1386 1387
{
  mxml_node_t	*node,			/* Current node */
1388
		*first,			/* First node added */
Michael R Sweet's avatar
Michael R Sweet committed
1389 1390 1391
		*parent;		/* Current parent node */
  int		ch,			/* Character from file */
		whitespace;		/* Non-zero if whitespace seen */
1392
  char		*buffer,		/* String buffer */
Michael R Sweet's avatar
Michael R Sweet committed
1393
		*bufptr;		/* Pointer into buffer */
1394
  int		bufsize;		/* Size of buffer */
Michael R Sweet's avatar
Michael R Sweet committed
1395
  mxml_type_t	type;			/* Current node type */
Michael R Sweet's avatar
Michael R Sweet committed
1396
  int		encoding;		/* Character encoding */
1397 1398
  _mxml_global_t *global = _mxml_global();
					/* Global data */
1399 1400 1401 1402 1403 1404 1405 1406 1407
  static const char * const types[] =	/* Type strings... */
		{
		  "MXML_ELEMENT",	/* XML element with attributes */
		  "MXML_INTEGER",	/* Integer value */
		  "MXML_OPAQUE",	/* Opaque string */
		  "MXML_REAL",		/* Real value */
		  "MXML_TEXT",		/* Text fragment */
		  "MXML_CUSTOM"		/* Custom data */
		};
Michael R Sweet's avatar
Michael R Sweet committed
1408 1409 1410 1411 1412 1413


 /*
  * Read elements and other nodes from the file...
  */

1414 1415
  if ((buffer = malloc(64)) == NULL)
  {
Michael R Sweet's avatar
Michael R Sweet committed
1416
    mxml_error("Unable to allocate string buffer!");
1417 1418 1419 1420
    return (NULL);
  }

  bufsize    = 64;
Michael R Sweet's avatar
Michael R Sweet committed
1421 1422
  bufptr     = buffer;
  parent     = top;
1423
  first      = NULL;
Michael R Sweet's avatar
Michael R Sweet committed
1424
  whitespace = 0;
Michael R Sweet's avatar
Michael R Sweet committed
1425
  encoding   = ENCODE_UTF8;
Michael R Sweet's avatar
Michael R Sweet committed
1426 1427 1428

  if (cb && parent)
    type = (*cb)(parent);
1429
  else if (parent)
Michael R Sweet's avatar
Michael R Sweet committed
1430
    type = MXML_TEXT;
1431 1432
  else
    type = MXML_IGNORE;
Michael R Sweet's avatar
Michael R Sweet committed
1433

Michael R Sweet's avatar
Michael R Sweet committed
1434
  while ((ch = (*getc_cb)(p, &encoding)) != EOF)
Michael R Sweet's avatar
Michael R Sweet committed
1435
  {
1436
    if ((ch == '<' ||
1437
         (mxml_isspace(ch) && type != MXML_OPAQUE && type != MXML_CUSTOM)) &&
1438
        bufptr > buffer)
Michael R Sweet's avatar
Michael R Sweet committed
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    {
     /*
      * Add a new value node...
      */

      *bufptr = '\0';

      switch (type)
      {
	case MXML_INTEGER :
1449
            node = mxmlNewInteger(parent, (int)strtol(buffer, &bufptr, 0));
Michael R Sweet's avatar
Michael R Sweet committed
1450 1451 1452 1453 1454 1455 1456
	    break;

	case MXML_OPAQUE :
            node = mxmlNewOpaque(parent, buffer);
	    break;

	case MXML_REAL :
1457
            node = mxmlNewReal(parent, strtod(buffer, &bufptr));
Michael R Sweet's avatar
Michael R Sweet committed
1458 1459 1460 1461 1462 1463
	    break;

	case MXML_TEXT :
            node = mxmlNewText(parent, whitespace, buffer);
	    break;

1464
	case MXML_CUSTOM :
1465
	    if (global->custom_load_cb)
1466 1467 1468 1469 1470 1471 1472
	    {
	     /*
	      * Use the callback to fill in the custom data...
	      */

              node = mxmlNewCustom(parent, NULL, NULL);

1473
	      if ((*global->custom_load_cb)(node, buffer))
1474 1475 1476 1477 1478 1479 1480 1481 1482
	      {
	        mxml_error("Bad custom value '%s' in parent <%s>!",
		           buffer, parent ? parent->value.element.name : "null");
		mxmlDelete(node);
		node = NULL;
	      }
	      break;
	    }

1483
        default : /* Ignore... */
Michael R Sweet's avatar
Michael R Sweet committed
1484 1485
	    node = NULL;
	    break;
1486
      }
Michael R Sweet's avatar
Michael R Sweet committed
1487

1488 1489 1490 1491 1492 1493
      if (*bufptr)
      {
       /*
        * Bad integer/real number value...
	*/

Michael R Sweet's avatar
Michael R Sweet committed
1494 1495 1496
        mxml_error("Bad %s value '%s' in parent <%s>!",
	           type == MXML_INTEGER ? "integer" : "real", buffer,
		   parent ? parent->value.element.name : "null");
1497 1498 1499 1500
	break;
      }

      bufptr     = buffer;
1501
      whitespace = mxml_isspace(ch) && type == MXML_TEXT;
Michael R Sweet's avatar
Michael R Sweet committed
1502

1503
      if (!node && type != MXML_IGNORE)
Michael R Sweet's avatar
Michael R Sweet committed
1504 1505
      {
       /*
1506
	* Print error and return...
Michael R Sweet's avatar
Michael R Sweet committed
1507 1508
	*/

1509 1510 1511
	mxml_error("Unable to add value node of type %s to parent <%s>!",
	           types[type], parent ? parent->value.element.name : "null");
	goto error;
Michael R Sweet's avatar
Michael R Sweet committed
1512
      }
1513

1514 1515 1516 1517 1518 1519 1520 1521
      if (sax_cb)
      {
        (*sax_cb)(node, MXML_SAX_DATA, sax_data);

        if (!mxmlRelease(node))
          node = NULL;
      }

1522
      if (!first && node)
1523
        first = node;
Michael R Sweet's avatar
Michael R Sweet committed
1524
    }
1525
    else if (mxml_isspace(ch) && type == MXML_TEXT)
1526 1527 1528
      whitespace = 1;

   /*
1529 1530
    * Add lone whitespace node if we have an element and existing
    * whitespace...
1531 1532 1533 1534
    */

    if (ch == '<' && whitespace && type == MXML_TEXT)
    {
1535
      if (parent)
1536
      {
1537
	node = mxmlNewText(parent, whitespace, "");
1538

1539 1540 1541
	if (sax_cb)
	{
	  (*sax_cb)(node, MXML_SAX_DATA, sax_data);
1542

1543 1544 1545 1546 1547 1548 1549
	  if (!mxmlRelease(node))
	    node = NULL;
	}

	if (!first && node)
	  first = node;
      }
1550

1551
      whitespace = 0;
1552
    }
Michael R Sweet's avatar
Michael R Sweet committed
1553 1554 1555 1556 1557 1558 1559 1560 1561

    if (ch == '<')
    {
     /*
      * Start of open/close tag...
      */

      bufptr = buffer;

Michael R Sweet's avatar
Michael R Sweet committed
1562
      while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1563
        if (mxml_isspace(ch) || ch == '>' || (ch == '/' && bufptr > buffer))
Michael R Sweet's avatar
Michael R Sweet committed
1564
	  break;
1565 1566 1567 1568 1569
	else if (ch == '<')
	{
	  mxml_error("Bare < in element!");
	  goto error;
	}
1570
	else if (ch == '&')
1571
	{
Michael R Sweet's avatar
Michael R Sweet committed
1572
	  if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb)) == EOF)
1573 1574 1575 1576
	    goto error;

	  if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
	    goto error;
1577
	}
1578 1579
	else if (ch < '0' && ch != '!' && ch != '-' && ch != '.' && ch != '/')
	  goto error;
1580 1581
	else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
	  goto error;
1582 1583 1584
	else if (((bufptr - buffer) == 1 && buffer[0] == '?') ||
	         ((bufptr - buffer) == 3 && !strncmp(buffer, "!--", 3)) ||
	         ((bufptr - buffer) == 8 && !strncmp(buffer, "![CDATA[", 8)))
1585
	  break;
1586

Michael R Sweet's avatar
Michael R Sweet committed
1587 1588
      *bufptr = '\0';

Michael R Sweet's avatar
Michael R Sweet committed
1589 1590 1591
      if (!strcmp(buffer, "!--"))
      {
       /*
1592
        * Gather rest of comment...
Michael R Sweet's avatar
Michael R Sweet committed
1593 1594
	*/

Michael R Sweet's avatar
Michael R Sweet committed
1595
	while ((ch = (*getc_cb)(p, &encoding)) != EOF)
Michael R Sweet's avatar
Michael R Sweet committed
1596
	{
1597
	  if (ch == '>' && bufptr > (buffer + 4) &&
1598
	      bufptr[-3] != '-' && bufptr[-2] == '-' && bufptr[-1] == '-')
1599
	    break;
1600 1601 1602
	  else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
	    goto error;
	}
1603

1604 1605 1606 1607 1608
       /*
        * Error out if we didn't get the whole comment...
	*/

        if (ch != '>')
1609 1610 1611 1612 1613 1614 1615 1616 1617
	{
	 /*
	  * Print error and return...
	  */

	  mxml_error("Early EOF in comment node!");
	  goto error;
	}

1618 1619 1620 1621 1622 1623 1624

       /*
        * Otherwise add this as an element under the current parent...
	*/

	*bufptr = '\0';

1625 1626 1627 1628 1629 1630
        if (!parent && first)
	{
	 /*
	  * There can only be one root element!
	  */

1631
	  mxml_error("<%s> cannot be a second root node after <%s>",
1632
	             buffer, first->value.element.name);