mxml-file.c 64.9 KB
Newer Older
Michael R Sweet's avatar
Michael R Sweet committed
1
/*
2
 * "$Id$"
Michael R Sweet's avatar
Michael R Sweet committed
3
 *
Michael R Sweet's avatar
Michael R Sweet committed
4
 * File loading code for Mini-XML, a small XML-like file parsing library.
Michael R Sweet's avatar
Michael R Sweet committed
5
 *
6
 * Copyright 2003-2009 by Michael Sweet.
Michael R Sweet's avatar
Michael R Sweet committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Contents:
 *
20 21 22 23 24 25 26
 *   mxmlLoadFd()            - Load a file descriptor into an XML node tree.
 *   mxmlLoadFile()          - Load a file into an XML node tree.
 *   mxmlLoadString()        - Load a string into an XML node tree.
 *   mxmlSaveAllocString()   - Save an XML node tree to an allocated string.
 *   mxmlSaveFd()            - Save an XML tree to a file descriptor.
 *   mxmlSaveFile()          - Save an XML tree to a file.
 *   mxmlSaveString()        - Save an XML node tree to a string.
27 28 29 30 31 32
 *   mxmlSAXLoadFd()         - Load a file descriptor into an XML node tree
 *                             using a SAX callback.
 *   mxmlSAXLoadFile()       - Load a file into an XML node tree
 *                             using a SAX callback.
 *   mxmlSAXLoadString()     - Load a string into an XML node tree
 *                             using a SAX callback.
33 34
 *   mxmlSetCustomHandlers() - Set the handling functions for custom data.
 *   mxmlSetErrorCallback()  - Set the error message callback.
35
 *   mxmlSetWrapMargin()     - Set the the wrap margin when saving XML data.
36 37 38 39 40 41 42 43 44 45 46
 *   mxml_add_char()         - Add a character to a buffer, expanding as needed.
 *   mxml_fd_getc()          - Read a character from a file descriptor.
 *   mxml_fd_putc()          - Write a character to a file descriptor.
 *   mxml_fd_read()          - Read a buffer of data from a file descriptor.
 *   mxml_fd_write()         - Write a buffer of data to a file descriptor.
 *   mxml_file_getc()        - Get a character from a file.
 *   mxml_file_putc()        - Write a character to a file.
 *   mxml_get_entity()       - Get the character corresponding to an entity...
 *   mxml_load_data()        - Load data into an XML node tree.
 *   mxml_parse_element()    - Parse an element for any attributes...
 *   mxml_string_getc()      - Get a character from a string.
47
 *   mxml_string_putc()      - Write a character to a string.
48 49 50 51
 *   mxml_write_name()       - Write a name string.
 *   mxml_write_node()       - Save an XML node to a file.
 *   mxml_write_string()     - Write a string, escaping & and < as needed.
 *   mxml_write_ws()         - Do whitespace callback...
Michael R Sweet's avatar
Michael R Sweet committed
52 53 54 55 56 57
 */

/*
 * Include necessary headers...
 */

Michael R Sweet's avatar
Michael R Sweet committed
58
#ifndef WIN32
59
#  include <unistd.h>
Michael R Sweet's avatar
Michael R Sweet committed
60 61
#endif /* !WIN32 */
#include "mxml-private.h"
Michael R Sweet's avatar
Michael R Sweet committed
62 63


Michael R Sweet's avatar
Michael R Sweet committed
64 65 66 67 68 69 70 71 72
/*
 * 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
73 74 75 76 77 78 79
/*
 * Macro to test for a bad XML character...
 */

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


80
/*
81
 * Types and structures...
82 83
 */

84 85 86 87
typedef int (*_mxml_getc_cb_t)(void *, int *);
typedef int (*_mxml_putc_cb_t)(int, void *);

typedef struct _mxml_fdbuf_s		/**** File descriptor buffer ****/
88 89 90 91 92
{
  int		fd;			/* File descriptor */
  unsigned char	*current,		/* Current position in buffer */
		*end,			/* End of buffer */
		buffer[8192];		/* Character buffer */
93
} _mxml_fdbuf_t;
94 95


Michael R Sweet's avatar
Michael R Sweet committed
96 97 98 99
/*
 * Local functions...
 */

100 101
static int		mxml_add_char(int ch, char **ptr, char **buffer,
			              int *bufsize);
102 103
static int		mxml_fd_getc(void *p, int *encoding);
static int		mxml_fd_putc(int ch, void *p);
104 105
static int		mxml_fd_read(_mxml_fdbuf_t *buf);
static int		mxml_fd_write(_mxml_fdbuf_t *buf);
106 107
static int		mxml_file_getc(void *p, int *encoding);
static int		mxml_file_putc(int ch, void *p);
108
static int		mxml_get_entity(mxml_node_t *parent, void *p,
Michael R Sweet's avatar
Michael R Sweet committed
109
			                int *encoding,
110
					_mxml_getc_cb_t getc_cb);
111 112 113
static inline int	mxml_isspace(int ch)
			{
			  return (ch == ' ' || ch == '\t' || ch == '\r' ||
114
			          ch == '\n');
115
			}
116
static mxml_node_t	*mxml_load_data(mxml_node_t *top, void *p,
117 118 119
			                mxml_load_cb_t cb,
			                _mxml_getc_cb_t getc_cb,
                                        mxml_sax_cb_t sax_cb, void *sax_data);
120
static int		mxml_parse_element(mxml_node_t *node, void *p,
Michael R Sweet's avatar
Michael R Sweet committed
121
			                   int *encoding,
122
					   _mxml_getc_cb_t getc_cb);
Michael R Sweet's avatar
Michael R Sweet committed
123
static int		mxml_string_getc(void *p, int *encoding);
124
static int		mxml_string_putc(int ch, void *p);
125
static int		mxml_write_name(const char *s, void *p,
126
					_mxml_putc_cb_t putc_cb);
127
static int		mxml_write_node(mxml_node_t *node, void *p,
128
			                mxml_save_cb_t cb, int col,
129 130
					_mxml_putc_cb_t putc_cb,
					_mxml_global_t *global);
131
static int		mxml_write_string(const char *s, void *p,
132
					  _mxml_putc_cb_t putc_cb);
133
static int		mxml_write_ws(mxml_node_t *node, void *p, 
134 135
			              mxml_save_cb_t cb, int ws,
				      int col, _mxml_putc_cb_t putc_cb);
136 137


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/*
 * '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.
 * If MXML_NO_CALLBACK is specified then all child nodes will be either
 * MXML_ELEMENT or MXML_TEXT nodes.
 *
 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
 * child nodes of the specified type.
 */

mxml_node_t *				/* O - First node or NULL if the file could not be read. */
154 155 156
mxmlLoadFd(mxml_node_t    *top,		/* I - Top node */
           int            fd,		/* I - File descriptor to read from */
           mxml_load_cb_t cb)		/* I - Callback function or MXML_NO_CALLBACK */
157
{
158
  _mxml_fdbuf_t	buf;			/* File descriptor buffer */
159 160 161 162 163 164 165 166 167 168 169 170 171 172


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

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

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

173
  return (mxml_load_data(top, &buf, cb, mxml_fd_getc, MXML_NO_CALLBACK, NULL));
174 175 176
}


177
/*
178
 * 'mxmlLoadFile()' - Load a file into an XML node tree.
179
 *
180 181 182
 * 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
183 184 185
 * function returns the value type that should be used for child nodes.
 * If MXML_NO_CALLBACK is specified then all child nodes will be either
 * MXML_ELEMENT or MXML_TEXT nodes.
186 187 188 189
 *
 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
 * child nodes of the specified type.
190 191
 */

192
mxml_node_t *				/* O - First node or NULL if the file could not be read. */
193 194 195
mxmlLoadFile(mxml_node_t    *top,	/* I - Top node */
             FILE           *fp,	/* I - File to read from */
             mxml_load_cb_t cb)		/* I - Callback function or MXML_NO_CALLBACK */
196
{
197 198 199 200
 /*
  * Read the XML data...
  */

201
  return (mxml_load_data(top, fp, cb, mxml_file_getc, MXML_NO_CALLBACK, NULL));
202
}
Michael R Sweet's avatar
Michael R Sweet committed
203 204 205


/*
206
 * 'mxmlLoadString()' - Load a string into an XML node tree.
207
 *
208 209 210
 * 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
211 212 213
 * function returns the value type that should be used for child nodes.
 * If MXML_NO_CALLBACK is specified then all child nodes will be either
 * MXML_ELEMENT or MXML_TEXT nodes.
214 215 216 217
 *
 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
 * child nodes of the specified type.
Michael R Sweet's avatar
Michael R Sweet committed
218 219
 */

220
mxml_node_t *				/* O - First node or NULL if the string has errors. */
221 222 223
mxmlLoadString(mxml_node_t    *top,	/* I - Top node */
               const char     *s,	/* I - String to load */
               mxml_load_cb_t cb)	/* I - Callback function or MXML_NO_CALLBACK */
224
{
225 226 227 228
 /*
  * Read the XML data...
  */

Michael R Sweet's avatar
Michael R Sweet committed
229
  return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, MXML_NO_CALLBACK,
230
                         NULL));
231 232 233
}


234 235 236 237 238 239 240 241
/*
 * 'mxmlSaveAllocString()' - Save an XML node tree to an allocated string.
 *
 * This function returns a pointer to a string containing the textual
 * representation of the XML node tree.  The string should be freed
 * using the free() function when you are done with it.  NULL is returned
 * if the node would produce an empty string or if the string cannot be
 * allocated.
242 243 244 245 246 247
 *
 * The callback argument specifies a function that returns a whitespace
 * string or NULL before and after each element. If MXML_NO_CALLBACK
 * is specified, whitespace will only be added before MXML_TEXT nodes
 * with leading whitespace and before attribute names inside opening
 * element tags.
248 249 250
 */

char *					/* O - Allocated string or NULL */
251 252 253
mxmlSaveAllocString(
    mxml_node_t    *node,		/* I - Node to write */
    mxml_save_cb_t cb)			/* I - Whitespace callback or MXML_NO_CALLBACK */
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
{
  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);
}


297 298 299 300 301 302 303 304 305 306 307
/*
 * 'mxmlSaveFd()' - Save an XML tree to a file descriptor.
 *
 * The callback argument specifies a function that returns a whitespace
 * string or NULL before and after each element. If MXML_NO_CALLBACK
 * is specified, whitespace will only be added before MXML_TEXT nodes
 * with leading whitespace and before attribute names inside opening
 * element tags.
 */

int					/* O - 0 on success, -1 on error. */
308 309 310
mxmlSaveFd(mxml_node_t    *node,	/* I - Node to write */
           int            fd,		/* I - File descriptor to write to */
	   mxml_save_cb_t cb)		/* I - Whitespace callback or MXML_NO_CALLBACK */
311 312
{
  int		col;			/* Final column */
313
  _mxml_fdbuf_t	buf;			/* File descriptor buffer */
314 315
  _mxml_global_t *global = _mxml_global();
					/* Global data */
316 317 318 319 320 321 322 323


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

  buf.fd      = fd;
  buf.current = buf.buffer;
324
  buf.end     = buf.buffer + sizeof(buf.buffer);
325 326 327 328 329

 /*
  * Write the node...
  */

330
  if ((col = mxml_write_node(node, &buf, cb, 0, mxml_fd_putc, global)) < 0)
331 332 333 334 335 336 337 338 339 340 341 342 343 344
    return (-1);

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

 /*
  * Flush and return...
  */

  return (mxml_fd_write(&buf));
}


345 346 347 348
/*
 * 'mxmlSaveFile()' - Save an XML tree to a file.
 *
 * The callback argument specifies a function that returns a whitespace
349
 * string or NULL before and after each element. If MXML_NO_CALLBACK
350 351 352 353 354 355
 * is specified, whitespace will only be added before MXML_TEXT nodes
 * with leading whitespace and before attribute names inside opening
 * element tags.
 */

int					/* O - 0 on success, -1 on error. */
356 357 358
mxmlSaveFile(mxml_node_t    *node,	/* I - Node to write */
             FILE           *fp,	/* I - File to write to */
	     mxml_save_cb_t cb)		/* I - Whitespace callback or MXML_NO_CALLBACK */
359 360
{
  int	col;				/* Final column */
361 362
  _mxml_global_t *global = _mxml_global();
					/* Global data */
363 364 365 366 367 368


 /*
  * Write the node...
  */

369
  if ((col = mxml_write_node(node, fp, cb, 0, mxml_file_putc, global)) < 0)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    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.
390 391 392 393 394 395
 *
 * The callback argument specifies a function that returns a whitespace
 * string or NULL before and after each element. If MXML_NO_CALLBACK
 * is specified, whitespace will only be added before MXML_TEXT nodes
 * with leading whitespace and before attribute names inside opening
 * element tags.
396 397 398
 */

int					/* O - Size of string */
399 400 401 402
mxmlSaveString(mxml_node_t    *node,	/* I - Node to write */
               char           *buffer,	/* I - String buffer */
               int            bufsize,	/* I - Size of string buffer */
               mxml_save_cb_t cb)	/* I - Whitespace callback or MXML_NO_CALLBACK */
403
{
404 405
  int	col;				/* Final column */
  char	*ptr[2];			/* Pointers for putc_cb */
406 407
  _mxml_global_t *global = _mxml_global();
					/* Global data */
408 409 410 411 412 413 414 415 416


 /*
  * Write the node...
  */

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

417
  if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc, global)) < 0)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    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...
  */

436
  return (ptr[0] - buffer);
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
/*
 * '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.
 * If MXML_NO_CALLBACK is specified then all child nodes will be either
 * MXML_ELEMENT or MXML_TEXT nodes.
 *
 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
 * child nodes of the specified type.
 *
 * The SAX callback must call mxmlRetain() for any nodes that need to
 * 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@
 */

mxml_node_t *				/* O - First node or NULL if the file could not be read. */
mxmlSAXLoadFd(mxml_node_t    *top,	/* I - Top node */
              int            fd,	/* I - File descriptor to read from */
              mxml_load_cb_t cb,	/* I - Callback function or MXML_NO_CALLBACK */
              mxml_sax_cb_t  sax_cb,	/* I - SAX callback or MXML_NO_CALLBACK */
              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.
 * If MXML_NO_CALLBACK is specified then all child nodes will be either
 * MXML_ELEMENT or MXML_TEXT nodes.
 *
 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
 * child nodes of the specified type.
 *
 * The SAX callback must call mxmlRetain() for any nodes that need to
 * 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@
 */

mxml_node_t *				/* O - First node or NULL if the file could not be read. */
mxmlSAXLoadFile(
    mxml_node_t    *top,		/* I - Top node */
    FILE           *fp,			/* I - File to read from */
    mxml_load_cb_t cb,			/* I - Callback function or MXML_NO_CALLBACK */
    mxml_sax_cb_t  sax_cb,		/* I - SAX callback or MXML_NO_CALLBACK */
    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.
 * If MXML_NO_CALLBACK is specified then all child nodes will be either
 * MXML_ELEMENT or MXML_TEXT nodes.
 *
 * The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK,
 * MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading
 * child nodes of the specified type.
 *
 * The SAX callback must call mxmlRetain() for any nodes that need to
 * 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@
 */

mxml_node_t *				/* O - First node or NULL if the string has errors. */
mxmlSAXLoadString(
    mxml_node_t    *top,		/* I - Top node */
    const char     *s,			/* I - String to load */
    mxml_load_cb_t cb,			/* I - Callback function or MXML_NO_CALLBACK */
    mxml_sax_cb_t  sax_cb,		/* I - SAX callback or MXML_NO_CALLBACK */
    void           *sax_data)		/* I - SAX user data */
{
 /*
  * Read the XML data...
  */

Michael R Sweet's avatar
Michael R Sweet committed
560
  return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, sax_cb, sax_data));
561 562 563
}


564 565 566 567 568 569 570 571 572 573 574 575
/*
 * '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
 * string on success and NULL on error.
 * 
 */

void
576 577 578
mxmlSetCustomHandlers(
    mxml_custom_load_cb_t load,		/* I - Load function */
    mxml_custom_save_cb_t save)		/* I - Save function */
579
{
580 581 582 583 584 585
  _mxml_global_t *global = _mxml_global();
					/* Global data */


  global->custom_load_cb = load;
  global->custom_save_cb = save;
586 587 588
}


Michael R Sweet's avatar
Michael R Sweet committed
589 590 591 592 593
/*
 * 'mxmlSetErrorCallback()' - Set the error message callback.
 */

void
594
mxmlSetErrorCallback(mxml_error_cb_t cb)/* I - Error callback function */
Michael R Sweet's avatar
Michael R Sweet committed
595
{
596 597 598 599 600
  _mxml_global_t *global = _mxml_global();
					/* Global data */


  global->error_cb = cb;
Michael R Sweet's avatar
Michael R Sweet committed
601 602 603
}


604 605 606
/*
 * 'mxmlSetWrapMargin()' - Set the the wrap margin when saving XML data.
 *
607
 * Wrapping is disabled when "column" is 0.
608 609
 *
 * @since Mini-XML 2.3@
610 611 612
 */

void
613
mxmlSetWrapMargin(int column)		/* I - Column for wrapping, 0 to disable wrapping */
614
{
615 616 617 618
  _mxml_global_t *global = _mxml_global();
					/* Global data */


619
  global->wrap = column;
620 621 622
}


623 624 625 626 627 628 629 630 631 632 633 634 635
/*
 * '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 */


636
  if (*bufptr >= (*buffer + *bufsize - 4))
637 638 639 640 641 642 643 644 645 646 647 648 649 650
  {
   /*
    * 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
651
      mxml_error("Unable to expand string buffer to %d bytes!", *bufsize);
652 653 654 655 656

      return (-1);
    }

    *bufptr = newbuffer + (*bufptr - *buffer);
657
    *buffer = newbuffer;
658 659
  }

660
  if (ch < 0x80)
661 662 663 664 665 666 667
  {
   /*
    * Single byte ASCII...
    */

    *(*bufptr)++ = ch;
  }
668
  else if (ch < 0x800)
669 670 671 672 673 674 675 676
  {
   /*
    * Two-byte UTF-8...
    */

    *(*bufptr)++ = 0xc0 | (ch >> 6);
    *(*bufptr)++ = 0x80 | (ch & 0x3f);
  }
677
  else if (ch < 0x10000)
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  {
   /*
    * 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);
  }
698 699 700 701 702

  return (0);
}


703
/*
704
 * 'mxml_fd_getc()' - Read a character from a file descriptor.
705 706
 */

707 708 709
static int				/* O  - Character or EOF */
mxml_fd_getc(void *p,			/* I  - File descriptor buffer */
             int  *encoding)		/* IO - Encoding */
710
{
711
  _mxml_fdbuf_t	*buf;			/* File descriptor buffer */
712 713
  int		ch,			/* Current character */
		temp;			/* Temporary character */
714 715


716 717 718
 /*
  * Grab the next character in the buffer...
  */
719

720
  buf = (_mxml_fdbuf_t *)p;
721

722 723 724
  if (buf->current >= buf->end)
    if (mxml_fd_read(buf) < 0)
      return (EOF);
725

726 727 728
  ch = *(buf->current)++;

  switch (*encoding)
729
  {
730 731 732 733 734 735
    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
736 737 738 739 740 741 742 743 744 745 746 747
	{
#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);
	  }

748
	  return (ch);
Michael R Sweet's avatar
Michael R Sweet committed
749 750
        }
	else if (ch == 0xfe)
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 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 799 800 801 802 803
	{
	 /*
	  * UTF-16 big-endian BOM?
	  */

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

	  ch = *(buf->current)++;
          
	  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)++;
          
	  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);
804 805

	  if (ch < 0x80)
806 807
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
808
	    return (EOF);
809
	  }
810 811 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
	}
	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);
838 839

	  if (ch < 0x800)
840 841 842 843 844 845 846 847
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
	    return (EOF);
	  }

	  if (ch < 0xfeff)
	  {
	    mxml_error("Invalid BOM in UTF-8 XML!");
848
	    return (EOF);
849
	  }
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	}
	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);
889 890

	  if (ch < 0x10000)
891 892
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
893
	    return (EOF);
894
	  }
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
	}
	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
913 914 915 916 917 918 919
	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)
920 921 922 923 924 925 926 927 928 929 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 957 958 959 960
	{
	 /*
	  * 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
961 962 963 964 965 966 967
        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)
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
	{
	 /*
	  * 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;
995 996
  }

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

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
  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 */
{
1013
  _mxml_fdbuf_t	*buf;			/* File descriptor buffer */
1014 1015 1016


 /*
1017
  * Flush the write buffer as needed...
1018 1019
  */

1020
  buf = (_mxml_fdbuf_t *)p;
1021 1022 1023 1024 1025

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

1026
  *(buf->current)++ = ch;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

 /*
  * 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 */
1041
mxml_fd_read(_mxml_fdbuf_t *buf)		/* I - File descriptor buffer */
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
{
  int	bytes;				/* Bytes read... */


 /*
  * Range check input...
  */

  if (!buf)
    return (-1);

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

  while ((bytes = read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)
1058
#ifdef EINTR
1059
    if (errno != EAGAIN && errno != EINTR)
1060 1061 1062
#else
    if (errno != EAGAIN)
#endif /* EINTR */
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
      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 */
1084
mxml_fd_write(_mxml_fdbuf_t *buf)	/* I - File descriptor buffer */
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
{
  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)
    if ((bytes = write(buf->fd, ptr, buf->current - ptr)) < 0)
      return (-1);

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

  buf->current = buf->buffer;

  return (0);
1119 1120 1121
}


1122 1123 1124 1125
/*
 * 'mxml_file_getc()' - Get a character from a file.
 */

Michael R Sweet's avatar
Michael R Sweet committed
1126 1127 1128
static int				/* O  - Character or EOF */
mxml_file_getc(void *p,			/* I  - Pointer to file */
               int  *encoding)		/* IO - Encoding */
1129
{
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  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
1142 1143
  if (ch == EOF)
    return (EOF);
1144

Michael R Sweet's avatar
Michael R Sweet committed
1145
  switch (*encoding)
1146
  {
Michael R Sweet's avatar
Michael R Sweet committed
1147 1148 1149 1150
    case ENCODE_UTF8 :
       /*
	* Got a UTF-8 character; convert UTF-8 to Unicode and return...
	*/
1151

Michael R Sweet's avatar
Michael R Sweet committed
1152
	if (!(ch & 0x80))
1153
	{
Michael R Sweet's avatar
Michael R Sweet committed
1154 1155 1156 1157 1158 1159 1160
	  if (mxml_bad_char(ch))
	  {
	    mxml_error("Bad control character 0x%02x not allowed by XML standard!",
        	       ch);
	    return (EOF);
	  }

1161 1162 1163 1164
#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
1165
	  return (ch);
1166 1167
        }
	else if (ch == 0xfe)
Michael R Sweet's avatar
Michael R Sweet committed
1168 1169 1170 1171
	{
	 /*
	  * UTF-16 big-endian BOM?
	  */
1172

Michael R Sweet's avatar
Michael R Sweet committed
1173 1174 1175 1176 1177
          ch = getc(fp);
	  if (ch != 0xff)
	    return (EOF);

	  *encoding = ENCODE_UTF16BE;
1178

Michael R Sweet's avatar
Michael R Sweet committed
1179 1180 1181 1182 1183 1184 1185
	  return (mxml_file_getc(p, encoding));
	}
	else if (ch == 0xff)
	{
	 /*
	  * UTF-16 little-endian BOM?
	  */
1186

Michael R Sweet's avatar
Michael R Sweet committed
1187 1188 1189
          ch = getc(fp);
	  if (ch != 0xfe)
	    return (EOF);
1190

Michael R Sweet's avatar
Michael R Sweet committed
1191
	  *encoding = ENCODE_UTF16LE;
1192

Michael R Sweet's avatar
Michael R Sweet committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	  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);
1205 1206

	  if (ch < 0x80)
1207 1208
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1209
	    return (EOF);
1210
	  }
Michael R Sweet's avatar
Michael R Sweet committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	}
	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);
1227 1228

	  if (ch < 0x800)
1229 1230 1231 1232 1233 1234 1235 1236
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
	    return (EOF);
	  }

	  if (ch < 0xfeff)
	  {
	    mxml_error("Invalid BOM in UTF-8 XML!");
1237
	    return (EOF);
1238
	  }
Michael R Sweet's avatar
Michael R Sweet committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	}
	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);
1260 1261

	  if (ch < 0x10000)
1262 1263
	  {
	    mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
1264
	    return (EOF);
1265
	  }
Michael R Sweet's avatar
Michael R Sweet committed
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
	}
	else
	  return (EOF);
	break;

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

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

Michael R Sweet's avatar
Michael R Sweet committed
1278 1279 1280 1281 1282 1283 1284
	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
1285 1286 1287 1288
	{
	 /*
	  * Multi-word UTF-16 char...
	  */
1289

Michael R Sweet's avatar
Michael R Sweet committed
1290
          int lch = (getc(fp) << 8) | getc(fp);
1291

1292
          if (lch < 0xdc00 || lch >= 0xdfff)
Michael R Sweet's avatar
Michael R Sweet committed
1293
	    return (EOF);
1294

Michael R Sweet's avatar
Michael R Sweet committed
1295 1296 1297
          ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
	}
	break;
1298

Michael R Sweet's avatar
Michael R Sweet committed
1299 1300 1301 1302
    case ENCODE_UTF16LE :
       /*
        * Read UTF-16 little-endian char...
	*/
1303

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

Michael R Sweet's avatar
Michael R Sweet committed
1306 1307 1308 1309 1310 1311 1312
        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
1313 1314 1315 1316 1317 1318 1319
	{
	 /*
	  * Multi-word UTF-16 char...
	  */

          int lch = getc(fp) | (getc(fp) << 8);

1320
          if (lch < 0xdc00 || lch >= 0xdfff)
Michael R Sweet's avatar
Michael R Sweet committed
1321 1322 1323 1324 1325
	    return (EOF);

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

1328 1329 1330 1331
#if DEBUG > 1
  printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
#endif /* DEBUG > 1 */

1332
  return (ch);
1333 1334 1335
}


1336 1337 1338 1339 1340 1341 1342 1343
/*
 * '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 */
{
1344
  return (putc(ch, (FILE *)p) == EOF ? -1 : 0);
1345 1346 1347
}


1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
/*
 * '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);
  }

1387
  if (entity[0] == '#')
1388
  {
1389 1390
    if (entity[1] == 'x')
      ch = strtol(entity + 2, NULL, 16);
1391
    else
1392
      ch = strtol(entity + 1, NULL, 10);
1393 1394 1395 1396 1397
  }
  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
1398 1399 1400 1401 1402 1403 1404
  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);
  }

1405 1406 1407 1408
  return (ch);
}


1409 1410 1411 1412 1413
/*
 * '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. */
1414 1415 1416 1417 1418 1419 1420
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
1421 1422
{
  mxml_node_t	*node,			/* Current node */
1423
		*first,			/* First node added */
Michael R Sweet's avatar
Michael R Sweet committed
1424 1425 1426
		*parent;		/* Current parent node */
  int		ch,			/* Character from file */
		whitespace;		/* Non-zero if whitespace seen */
1427
  char		*buffer,		/* String buffer */
Michael R Sweet's avatar
Michael R Sweet committed
1428
		*bufptr;		/* Pointer into buffer */
1429
  int		bufsize;		/* Size of buffer */
Michael R Sweet's avatar
Michael R Sweet committed
1430
  mxml_type_t	type;			/* Current node type */
Michael R Sweet's avatar
Michael R Sweet committed
1431
  int		encoding;		/* Character encoding */
1432 1433
  _mxml_global_t *global = _mxml_global();
					/* Global data */
1434 1435 1436 1437 1438 1439 1440 1441 1442
  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
1443 1444 1445 1446 1447 1448


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

1449 1450
  if ((buffer = malloc(64)) == NULL)
  {
Michael R Sweet's avatar
Michael R Sweet committed
1451
    mxml_error("Unable to allocate string buffer!");
1452 1453 1454 1455
    return (NULL);
  }

  bufsize    = 64;
Michael R Sweet's avatar
Michael R Sweet committed
1456 1457
  bufptr     = buffer;
  parent     = top;
1458
  first      = NULL;
Michael R Sweet's avatar
Michael R Sweet committed
1459
  whitespace = 0;
Michael R Sweet's avatar
Michael R Sweet committed
1460
  encoding   = ENCODE_UTF8;
Michael R Sweet's avatar
Michael R Sweet committed
1461 1462 1463 1464 1465 1466

  if (cb && parent)
    type = (*cb)(parent);
  else
    type = MXML_TEXT;

Michael R Sweet's avatar
Michael R Sweet committed
1467
  while ((ch = (*getc_cb)(p, &encoding)) != EOF)
Michael R Sweet's avatar
Michael R Sweet committed
1468
  {
1469
    if ((ch == '<' ||
1470
         (mxml_isspace(ch) && type != MXML_OPAQUE && type != MXML_CUSTOM)) &&
1471
        bufptr > buffer)
Michael R Sweet's avatar
Michael R Sweet committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    {
     /*
      * Add a new value node...
      */

      *bufptr = '\0';

      switch (type)
      {
	case MXML_INTEGER :
1482
            node = mxmlNewInteger(parent, strtol(buffer, &bufptr, 0));
Michael R Sweet's avatar
Michael R Sweet committed
1483 1484 1485 1486 1487 1488 1489
	    break;

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

	case MXML_REAL :
1490
            node = mxmlNewReal(parent, strtod(buffer, &bufptr));
Michael R Sweet's avatar
Michael R Sweet committed
1491 1492 1493 1494 1495 1496
	    break;

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

1497
	case MXML_CUSTOM :
1498
	    if (global->custom_load_cb)
1499 1500 1501 1502 1503 1504 1505
	    {
	     /*
	      * Use the callback to fill in the custom data...
	      */

              node = mxmlNewCustom(parent, NULL, NULL);

1506
	      if ((*global->custom_load_cb)(node, buffer))
1507 1508 1509 1510 1511 1512 1513 1514 1515
	      {
	        mxml_error("Bad custom value '%s' in parent <%s>!",
		           buffer, parent ? parent->value.element.name : "null");
		mxmlDelete(node);
		node = NULL;
	      }
	      break;
	    }

1516
        default : /* Ignore... */
Michael R Sweet's avatar
Michael R Sweet committed
1517 1518 1519 1520
	    node = NULL;
	    break;
      }	  

1521 1522 1523 1524 1525 1526
      if (*bufptr)
      {
       /*
        * Bad integer/real number value...
	*/

Michael R Sweet's avatar
Michael R Sweet committed
1527 1528 1529
        mxml_error("Bad %s value '%s' in parent <%s>!",
	           type == MXML_INTEGER ? "integer" : "real", buffer,
		   parent ? parent->value.element.name : "null");
1530 1531 1532 1533
	break;
      }

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

1536
      if (!node && type != MXML_IGNORE)
Michael R Sweet's avatar
Michael R Sweet committed
1537 1538
      {
       /*
1539
	* Print error and return...
Michael R Sweet's avatar
Michael R Sweet committed
1540 1541
	*/

1542 1543 1544
	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
1545
      }
1546

1547 1548 1549 1550 1551 1552 1553 1554
      if (sax_cb)
      {
        (*sax_cb)(node, MXML_SAX_DATA, sax_data);

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

1555
      if (!first && node)
1556
        first = node;
Michael R Sweet's avatar
Michael R Sweet committed
1557
    }
1558
    else if (mxml_isspace(ch) && type == MXML_TEXT)
1559 1560 1561
      whitespace = 1;

   /*
1562 1563
    * Add lone whitespace node if we have an element and existing
    * whitespace...
1564 1565 1566 1567
    */

    if (ch == '<' && whitespace && type == MXML_TEXT)
    {
1568 1569
      node = mxmlNewText(parent, whitespace, "");

1570 1571 1572 1573 1574 1575 1576 1577
      if (sax_cb)
      {
        (*sax_cb)(node, MXML_SAX_DATA, sax_data);

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

1578 1579 1580
      if (!first && node)
        first = node;

1581
      whitespace = 0;
1582
    }
Michael R Sweet's avatar
Michael R Sweet committed
1583 1584 1585 1586 1587 1588 1589 1590 1591

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

      bufptr = buffer;

Michael R Sweet's avatar
Michael R Sweet committed
1592
      while ((ch = (*getc_cb)(p, &encoding)) != EOF)
1593
        if (mxml_isspace(ch) || ch == '>' || (ch == '/' && bufptr > buffer))
Michael R Sweet's avatar
Michael R Sweet committed
1594
	  break;
1595 1596 1597 1598 1599
	else if (ch == '<')
	{
	  mxml_error("Bare < in element!");
	  goto error;
	}
1600
	else if (ch == '&')
1601
	{
Michael R Sweet's avatar
Michael R Sweet committed
1602
	  if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb)) == EOF)
1603 1604 1605 1606
	    goto error;

	  if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
	    goto error;
1607
	}
1608 1609
	else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
	  goto error;
1610 1611 1612
	else if (((bufptr - buffer) == 1 && buffer[0] == '?') ||
	         ((bufptr - buffer) == 3 && !strncmp(buffer, "!--", 3)) ||
	         ((bufptr - buffer) == 8 && !strncmp(buffer, "![CDATA[", 8)))
1613
	  break;
1614

Michael R Sweet's avatar
Michael R Sweet committed
1615 1616
      *bufptr = '\0';

Michael R Sweet's avatar
Michael R Sweet committed