This is the mail archive of the
ecos-patches@sourceware.org
mailing list for the eCos project.
Ezxml upgrade to v0.8.5
- From: Peter Korsgaard <jacmet at sunsite dot dk>
- To: ecos-patches at ecos dot sourceware dot org
- Date: Mon, 23 Jan 2006 21:17:22 +0100
- Subject: Ezxml upgrade to v0.8.5
Hi,
This patch upgrades the services/ezxml package to upstream version
0.8.5.
This version has a number of bugfixes and new features compared to the
current 0.8.1 version. From the change log:
ezXML 0.8.5
- fixed ezxml_toxml() to not output siblings of tag being converted
- fixed a segfault when ezxml_set_attr() was used on a new root tag
- added ezxml_name() function macro
- all external functions now handle NULL ezxml_t structs without segfaulting
ezXML 0.8.4
- fixed to compile under win-doze when NOMMAP make option is set
- fixed a bug where ezxml_toxml() could segfault if tag offset is out of bounds
- ezxml_add_child() now works properly when tags are added out of order
- improved error messages now include line numbers
- fixed memory leak when entity reference is shorter than replacement text
- added ezxml_new_d(), ezxml_add_child_d(), ezxml_set_txt_d() and
ezxml_set_attr_d() function macros as wrappers that strdup() their arguments
ezXML 0.8.3
- fixed a UTF-16 decoding bug affecting larger unicode values
- added internal dtd processing for entity declarations and default attributes
- now correctly normalizes attribute values in compliance with the XML 1.0 spec
- added check for correct tag nesting
- ezxml_toxml() now generates canonical xml (apart from the namespace stuff)
ezXML 0.8.2
- fixed compiler warning about lvalue type casting
- ezxml_get() argument list can now be terminated by an empty string tag name
- added NOMMAP make option for systems without posix memory mapping
- added support for UTF-16
- fixed bug in ezxml_toxml() where UTF-8 sequences were being ampersand encoded
- added ezxml_new(), ezxml_add_child(), ezxml_set_txt(), ezxml_set_attr(),
and ezxml_remove() to facilitate creating and modifying xml
Index: packages/services/ezxml/current/ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/services/ezxml/current/ChangeLog,v
retrieving revision 1.1
diff -u -r1.1 ChangeLog
--- packages/services/ezxml/current/ChangeLog 31 Jan 2005 00:09:30 -0000 1.1
+++ packages/services/ezxml/current/ChangeLog 23 Jan 2006 15:33:08 -0000
@@ -1,3 +1,11 @@
+2006-01-23 Peter Korsgaard <jacmet@sunsite.dk>
+
+ * src/ezxml.c:
+ * include/ezxml.h:
+ * doc/ezxml.txt:
+ * doc/ezxml.html:
+ * doc/license.txt: Upgrade to ezxml-0.8.5
+
2005-01-31 Andrew Lunn <andrew.lunn@ascom.ch>
* Import of the ezxml XML parsing library
Index: packages/services/ezxml/current/doc/ezxml.html
===================================================================
RCS file: /cvs/ecos/ecos/packages/services/ezxml/current/doc/ezxml.html,v
retrieving revision 1.1
diff -u -r1.1 ezxml.html
--- packages/services/ezxml/current/doc/ezxml.html 31 Jan 2005 00:09:31 -0000 1.1
+++ packages/services/ezxml/current/doc/ezxml.html 23 Jan 2006 15:33:08 -0000
@@ -4,20 +4,21 @@
<head><title>ezXML</title></head>
<body>
<h1>ezXML - XML Parsing C Library</h1>
- <h3>version 0.8</h3>
+ <h3>version 0.8.5</h3>
<p>
ezXML is a C library for parsing XML documents inspired by
<a href="http://www.php.net/SimpleXML">simpleXML</a> for
- PHP. As the name implies, it's easy to use. It's ideal for parsing xml
+ PHP. As the name implies, it's easy to use. It's ideal for parsing XML
configuration files or REST web service responses. It's also fast and
- lightweight (11k compiled). The latest version is available here:
- <a href="http://prdownloads.sf.net/ezxml/ezxml-0.8.tar.gz?download"
- >ezxml-0.8.tar.gz</a>
+ lightweight (less than 20k compiled). The latest version is available
+ here:
+ <a href="http://prdownloads.sf.net/ezxml/ezxml-0.8.5.tar.gz?download"
+ >ezxml-0.8.5.tar.gz</a>
</p>
<b>Example Usage</b>
<p>
- Given the following example xml document:
+ Given the following example XML document:
</p>
<code>
<?xml version="1.0"?><br />
@@ -37,7 +38,7 @@
</formula1>
</code>
<p>
- This code snipped prints out a list of drivers, which team they drive for,
+ This code snippet prints out a list of drivers, which team they drive for,
and how many championship points they have:
</p>
<code>
@@ -58,7 +59,7 @@
</code>
<p>
Alternately, the following would print out the name of the second driver
- of the first team:
+ on the first team:
</p>
<code>
ezxml_t f1 = ezxml_parse_file("formula1.xml");<br />
@@ -74,14 +75,12 @@
<b>Known Limitations</b>
<ul>
<li>
- No support for UTF-16, however UTF-8 is handled correctly. UTF-16
- support is required for XML 1.0 conformity and will be implimented for
- the 1.0 release.
+ ezXML is not a validating parser.
<br />
</li>
<li>
- Loads the entire xml document into memory at once and does not allow for
- documents to be passed in a chunk at a time. Large xml files can still
+ Loads the entire XML document into memory at once and does not allow for
+ documents to be passed in a chunk at a time. Large XML files can still
be handled though through <code>ezxml_parse_file()</code> and
<code>ezxml_parse_fd()</code>, which use mmap to map the file to a
virtual address space and rely on the virtual memory system to page in
@@ -89,9 +88,10 @@
<br />
</li>
<li>
- Ignores DTDs. Parsing of the internal DTD subset is required for XML 1.0
- conformity and will be implimented for the 1.0 release. ezXML is not,
- and is not likely to become, a validating parser.
+ Does not currently recognize all possible well-formedness errors. It
+ should correctly handle all well-formed XML documents and will either
+ ignore or halt XML processing on well-formedness errors. More
+ well-formedness checking will be added in subsiquent releases.
<br />
</li>
<li>
@@ -106,7 +106,7 @@
<code>"line one\nline two"</code>, and <code><br/></code> is
reported as a sub tag, but the location of <code><br/></code>
within the character data is not. The function
- <code>ezxml_toxml()</code> will convert an ezXML structure back to xml
+ <code>ezxml_toxml()</code> will convert an ezXML structure back to XML
with sub tag locations intact.
</p>
</li>
Index: packages/services/ezxml/current/doc/ezxml.txt
===================================================================
RCS file: /cvs/ecos/ecos/packages/services/ezxml/current/doc/ezxml.txt,v
retrieving revision 1.1
diff -u -r1.1 ezxml.txt
--- packages/services/ezxml/current/doc/ezxml.txt 31 Jan 2005 00:09:31 -0000 1.1
+++ packages/services/ezxml/current/doc/ezxml.txt 23 Jan 2006 15:33:08 -0000
@@ -1,15 +1,15 @@
ezXML - XML Parsing C Library
-version 0.8
+version 0.8.5
ezXML is a C library for parsing XML documents inspired by simpleXML for PHP.
-As the name implies, it's easy to use. It's ideal for parsing xml configuration
-files or REST web service responses. It's also fast and lightweight (11k
-compiled). The latest version is available here:
-http://prdownloads.sf.net/ezxml/ezxml-0.8.tar.gz?download
+As the name implies, it's easy to use. It's ideal for parsing XML configuration
+files or REST web service responses. It's also fast and lightweight (less than
+20k compiled). The latest verions is available here:
+http://prdownloads.sf.net/ezxml/ezxml-0.8.5.tar.gz?download
Example Usage
-Given the following example xml document:
+Given the following example XML document:
<?xml version="1.0"?>
<formula1>
@@ -25,7 +25,7 @@
</team>
</formula1>
-This code snipped prints out a list of drivers, which team they drive for,
+This code snippet prints out a list of drivers, which team they drive for,
and how many championship points they have:
ezxml_t f1 = ezxml_parse_file("formula1.xml"), team, driver;
@@ -40,7 +40,7 @@
}
ezxml_free(f1);
-Alternately, the following would print out the name of the second driver of the
+Alternately, the following would print out the name of the second driver on the
first team:
ezxml_t f1 = ezxml_parse_file("formula1.xml");
@@ -53,18 +53,18 @@
Known Limitations
-- No support for UTF-16, however UTF-8 is handled correctly. UTF-16 support is
- required for XML 1.0 conformity and will be implimented for the 1.0 release.
+- ezXML is not a validating parser
-- Loads the entire xml document into memory at once and does not allow for
- documents to be passed in a chunk at a time. Large xml files can still be
+- Loads the entire XML document into memory at once and does not allow for
+ documents to be passed in a chunk at a time. Large XML files can still be
handled though through ezxml_parse_file() and ezxml_parse_fd(), which use mmap
to map the file to a virtual address space and rely on the virtual memory
system to page in data as needed.
-- Ignores DTDs. Parsing of the internal DTD subset is required for XML 1.0
- conformity and will be implimented for the 1.0 release. ezXML is not, and is
- not likely to become, a validating parser.
+- Does not currently recognize all possible well-formedness errors. It should
+ correctly handle all well-formed XML documents and will either ignore or halt
+ XML processing on well-formedness errors. More well-formedness checking will
+ be added in subsiquent releases.
- In making the character content of tags easy to access, there is no way
provided to keep track of the location of sub tags relative to the character
@@ -76,7 +76,7 @@
The character content of the doc tag is reported as "line one\nline two", and
<br/> is reported as a sub tag, but the location of <br/> within the
character data is not. The function ezxml_toxml() will convert an ezXML
- structure back to xml with sub tag locations intact.
+ structure back to XML with sub tag locations intact.
Licensing
Index: packages/services/ezxml/current/doc/license.txt
===================================================================
RCS file: /cvs/ecos/ecos/packages/services/ezxml/current/doc/license.txt,v
retrieving revision 1.1
diff -u -r1.1 license.txt
--- packages/services/ezxml/current/doc/license.txt 31 Jan 2005 00:09:31 -0000 1.1
+++ packages/services/ezxml/current/doc/license.txt 23 Jan 2006 15:33:08 -0000
@@ -1,4 +1,4 @@
-Copyright 2004 Aaron Voisine <aaron@voisine.org>
+Copyright 2004, 2005 Aaron Voisine <aaron@voisine.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Index: packages/services/ezxml/current/include/ezxml.h
===================================================================
RCS file: /cvs/ecos/ecos/packages/services/ezxml/current/include/ezxml.h,v
retrieving revision 1.1
diff -u -r1.1 ezxml.h
--- packages/services/ezxml/current/include/ezxml.h 31 Jan 2005 00:09:31 -0000 1.1
+++ packages/services/ezxml/current/include/ezxml.h 23 Jan 2006 15:33:08 -0000
@@ -54,7 +54,7 @@
/* ezxml.h
*
- * Copyright 2004 Aaron Voisine <aaron@voisine.org>
+ * Copyright 2004, 2005 Aaron Voisine <aaron@voisine.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -88,29 +88,25 @@
extern "C" {
#endif
-#define EZXML_BUFSIZE 1024
-
-// returns the next tag of the same name in the same section and depth or NULL
-// if not found
-#define ezxml_next(xml) xml->next
-
-// returns the tag character content or empty string if none
-#define ezxml_txt(xml) xml->txt
+#define EZXML_BUFSIZE 1024 // size of internal memory buffers
+#define EZXML_NAMEM 0x80 // name is malloced
+#define EZXML_TXTM 0x40 // txt is malloced
+#define EZXML_DUP 0x20 // attribute name and value are strduped
typedef struct ezxml *ezxml_t;
struct ezxml {
char *name; // tag name
char **attr; // tag attributes { name, value, name, value, ... NULL }
char *txt; // tag character content, empty string if none
- size_t off; // tag offset in parent tag character content
+ size_t off; // tag offset from start of parent tag character content
ezxml_t next; // next tag with same name in this section at this depth
ezxml_t sibling; // next tag with different name in same section and depth
ezxml_t ordered; // next tag, same section and depth, in original order
ezxml_t child; // head of sub tag list, NULL if none
ezxml_t parent; // parent tag, NULL if current tag is root tag
- short flags; // additional information, only used internally for now
+ short flags; // additional information
};
-
+
// Given a string of xml data and its length, parses it and creates an ezxml
// structure. For efficiency, modifies the data by adding null terminators
// and decoding ampersand sequences. If you don't want this, copy the data and
@@ -129,20 +125,31 @@
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
// or ezxml_parse_fd()
ezxml_t ezxml_parse_fp(FILE *fp);
-
-// returns the first child tag (one level deeper) with the given name or NULL if
-// not found
+
+// returns the first child tag (one level deeper) with the given name or NULL
+// if not found
ezxml_t ezxml_child(ezxml_t xml, const char *name);
+// returns the next tag of the same name in the same section and depth or NULL
+// if not found
+#define ezxml_next(xml) ((xml) ? xml->next : NULL)
+
// Returns the Nth tag with the same name in the same section at the same depth
// or NULL if not found. An index of 0 returns the tag given.
ezxml_t ezxml_idx(ezxml_t xml, int idx);
+// returns the name of the given tag
+#define ezxml_name(xml) ((xml) ? xml->name : NULL)
+
+// returns the given tag's character content or empty string if none
+#define ezxml_txt(xml) ((xml) ? xml->txt : "")
+
// returns the value of the requested tag attribute, or NULL if not found
const char *ezxml_attr(ezxml_t xml, const char *attr);
-// Traverses the ezxml sturcture to retrive a specific subtag. Takes a variable
-// length list of tag names and indexes. Final index must be -1. Example:
+// Traverses the ezxml sturcture to retrieve a specific subtag. Takes a
+// variable length list of tag names and indexes. The argument list must be
+// terminated by either an index of -1 or an empty string tag name. Example:
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
// This retrieves the title of the 3rd book on the 1st shelf of library.
// Returns NULL if not found.
@@ -162,6 +169,41 @@
// returns parser error message or empty string if none
const char *ezxml_error(ezxml_t xml);
+// returns a new empty ezxml structure with the given root tag name
+ezxml_t ezxml_new(const char *name);
+
+// wrapper for ezxml_new() that strdup()s name
+#define ezxml_new_d(name) ezxml_set_flag(ezxml_new(strdup(name)), EZXML_NAMEM)
+
+// Adds a child tag. off is the offset of the child tag relative to the start
+// of the parent tag's character content. Returns the child tag.
+ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off);
+
+// wrapper for ezxml_add_child() that strdup()s name
+#define ezxml_add_child_d(xml, name, off) \
+ ezxml_set_flag(ezxml_add_child(xml, strdup(name), off), EZXML_NAMEM)
+
+// sets the character content for the given tag and returns the tag
+ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt);
+
+// wrapper for ezxml_set_txt() that strdup()s txt
+#define ezxml_set_txt_d(xml, txt) \
+ ezxml_set_flag(ezxml_set_txt(xml, strdup(txt)), EZXML_TXTM)
+
+// Sets the given tag attribute or adds a new attribute if not found. A value
+// of NULL will remove the specified attribute.
+void ezxml_set_attr(ezxml_t xml, const char *name, const char *value);
+
+// Wrapper for ezxml_set_attr() that strdup()s name/value. Value cannot be NULL
+#define ezxml_set_attr_d(xml, name, value) \
+ ezxml_set_attr(ezxml_set_flag(xml, EZXML_DUP), strdup(name), strdup(value))
+
+// sets a flag for the given tag and returns the tag
+ezxml_t ezxml_set_flag(ezxml_t xml, short flag);
+
+// removes a tag along with all its subtags
+void ezxml_remove(ezxml_t xml);
+
#ifdef __cplusplus
}
#endif
Index: packages/services/ezxml/current/src/ezxml.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/services/ezxml/current/src/ezxml.c,v
retrieving revision 1.1
diff -u -r1.1 ezxml.c
--- packages/services/ezxml/current/src/ezxml.c 31 Jan 2005 00:09:31 -0000 1.1
+++ packages/services/ezxml/current/src/ezxml.c 23 Jan 2006 15:33:09 -0000
@@ -1,8 +1,8 @@
//==========================================================================
//
-// crc32.c
+// ezxml.h
//
-// Gary S. Brown's 32 bit CRC
+// Simple XML parser
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
@@ -52,10 +52,9 @@
//
//==========================================================================
-
/* ezxml.c
*
- * Copyright 2004 Aaron Voisine <aaron@voisine.org>
+ * Copyright 2004, 2005 Aaron Voisine <aaron@voisine.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -77,6 +76,8 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
+#define EZXML_NOMMAP // no mmap on eCos
+
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
@@ -84,37 +85,40 @@
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
-#ifndef __ECOS__
+#ifndef EZXML_NOMMAP
#include <sys/mman.h>
-#endif
+#endif // EZXML_NOMMAP
#include <sys/stat.h>
#include "ezxml.h"
#include <pkgconf/system.h>
-#define EZXML_TXTM 0x80 // flag value meaning txt was malloced
-#define EZXML_WS "\t\r\n " // whitespace
-
-// called when parser finds close of tag
-#define ezxml_close_tag(root) root->cur = root->cur->parent;
+#define EZXML_WS "\t\r\n " // whitespace
+#define EZXML_ERRL 128 // maximum error string length
typedef struct ezxml_root *ezxml_root_t;
-struct ezxml_root { // additional data for the root tag
- struct ezxml xml; // is a super-struct built on top of ezxml struct
- ezxml_t cur; // current xml tree insertion point
- void *m; // original xml string
- size_t len; // length of allocated memory for mmap, or -1 for malloc
- const char *err; // error string
- char ***pi; // processing instructions
+struct ezxml_root { // additional data for the root tag
+ struct ezxml xml; // is a super-struct built on top of ezxml struct
+ ezxml_t cur; // current xml tree insertion point
+ char *m; // original xml string
+ size_t len; // length of allocated memory for mmap, -1 for malloc
+ char *u; // UTF-8 conversion of string if original was UTF-16
+ char *s; // start of work area
+ char *e; // end of work area
+ char **ent; // general entities (ampersand sequences)
+ char ***attr; // default attributes
+ char ***pi; // processing instructions
+ short standalone; // non-zero if <?xml standalone="yes"?>
+ char err[EZXML_ERRL]; // error string
};
+char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings
+
// returns the first child tag with the given name or NULL if not found
ezxml_t ezxml_child(ezxml_t xml, const char *name)
{
- if (! xml) return NULL;
- xml = xml->child;
+ xml = (xml) ? xml->child : NULL;
while (xml && strcmp(name, xml->name)) xml = xml->sibling;
-
return xml;
}
@@ -129,321 +133,528 @@
// returns the value of the requested tag attribute or NULL if not found
const char *ezxml_attr(ezxml_t xml, const char *attr)
{
- int i = 0;
+ int i = 0, j = 1;
+ ezxml_root_t root = (ezxml_root_t)xml;
- if (! xml) return NULL;
+ if (! xml || ! xml->attr) return NULL;
while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2;
- return (xml->attr[i]) ? xml->attr[i + 1] : NULL;
+ if (xml->attr[i]) return xml->attr[i + 1]; // found attribute
+
+ while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
+ for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++);
+ if (! root->attr[i]) return NULL; // no matching default attributes
+ while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3;
+ return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default
}
-// same as ezxml_get but takes an alredy initialized va_list
+// same as ezxml_get but takes an already initialized va_list
ezxml_t ezxml_vget(ezxml_t xml, va_list ap)
{
char *name = va_arg(ap, char *);
- int idx = va_arg(ap, int);
+ int idx = -1;
- xml = ezxml_child(xml, name);
+ if (name && *name) {
+ idx = va_arg(ap, int);
+ xml = ezxml_child(xml, name);
+ }
return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap);
}
-// Traverses the xml tree to retrive a specific subtag. Takes a variable
-// length list of tag names and indexes. Final index must be -1. Example:
+// Traverses the xml tree to retrieve a specific subtag. Takes a variable
+// length list of tag names and indexes. The argument list must be terminated
+// by either an index of -1 or an empty string tag name. Example:
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
// This retrieves the title of the 3rd book on the 1st shelf of library.
// Returns NULL if not found.
ezxml_t ezxml_get(ezxml_t xml, ...)
{
va_list ap;
- ezxml_t ret;
+ ezxml_t r;
va_start(ap, xml);
- ret = ezxml_vget(xml, ap);
+ r = ezxml_vget(xml, ap);
va_end(ap);
-
- return ret;
+ return r;
}
-// returns a NULL terminated array of processing instructions for the given
+// returns a null terminated array of processing instructions for the given
// target
const char **ezxml_pi(ezxml_t xml, const char *target)
{
- static const char *nopi = NULL;
- ezxml_root_t root;
+ ezxml_root_t root = (ezxml_root_t)xml;
int i = 0;
- while (xml->parent) xml = xml->parent;
- root = (ezxml_root_t)xml;
-
- if (! root->pi) return &nopi;
- while (root->pi[i] && strcmp(target, root->pi[i][0])) i++;
- return (root->pi[i]) ? (const char **)root->pi[i] + 1 : &nopi;
+ if (! root) return (const char **)EZXML_NIL;
+ while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
+ while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
+ return (const char **)((root->pi[i]) ? root->pi[i] + 1 : EZXML_NIL);
}
-// Converts \r or \r\n to a single \n. If decode is non-zero, decodes ampersand
-// sequences in place. Returns s.
-char *ezxml_decode(char *s, int decode)
+// set an error string and return root
+ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...)
{
- int b;
- char *e, *ret = s;
- long c, d;
+ va_list ap;
+ int line = 1;
+ char *t, fmt[EZXML_ERRL];
+
+ for (t = root->s; t < s; t++) if (*t == '\n') line++;
+ snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err);
- for (;;) {
- while (*s && *s != '\r' && *s != '&') s++;
+ va_start(ap, err);
+ vsnprintf(root->err, EZXML_ERRL, fmt, ap);
+ va_end(ap);
- if (! *s) return ret;
- else if (*s == '\r') {
+ return &root->xml;
+}
+
+// Recursively decodes entity and character references and normalizes new lines
+// ent is a null terminated array of alternating entity names and values. set t
+// to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
+// for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
+// attribute normalization. Returns s, or if the decoded string is longer than
+// s, returns a malloced string that must be freed.
+char *ezxml_decode(char *s, char **ent, char t)
+{
+ char *e, *r = s, *m = s;
+ long b, c, d, l;
+
+ for (; *s; s++) { // normalize line endings
+ while (*s == '\r') {
*(s++) = '\n';
- if (*s == '\n') memmove((void *)s, (void *)(s + 1), strlen(s));
- continue;
+ if (*s == '\n') memmove(s, (s + 1), strlen(s));
}
- else if (! decode) { s++; continue; }
- else if (! strncmp(s, "<", 4)) *(s++) = '<';
- else if (! strncmp(s, ">", 4)) *(s++) = '>';
- else if (! strncmp(s, """, 6)) *(s++) = '"';
- else if (! strncmp(s, "'", 6)) *(s++) = '\'';
- else if (! strncmp(s, "&", 5)) s++;
- else if (! strncmp(s, "&#", 2)) {
- if (s[2] == 'x') c = strtol(s + 3, &e, 16);
- else c = strtol(s + 2, &e, 10);
- if (! c || *e != ';') { s++; continue; }
+ }
+
+ for (s = r; ; ) {
+ while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++;
- if (c < 0x80) *(s++) = (char)c; // US-ASCII subset
+ if (! *s) break;
+ else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference
+ if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16
+ else c = strtol(s + 2, &e, 10); // base 10
+ if (! c || *e != ';') { s++; continue; } // not a character ref
+
+ if (c < 0x80) *(s++) = c; // US-ASCII subset
else { // multi-byte UTF-8 sequence
for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
b = (b - 2) / 5; // number of bytes in payload
*(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
}
+
+ memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
}
- else { s++; continue; }
-
- memmove((void *)s, (void *)(strchr(s, ';') + 1), strlen(strchr(s, ';')));
- }
+ else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) ||
+ (*s == '%' && t == '%')) { // entity reference
+ for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b]));
+ b += 2); // find entity in entity list
+
+ if (ent[b++]) { // found a match
+ if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) {
+ l = (d = (s - r)) + c + strlen(e); // new length
+ r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l);
+ e = strchr((s = r + d), ';'); // fix up pointers
+ }
+
+ memmove(s + c, e + 1, strlen(e)); // shift rest of string
+ strncpy(s, ent[b], c); // copy in replacement text
+ }
+ else s++; // not a known entity
+ }
+ else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' ';
+ else s++; // no decoding needed
+ }
+
+ if (t == '*') { // normalize spaces for non-cdata attributes
+ for (s = r; *s; s++) {
+ if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1);
+ while (*s && *s != ' ') s++;
+ }
+ if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space
+ }
+ return r;
}
// called when parser finds start of new tag
void ezxml_open_tag(ezxml_root_t root, char *name, char **attr)
{
ezxml_t xml = root->cur;
+
+ if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt));
+ else xml->name = name; // first open tag
- if (xml->name) { // not root tag
- if (xml->child) { // already have sub tags
- xml = xml->child;
- while (xml->ordered) xml = xml->ordered;
- xml->ordered = (ezxml_t)malloc(sizeof(struct ezxml));
- xml->ordered->parent = root->cur;
- root->cur = xml->ordered;
- xml = xml->parent->child;
-
- while (strcmp(xml->name, name) && xml->sibling) xml = xml->sibling;
- if (! strcmp(xml->name, name)) { // already have this tag type
- while (xml->next) xml = xml->next;
- xml = xml->next = root->cur;
- }
- else xml = xml->sibling = root->cur;
- }
- else { // first sub tag
- xml->child = (ezxml_t)malloc(sizeof(struct ezxml));
- xml->child->parent = xml;
- root->cur = xml = xml->child;
- }
-
- xml->off = strlen(xml->parent->txt); // offset in parent char content
- }
-
- // initialize new tag
- xml->name = name;
xml->attr = attr;
- xml->next = xml->child = xml->sibling = xml->ordered = NULL;
- xml->txt = "";
- xml->flags = 0;
+ root->cur = xml; // update tag insertion point
}
// called when parser finds character content between open and closing tag
-void ezxml_char_content(ezxml_root_t root, char *s, size_t len, short decode)
+void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t)
{
ezxml_t xml = root->cur;
+ char *m = s;
size_t l;
- if (! xml || ! xml->name || ! len) return;
+ if (! xml || ! xml->name || ! len) return; // sanity check
- s[len] = '\0';
- ezxml_decode(s, decode);
+ s[len] = '\0'; // null terminate text (calling functions anticipate this)
+ len = strlen(s = ezxml_decode(s, root->ent, t)) + 1;
- if (! *(xml->txt)) xml->txt = s;
+ if (! *(xml->txt)) xml->txt = s; // initial character content
else { // allocate our own memory and make a copy
- l = strlen(xml->txt);
- if (! (xml->flags & EZXML_TXTM)) {
- xml->txt = strcpy((char *)malloc(l + len + 1), xml->txt);
- xml->flags |= EZXML_TXTM;
- }
- else xml->txt = (char *)realloc((void *)(xml->txt), l + len + 1);
- strcpy(xml->txt + l, s);
+ xml->txt = (xml->flags & EZXML_TXTM) // allocate some space
+ ? realloc(xml->txt, (l = strlen(xml->txt)) + len)
+ : strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt);
+ strcpy(xml->txt + l, s); // add new char content
+ if (s != m) free(s); // free s if it was malloced by ezxml_decode()
+ }
+
+ if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM);
+}
+
+// called when parser finds closing tag
+ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s)
+{
+ if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name))
+ return ezxml_err(root, s, "unexpected closing tag </%s>", name);
+
+ root->cur = root->cur->parent;
+ return NULL;
+}
+
+// checks for circular entity references, returns non-zero if no circular
+// references are found, zero otherwise
+int ezxml_ent_ok(char *name, char *s, char **ent)
+{
+ int i;
+
+ for (; ; s++) {
+ while (*s && *s != '&') s++; // find next entity reference
+ if (! *s) return 1;
+ if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref.
+ for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2);
+ if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0;
}
}
-// called when the parser finds an xml processing instruction
+// called when the parser finds a processing instruction
void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len)
{
int i = 0, j = 1;
char *target = s;
s[len] = '\0'; // null terminate instruction
- *(s += strcspn(s, EZXML_WS)) = '\0'; // null terminate target
- s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target
+ if (*(s += strcspn(s, EZXML_WS))) {
+ *s = '\0'; // null terminate target
+ s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target
+ }
+
+ if (! strcmp(target, "xml")) { // <?xml ... ?>
+ if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10,
+ EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1;
+ return;
+ }
- if (! root->pi) *(root->pi = (char ***)malloc(sizeof(char**))) = NULL;
+ if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi
- while (root->pi[i] && strcmp(target, root->pi[i][0])) i++;
+ while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
if (! root->pi[i]) { // new target
- root->pi = (char ***)realloc(root->pi, sizeof(char **) * (i + 2));
- root->pi[i] = (char **)malloc(sizeof(char *) * 2);
+ root->pi = realloc(root->pi, sizeof(char **) * (i + 2));
+ root->pi[i] = malloc(sizeof(char *) * 3);
root->pi[i][0] = target;
- root->pi[i + 1] = NULL; // null terminate lists
- root->pi[i][1] = (char *)root->pi[i + 1];
+ root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list
+ root->pi[i][2] = strdup(""); // empty document position list
}
- while (root->pi[i][j]) j++;
- root->pi[i] = (char **)realloc(root->pi[i], sizeof(char *) * (j + 2));
- root->pi[i][j] = s;
- root->pi[i][j + 1] = NULL;
+ while (root->pi[i][j]) j++; // find end of instruction list for this target
+ root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3));
+ root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1);
+ strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
+ root->pi[i][j + 1] = NULL; // null terminate pi list for this target
+ root->pi[i][j] = s; // set instruction
}
-// set an error string and return root
-ezxml_t ezxml_seterr(ezxml_root_t root, const char *err)
+// called when the parser finds an internal doctype subset
+short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len)
{
- root->err = err;
- return (ezxml_t)root;
+ char q, *c, *t, *n = NULL, *v, **ent, **pe;
+ int i, j;
+
+ pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL));
+
+ for (s[len] = '\0'; s; ) {
+ while (*s && *s != '<' && *s != '%') s++; // find next declaration
+
+ if (! *s) break;
+ else if (! strncmp(s, "<!ENTITY", 8)) { // parse entity definitions
+ c = s += strspn(s + 8, EZXML_WS) + 8; // skip white space separator
+ n = s + strspn(s, EZXML_WS "%"); // find name
+ *(s = n + strcspn(n, EZXML_WS)) = ';'; // append ; to name
+
+ v = s + strspn(s + 1, EZXML_WS) + 1; // find value
+ if ((q = *(v++)) != '"' && q != '\'') { // skip externals
+ s = strchr(s, '>');
+ continue;
+ }
+
+ for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
+ ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent
+ if (*c == '%') pe = ent;
+ else root->ent = ent;
+
+ *(++s) = '\0'; // null terminate name
+ if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value
+ ent[i + 1] = ezxml_decode(v, pe, '%'); // set value
+ ent[i + 2] = NULL; // null terminate entity list
+ if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference
+ if (ent[i + 1] != v) free(ent[i + 1]);
+ ezxml_err(root, v, "circular entity declaration &%s", n);
+ break;
+ }
+ else ent[i] = n; // set entity name
+ }
+ else if (! strncmp(s, "<!ATTLIST", 9)) { // parse default attributes
+ t = s + strspn(s + 9, EZXML_WS) + 9; // skip whitespace separator
+ if (! *t) { ezxml_err(root, t, "unclosed <!ATTLIST"); break; }
+ if (*(s = t + strcspn(t, EZXML_WS ">")) == '>') continue;
+ else *s = '\0'; // null terminate tag name
+ for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
+
+ while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') {
+ if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name
+ else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
+
+ s += strspn(s + 1, EZXML_WS) + 1; // find next token
+ c = (strncmp(s, "CDATA", 5)) ? "*" : " "; // is it cdata?
+ if (! strncmp(s, "NOTATION", 8))
+ s += strspn(s + 8, EZXML_WS) + 8;
+ s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, EZXML_WS);
+ if (! s) { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
+
+ s += strspn(s, EZXML_WS ")"); // skip white space separator
+ if (! strncmp(s, "#FIXED", 6))
+ s += strspn(s + 6, EZXML_WS) + 6;
+ if (*s == '#') { // no default value
+ s += strcspn(s, EZXML_WS ">") - 1;
+ if (*c == ' ') continue; // cdata is default, nothing to do
+ v = NULL;
+ }
+ else if ((*s == '"' || *s == '\'') && // default value
+ (s = strchr(v = s + 1, *s))) *s = '\0';
+ else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
+
+ if (! root->attr[i]) { // new tag name
+ root->attr = (! i) ? malloc(2 * sizeof(char **))
+ : realloc(root->attr,
+ (i + 2) * sizeof(char **));
+ root->attr[i] = malloc(2 * sizeof(char *));
+ root->attr[i][0] = t; // set tag name
+ root->attr[i][1] = (char *)(root->attr[i + 1] = NULL);
+ }
+
+ for (j = 1; root->attr[i][j]; j += 3); // find end of list
+ root->attr[i] = realloc(root->attr[i],
+ (j + 4) * sizeof(char *));
+
+ root->attr[i][j + 3] = NULL; // null terminate list
+ root->attr[i][j + 2] = c; // is it cdata?
+ root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c)
+ : NULL;
+ root->attr[i][j] = n; // attribute name
+ }
+ }
+ else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); // comments
+ else if (! strncmp(s, "<?", 2)) { // processing instructions
+ if ((s = strstr(c = s + 2, "?>")))
+ ezxml_proc_inst(root, c, s++ - c);
+ }
+ else if (*s == '<') s = strchr(s, '>'); // skip other declarations
+ else if (*(s++) == '%' && ! root->standalone) break;
+ }
+
+ free(pe);
+ return ! *root->err;
+}
+
+// Converts a UTF-16 string to UTF-8. Returns a new string that must be freed
+// or NULL if no conversion was needed.
+char *ezxml_str2utf8(char **s, size_t *len)
+{
+ char *u;
+ size_t l = 0, sl, max = *len;
+ long c, d;
+ int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
+
+ if (be == -1) return NULL; // not UTF-16
+
+ u = malloc(max);
+ for (sl = 2; sl < *len - 1; sl += 2) {
+ c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE
+ : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE
+ if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half
+ d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)
+ : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF);
+ c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;
+ }
+
+ while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE);
+ if (c < 0x80) u[l++] = c; // US-ASCII subset
+ else { // multi-byte UTF-8 sequence
+ for (b = 0, d = c; d; d /= 2) b++; // bits in c
+ b = (b - 2) / 5; // bytes in payload
+ u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head
+ while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
+ }
+ }
+ return *s = realloc(u, *len = l);
+}
+
+// frees a tag attribute list
+void ezxml_free_attr(char **attr) {
+ int i = 0;
+ char *m;
+
+ if (! attr || attr == EZXML_NIL) return; // nothing to free
+ while (attr[i]) i += 2; // find end of attribute list
+ m = attr[i + 1]; // list of which names and values are malloced
+ for (i = 0; m[i]; i++) {
+ if (m[i] & EZXML_NAMEM) free(attr[i * 2]);
+ if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]);
+ }
+ free(m);
+ free(attr);
}
// parse the given xml string and return an ezxml structure
ezxml_t ezxml_parse_str(char *s, size_t len)
{
- ezxml_root_t root = (ezxml_root_t)malloc(sizeof(struct ezxml_root));
- char *d, **attr, q, e;
- static char *noattr[] = { NULL };
- int l;
-
- if (! root) return NULL;
+ ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL);
+ char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning
+ int l, i, j;
+
+ root->m = s;
+ if (! len) return ezxml_err(root, s, "root tag missing");
+ root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8
+ root->e = (root->s = s) + len; // record start and end of work area
- // initialize root tag
- memset((void *)root, '\0', sizeof (struct ezxml_root));
- root->xml.attr = noattr;
- root->cur = (ezxml_t)root;
- root->m = (void *)s;
- root->err = root->xml.txt = "";
-
- if (! len) return ezxml_seterr(root, "root tag missing");
- e = s[len - 1];
- s[len - 1] = '\0';
+ e = s[len - 1]; // save end char
+ s[len - 1] = '\0'; // turn end char into null terminator
while (*s && *s != '<') s++; // find first tag
- if (! *s) return ezxml_seterr(root, "root tag missing");
+ if (! *s) return ezxml_err(root, s, "root tag missing");
- for (;;) {
- attr = noattr;
+ for (; ; ) {
+ attr = (char **)EZXML_NIL;
d = ++s;
if (isalpha(*s) || *s == '_' || *s == ':') { // new tag
- if (! root->cur) return ezxml_seterr(root, "unmatched closing tag");
+ if (! root->cur)
+ return ezxml_err(root, d, "markup outside of root element");
s += strcspn(s, EZXML_WS "/>");
- if (isspace(*s)) *(s++) = '\0';
+ while (isspace(*s)) *(s++) = '\0'; // null terminate tag name
- l = 0;
- while (*s && *s != '/' && *s != '>') { // new tag attribute
- while (isspace(*s)) s++;
-
- attr = (char **)((! l) ? malloc(3 * sizeof (char *)) :
- realloc((void *)attr, (l + 3) * sizeof (char *)));
- attr[l] = s;
+ if (*s && *s != '/' && *s != '>') // find tag in default attr list
+ for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
+
+ for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib
+ attr = (l) ? realloc(attr, (l + 4) * sizeof(char *))
+ : malloc(4 * sizeof(char *)); // allocate space
+ attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2)
+ : malloc(2); // mem for list of maloced vals
+ strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced
+ attr[l + 2] = NULL; // null terminate list
+ attr[l + 1] = ""; // temporary attribute value
+ attr[l] = s; // set attribute name
s += strcspn(s, EZXML_WS "=/>");
if (*s == '=' || isspace(*s)) {
- *(s++) = '\0';
+ *(s++) = '\0'; // null terminate tag attribute name
q = *(s += strspn(s, EZXML_WS "="));
if (q == '"' || q == '\'') { // attribute value
attr[l + 1] = ++s;
while (*s && *s != q) s++;
- if (*s) *(s++) = '\0';
+ if (*s) *(s++) = '\0'; // null terminate attribute val
else {
- free(attr);
- return ezxml_seterr(root, (q == '"') ?
- "missing \"" : "missing '");
+ ezxml_free_attr(attr);
+ return ezxml_err(root, d, "missing %c", q);
}
- ezxml_decode(attr[l + 1], 1);
+
+ for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3);
+ attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a
+ && a[j]) ? *a[j + 2] : ' ');
+ if (attr[l + 1] < d || attr[l + 1] > s)
+ attr[l + 3][l / 2] = EZXML_TXTM; // value malloced
}
- else attr[l + 1] = "";
}
- else attr[l + 1] = "";
-
- attr[(l += 2)] = NULL;
+ while (isspace(*s)) s++;
}
if (*s == '/') { // self closing tag
*(s++) = '\0';
if ((*s && *s != '>') || (! *s && e != '>')) {
- if (l) free(attr);
- return ezxml_seterr(root, "missing >");
+ if (l) ezxml_free_attr(attr);
+ return ezxml_err(root, d, "missing >");
}
ezxml_open_tag(root, d, attr);
- ezxml_close_tag(root);
+ ezxml_close_tag(root, d, s);
}
- else if (*s == '>' || (! *s && e == '>')) { // open tag
- q = *s;
- *s = '\0';
+ else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag
+ *s = '\0'; // temporarily null terminate tag name
ezxml_open_tag(root, d, attr);
*s = q;
}
else {
- if (l) free(attr);
- return ezxml_seterr(root, "missing >");
+ if (l) ezxml_free_attr(attr);
+ return ezxml_err(root, d, "missing >");
}
}
else if (*s == '/') { // close tag
- if (! root->cur) return ezxml_seterr(root, "unmatched closing tag");
- ezxml_close_tag(root);
- while (*s && *s != '>') s++;
- if (! *s && e != '>') return ezxml_seterr(root, "missing >");
+ s += strcspn(d = s + 1, EZXML_WS ">") + 1;
+ if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >");
+ *s = '\0'; // temporarily null terminate tag name
+ if (ezxml_close_tag(root, d, s)) return &root->xml;
+ if (isspace(*s = q)) s += strspn(s, EZXML_WS);
}
else if (! strncmp(s, "!--", 3)) { // comment
- do { s = strstr(s, "--"); } while (s && *(s += 2) && *s != '>');
- if (! s || (! *s && e != '>'))
- return ezxml_seterr(root, "unclosed <!--");
+ if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) ||
+ (! *s && e != '>')) return ezxml_err(root, d, "unclosed <!--");
}
else if (! strncmp(s, "![CDATA[", 8)) { // cdata
if ((s = strstr(s, "]]>")))
- ezxml_char_content(root, d + 8, (s += 2) - d - 10, 0);
- else return ezxml_seterr(root, "unclosed <![CDATA[");
+ ezxml_char_content(root, d + 8, (s += 2) - d - 10, 'c');
+ else return ezxml_err(root, d, "unclosed <![CDATA[");
}
- else if (! strncmp(s, "!DOCTYPE", 8)) { // skip <!DOCTYPE declarations
+ else if (! strncmp(s, "!DOCTYPE", 8)) { // dtd
for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' ||
- s[strspn(s + 1, EZXML_WS) + 1] != '>')));
+ *(s + strspn(s + 1, EZXML_WS) + 1) != '>')));
l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
- if (! *s && e != '>')
- return ezxml_seterr(root, "unclosed <!DOCTYPE");
+ if (! *s && e != '>')
+ return ezxml_err(root, d, "unclosed <!DOCTYPE");
+ d = (l) ? strchr(d, '[') + 1 : d;
+ if (l && ! ezxml_internal_dtd(root, d, s++ - d)) return &root->xml;
}
else if (*s == '?') { // <?...?> processing instructions
do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
if (! s || (! *s && e != '>'))
- return ezxml_seterr(root, "unclosed <?");
+ return ezxml_err(root, d, "unclosed <?");
else ezxml_proc_inst(root, d + 1, s - d - 2);
}
- else return ezxml_seterr(root, "syntax error");
+ else return ezxml_err(root, d, "unexpected <");
if (! s || ! *s) break;
*s = '\0';
d = ++s;
if (*s && *s != '<') { // tag character content
while (*s && *s != '<') s++;
- if (*s) ezxml_char_content(root, d, s - d, 1);
+ if (*s) ezxml_char_content(root, d, s - d, '&');
else break;
}
else if (! *s) break;
}
-
- return (root->cur) ? ezxml_seterr(root, (root->cur->name) ? "unclosed tag" :
- "root tag missing") : (ezxml_t)root;
+
+ if (! root->cur) return &root->xml;
+ else if (! root->cur->name) return ezxml_err(root, d, "root tag missing");
+ else return ezxml_err(root, d, "unclosed tag <%s>", root->cur->name);
}
#ifdef CYGPKG_IO_FILEIO
@@ -452,22 +663,20 @@
// or ezxml_parse_fd()
ezxml_t ezxml_parse_fp(FILE *fp)
{
- ezxml_root_t ret;
- size_t l, len = 0, ps = (size_t)sysconf(_SC_PAGESIZE);
- void *s;
+ ezxml_root_t root;
+ size_t l, len = 0;
+ char *s;
- if (! (s = (char *)malloc(ps))) return NULL;
+ if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
do {
- len += (l = fread((void *)((int)s + len), 1, ps, fp));
- if (l == ps) s = (char *)realloc((void *)s, len + ps);
- } while (s && l == ps);
+ len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
+ if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
+ } while (s && l == EZXML_BUFSIZE);
if (! s) return NULL;
-
- ret = (ezxml_root_t)ezxml_parse_str(s, len);
- ret->len = -1; // so we know to free s in ezxml_free()
-
- return (ezxml_t)ret;
+ root = (ezxml_root_t)ezxml_parse_str(s, len);
+ root->len = -1; // so we know to free s in ezxml_free()
+ return &root->xml;
}
// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
@@ -475,50 +684,48 @@
// Returns NULL on failure.
ezxml_t ezxml_parse_fd(int fd)
{
- ezxml_root_t ret;
- struct stat stat;
- size_t len, ps = (size_t)sysconf(_SC_PAGESIZE);
+ ezxml_root_t root;
+ struct stat st;
+ size_t l;
void *m;
if (fd < 0) return NULL;
+ fstat(fd, &st);
- fstat(fd, &stat);
- len = (stat.st_size + ps - 1) & ~(ps - 1); // round up to next page boundry
-#ifdef __ECOS__
- if (0) {
-#else
- if ((m = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
+#ifndef EZXML_NOMMAP
+ l = (st.st_size + sysconf(_SC_PAGESIZE) - 1) & ~(sysconf(_SC_PAGESIZE) -1);
+ if ((m = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
MAP_FAILED) {
- madvise(m, len, MADV_SEQUENTIAL); // optimize for sequential access
- ret = (ezxml_root_t)ezxml_parse_str((char *)m, stat.st_size);
- madvise(m, len, MADV_NORMAL); // put it back to normal
- ret->len = len;
-#endif
+ madvise(m, l, MADV_SEQUENTIAL); // optimize for sequential access
+ root = (ezxml_root_t)ezxml_parse_str(m, st.st_size);
+ madvise(m, root->len = l, MADV_NORMAL); // put it back to normal
}
else { // mmap failed, read file into memory
- if (! (m = malloc(stat.st_size))) return NULL;
- len = read(fd, m, stat.st_size);
- ret = (ezxml_root_t)ezxml_parse_str((char *)m, len);
- ret->len = -1;
+#endif // EZXML_NOMMAP
+ l = read(fd, m = malloc(st.st_size), st.st_size);
+ root = (ezxml_root_t)ezxml_parse_str(m, l);
+ root->len = -1; // so we know to free s in ezxml_free()
+#ifndef EZXML_NOMMAP
}
-
- return (ezxml_t)ret;
+#endif // EZXML_NOMMAP
+ return &root->xml;
}
// a wrapper for ezxml_parse_fd that accepts a file name
ezxml_t ezxml_parse_file(const char *file)
{
int fd = open(file, O_RDONLY, 0);
- ezxml_t ret = ezxml_parse_fd(fd);
+ ezxml_t xml = ezxml_parse_fd(fd);
if (fd >= 0) close(fd);
- return ret;
+ return xml;
}
-#endif
-// Encodes ampersand sequences appending the results to dst, reallocating dst
-// if it's length excedes max. Returns *dst.
+#endif // CYGPKG_IO_FILEIO
+
+// Encodes ampersand sequences appending the results to *dst, reallocating *dst
+// if length excedes max. a is non-zero for attribute encoding. Returns *dst
char *ezxml_ampencode(const char *s, size_t len, char **dst, size_t *dlen,
- size_t *max)
+ size_t *max, short a)
{
const char *e;
@@ -530,130 +737,327 @@
case '&': *dlen += sprintf(*dst + *dlen, "&"); break;
case '<': *dlen += sprintf(*dst + *dlen, "<"); break;
case '>': *dlen += sprintf(*dst + *dlen, ">"); break;
- case '"': *dlen += sprintf(*dst + *dlen, """); break;
- default:
- if (*s >= ' ' || *s == '\n' || *s == '\t') (*dst)[(*dlen)++] = *s;
- else *dlen += sprintf(*dst + *dlen, "&#%02d;", *s);
+ case '"': *dlen += sprintf(*dst + *dlen, (a) ? """ : "\""); break;
+ case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "
" : "\n"); break;
+ case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "	" : "\t"); break;
+ case '\r': *dlen += sprintf(*dst + *dlen, "
"); break;
+ default: (*dst)[(*dlen)++] = *s;
}
}
-
return *dst;
}
-// Recursively converts each tag to xml appending it to s. Reallocates s if it's
-// length excedes max. start is the location of the previous tag in the parent
-// tag's character content. Returns *s.
+// Recursively converts each tag to xml appending it to *s. Reallocates *s if
+// its length excedes max. start is the location of the previous tag in the
+// parent tag's character content. Returns *s.
char *ezxml_toxml_r(ezxml_t xml, char **s, size_t *len, size_t *max,
- size_t start)
+ size_t start, char ***attr)
{
- int i;
+ int i, j;
char *txt = (xml->parent) ? xml->parent->txt : "";
+ size_t off = 0;
// parent character content up to this tag
- ezxml_ampencode(txt + start, xml->off - start, s, len, max);
+ *s = ezxml_ampencode(txt + start, xml->off - start, s, len, max, 0);
while (*len + strlen(xml->name) + 4 > *max) // reallocate s
*s = realloc(*s, *max += EZXML_BUFSIZE);
*len += sprintf(*s + *len, "<%s", xml->name); // open tag
for (i = 0; xml->attr[i]; i += 2) { // tag attributes
+ if (ezxml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
while (*len + strlen(xml->attr[i]) + 7 > *max) // reallocate s
*s = realloc(*s, *max += EZXML_BUFSIZE);
*len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
- ezxml_ampencode(xml->attr[i + 1], -1, s, len, max);
+ ezxml_ampencode(xml->attr[i + 1], -1, s, len, max, 1);
*len += sprintf(*s + *len, "\"");
}
- if (xml->child || *(xml->txt)) { // tag content
- *len += sprintf(*s + *len, ">");
- if (xml->child) ezxml_toxml_r(xml->child, s, len, max, 0);
- else ezxml_ampencode(xml->txt, -1, s, len, max); // char content
-
- while (*len + strlen(xml->name) + 4 > *max) // reallocate s
+ for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
+ for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes
+ if (! attr[i][j + 1] || ezxml_attr(xml, attr[i][j]) != attr[i][j + 1])
+ continue; // skip duplicates and non-values
+ while (*len + strlen(attr[i][j]) + 7 > *max) // reallocate s
*s = realloc(*s, *max += EZXML_BUFSIZE);
- *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
+ *len += sprintf(*s + *len, " %s=\"", attr[i][j]);
+ ezxml_ampencode(attr[i][j + 1], -1, s, len, max, 1);
+ *len += sprintf(*s + *len, "\"");
}
- else *len += sprintf(*s + *len, "/>"); // self closing tag
+ *len += sprintf(*s + *len, ">");
+
+ *s = (xml->child) ? ezxml_toxml_r(xml->child, s, len, max, 0, attr) //child
+ : ezxml_ampencode(xml->txt, -1, s, len, max, 0); //data
+
+ while (*len + strlen(xml->name) + 4 > *max) // reallocate s
+ *s = realloc(*s, *max += EZXML_BUFSIZE);
- if (xml->ordered) return ezxml_toxml_r(xml->ordered, s, len, max, xml->off);
- return ezxml_ampencode(txt + xml->off, -1, s, len, max);
+ *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
+
+ while (txt[off] && off < xml->off) off++; // make sure off is within bounds
+ return (xml->ordered) ? ezxml_toxml_r(xml->ordered, s, len, max, off, attr)
+ : ezxml_ampencode(txt + off, -1, s, len, max, 0);
}
// converts an ezxml structure back to xml, returning it as a string that must
// be freed
char *ezxml_toxml(ezxml_t xml)
{
- ezxml_t p = xml->parent;
+ ezxml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL;
+ ezxml_root_t root = (ezxml_root_t)xml;
size_t len = 0, max = EZXML_BUFSIZE;
- char *s = strcpy((char *)malloc(max), "");
+ char *s = strcpy(malloc(max), ""), *t, *n;
+ int i, j, k;
if (! xml || ! xml->name) return realloc(s, len + 1);
+ while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
- xml->parent = NULL;
- ezxml_toxml_r(xml, &s, &len, &max, 0);
+ for (i = 0; ! p && root->pi[i]; i++) { // pre-root processing instructions
+ for (k = 2; root->pi[i][k - 1]; k++);
+ for (j = 1; (n = root->pi[i][j]); j++) {
+ if (root->pi[i][k][j - 1] == '>') continue; // not pre-root
+ while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
+ s = realloc(s, max += EZXML_BUFSIZE);
+ len += sprintf(s + len, "<?%s%s%s?>\n", t, *n ? " " : "", n);
+ }
+ }
+
+ xml->parent = xml->ordered = NULL;
+ s = ezxml_toxml_r(xml, &s, &len, &max, 0, root->attr);
xml->parent = p;
+ xml->ordered = o;
+ for (i = 0; ! p && root->pi[i]; i++) { // post-root processing instructions
+ for (k = 2; root->pi[i][k - 1]; k++);
+ for (j = 1; (n = root->pi[i][j]); j++) {
+ if (root->pi[i][k][j - 1] == '<') continue; // not post-root
+ while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
+ s = realloc(s, max += EZXML_BUFSIZE);
+ len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
+ }
+ }
return realloc(s, len + 1);
}
// free the memory allocated for the ezxml structure
void ezxml_free(ezxml_t xml)
{
- void *m = NULL; // assigned to null to avoid compiler warning
- size_t len = 0;
- int i;
+ ezxml_root_t root = (ezxml_root_t)xml;
+ int i, j;
+ char **a, *s;
if (! xml) return;
+ ezxml_free(xml->child);
+ ezxml_free(xml->ordered);
- if (! xml->parent) {
- m = ((ezxml_root_t)xml)->m;
- len = ((ezxml_root_t)xml)->len;
-
- if (((ezxml_root_t)xml)->pi) {
- for (i = 0; ((ezxml_root_t)xml)->pi[i]; i++)
- free(((ezxml_root_t)xml)->pi[i]);
- free(((ezxml_root_t)xml)->pi);
+ if (! xml->parent) { // free root tag allocations
+ for (i = 10; root->ent[i]; i += 2) // 0 - 9 are default entites (<>&"')
+ if ((s = root->ent[i + 1]) < root->s || s > root->e) free(s);
+ free(root->ent); // free list of general entities
+
+ for (i = 0; (a = root->attr[i]); i++) {
+ for (j = 1; a[j++]; j += 2) // free malloced attribute values
+ if (a[j] && (a[j] < root->s || a[j] > root->e)) free(a[j]);
+ free(a);
}
- }
+ if (root->attr[0]) free(root->attr); // free default attribute list
- ezxml_free(xml->child);
- ezxml_free(xml->ordered);
+ for (i = 0; root->pi[i]; i++) {
+ for (j = 1; root->pi[i][j]; j++);
+ free(root->pi[i][j + 1]);
+ free(root->pi[i]);
+ }
+ if (root->pi[0]) free(root->pi); // free processing instructions
+
+ if (root->len == -1) free(root->m); // malloced xml data
+#ifndef EZXML_NOMMAP
+ else if (root->len) munmap(root->m, root->len); // mem mapped xml data
+#endif // EZXML_NOMMAP
+ if (root->u) free(root->u); // utf8 conversion
+ }
- if (xml->attr[0]) free((void *)xml->attr);
- if ((xml->flags & EZXML_TXTM)) free((void *)xml->txt);
- free((void *)xml);
-
- if (len == -1) free(m);
-#ifndef __ECOS__
- else if (len) munmap(m, len);
-#endif
+ ezxml_free_attr(xml->attr); // tag attributes
+ if ((xml->flags & EZXML_TXTM)) free(xml->txt); // character content
+ if ((xml->flags & EZXML_NAMEM)) free(xml->name); // tag name
+ free(xml);
}
// return parser error message or empty string if none
const char *ezxml_error(ezxml_t xml)
{
- while (xml->parent) xml = xml->parent;
- return ((ezxml_root_t)xml)->err;
+ while (xml && xml->parent) xml = xml->parent; // find root tag
+ return (xml) ? ((ezxml_root_t)xml)->err : "";
}
-#ifdef EZXMLTEST // test harness
+// returns a new empty ezxml structure with the given root tag name
+ezxml_t ezxml_new(const char *name)
+{
+ static char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """,
+ "apos;", "'", "amp;", "&", NULL };
+ ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)),
+ '\0', sizeof(struct ezxml_root));
+ root->xml.name = (char *)name;
+ root->cur = &root->xml;
+ strcpy(root->err, root->xml.txt = "");
+ root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
+ root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL);
+ return &root->xml;
+}
+
+// Adds a child tag. off is the offset of the child tag relative to the start
+// of the parent tag's character content. returns the child tag
+ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off)
+{
+ ezxml_t cur, head, child;
+ if (! xml) return NULL;
+ child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0',
+ sizeof(struct ezxml));
+ child->name = (char *)name;
+ child->attr = EZXML_NIL;
+ child->off = off;
+ child->parent = xml;
+ child->txt = "";
+
+ if ((head = xml->child)) { // already have sub tags
+ if (head->off <= off) { // not first subtag
+ for (cur = head; cur->ordered && cur->ordered->off <= off;
+ cur = cur->ordered);
+ child->ordered = cur->ordered;
+ cur->ordered = child;
+ }
+ else { // first subtag
+ child->ordered = head;
+ xml->child = child;
+ }
+
+ for (cur = head; cur->sibling && strcmp(cur->name, name);
+ cur = cur->sibling); // find tag type
+ if (! strcmp(cur->name, name) && cur->off <= off) { //not first of type
+ while (cur->next && cur->next->off <= off) cur = cur->next;
+ child->next = cur->next;
+ cur->next = child;
+ }
+ else { // first tag of this type
+ if (cur->off > off) child->next = cur; // not only tag of this type
+ for (cur = head; cur->sibling && cur->sibling->off <= off;
+ cur = cur->sibling);
+ child->sibling = cur->sibling;
+ cur->sibling = child;
+ }
+ }
+ else xml->child = child; // only sub tag
+
+ return child;
+}
+
+// sets the character content for the given tag and returns the tag
+ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt)
+{
+ if (! xml) return NULL;
+ if (xml->flags & EZXML_TXTM) free(xml->txt); // existing txt was malloced
+ xml->flags &= ~EZXML_TXTM;
+ xml->txt = (char *)txt;
+ return xml;
+}
+
+// Sets the given tag attribute or adds a new attribute if not found. A value
+// of NULL will remove the specified attribute.
+void ezxml_set_attr(ezxml_t xml, const char *name, const char *value)
+{
+ int l = 0, c;
+
+ if (! xml) return;
+ while (xml->attr[l] && strcmp(xml->attr[l], name)) l += 2;
+ if (! xml->attr[l]) { // not found, add as new attribute
+ if (! value) return; // nothing to do
+ if (xml->attr == EZXML_NIL) { // first attribute
+ xml->attr = malloc(4 * sizeof(char *));
+ xml->attr[1] = strdup(""); // empty list of malloced names/vals
+ }
+ else xml->attr = realloc(xml->attr, (l + 4) * sizeof(char *));
+
+ xml->attr[l] = (char *)name; // set attribute name
+ xml->attr[l + 2] = NULL; // null terminate attribute list
+ xml->attr[l + 3] = realloc(xml->attr[l + 1],
+ (c = strlen(xml->attr[l + 1])) + 2);
+ strcpy(xml->attr[l + 3] + c, " "); // set name/value as not malloced
+ if (xml->flags & EZXML_DUP) xml->attr[l + 3][c] = EZXML_NAMEM;
+ }
+ else if (xml->flags & EZXML_DUP) free((char *)name); // name was strduped
+
+ for (c = l; xml->attr[c]; c += 2); // find end of attribute list
+ if (xml->attr[c + 1][l / 2] & EZXML_TXTM) free(xml->attr[l + 1]); //old val
+ if (xml->flags & EZXML_DUP) xml->attr[c + 1][l / 2] |= EZXML_TXTM;
+ else xml->attr[c + 1][l / 2] &= ~EZXML_TXTM;
+
+ if (value) xml->attr[l + 1] = (char *)value; // set attribute value
+ else { // remove attribute
+ if (xml->attr[c + 1][l / 2] & EZXML_NAMEM) free(xml->attr[l]);
+ memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
+ xml->attr = realloc(xml->attr, (c + 2) * sizeof(char *));
+ memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
+ (c / 2) - (l / 2)); // fix list of which name/vals are malloced
+ }
+ xml->flags &= ~EZXML_DUP; // clear strdup() flag
+}
+
+// sets a flag for the given tag and returns the tag
+ezxml_t ezxml_set_flag(ezxml_t xml, short flag)
+{
+ if (xml) xml->flags |= flag;
+ return xml;
+}
+
+// removes a tag along with all its subtags
+void ezxml_remove(ezxml_t xml)
+{
+ ezxml_t cur;
+
+ if (! xml) return; // nothing to do
+ if (xml->next) xml->next->sibling = xml->sibling; // patch sibling list
+
+ if (xml->parent) { // not root tag
+ cur = xml->parent->child; // find head of subtag list
+ if (cur == xml) xml->parent->child = xml->ordered; // first subtag
+ else { // not first subtag
+ while (cur->ordered != xml) cur = cur->ordered;
+ cur->ordered = cur->ordered->ordered; // patch ordered list
+
+ cur = xml->parent->child; // go back to head of subtag list
+ if (strcmp(cur->name, xml->name)) { // not in first sibling list
+ while (strcmp(cur->sibling->name, xml->name))
+ cur = cur->sibling;
+ if (cur->sibling == xml) { // first of a sibling list
+ cur->sibling = (xml->next) ? xml->next
+ : cur->sibling->sibling;
+ }
+ else cur = cur->sibling; // not first of a sibling list
+ }
+
+ while (cur->next && cur->next != xml) cur = cur->next;
+ if (cur->next) cur->next = cur->next->next; // patch next list
+ }
+ }
+ xml->ordered = NULL; // prevent ezxml_free() from clobbering ordered list
+ ezxml_free(xml);
+}
+
+#ifdef EZXML_TEST // test harness
int main(int argc, char **argv)
{
ezxml_t xml;
char *s;
+ int i;
if (argc != 2) return fprintf(stderr, "usage: %s xmlfile\n", argv[0]);
xml = ezxml_parse_file(argv[1]);
- printf("%s", (s = ezxml_toxml(xml)));
- fprintf(stderr, "%s", ezxml_error(xml));
+ printf("%s\n", (s = ezxml_toxml(xml)));
free(s);
+ i = fprintf(stderr, "%s", ezxml_error(xml));
ezxml_free(xml);
-
- return 0;
+ return (i) ? 1 : 0;
}
-
-#endif // EZXMLTEST
+#endif // EZXML_TEST
--
Bye, Peter Korsgaard