1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ezxml.h
|
4 |
|
|
//
|
5 |
|
|
// Simple XML parser
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2005 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Aaron Voisine
|
43 |
|
|
// Contributors: Matt Jerdonek
|
44 |
|
|
// Date: 2005-01-31
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
//
|
48 |
|
|
// This code is part of eCos (tm).
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
/* ezxml.c
|
55 |
|
|
*
|
56 |
|
|
* Copyright 2004, 2005 Aaron Voisine <aaron@voisine.org>
|
57 |
|
|
*
|
58 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
59 |
|
|
* a copy of this software and associated documentation files (the
|
60 |
|
|
* "Software"), to deal in the Software without restriction, including
|
61 |
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
62 |
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
63 |
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
64 |
|
|
* the following conditions:
|
65 |
|
|
*
|
66 |
|
|
* The above copyright notice and this permission notice shall be included
|
67 |
|
|
* in all copies or substantial portions of the Software.
|
68 |
|
|
*
|
69 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
70 |
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
71 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
72 |
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
73 |
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
74 |
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
75 |
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
76 |
|
|
*/
|
77 |
|
|
|
78 |
|
|
#define EZXML_NOMMAP // no mmap on eCos
|
79 |
|
|
|
80 |
|
|
#include <stdlib.h>
|
81 |
|
|
#include <stdio.h>
|
82 |
|
|
#include <stdarg.h>
|
83 |
|
|
#include <string.h>
|
84 |
|
|
#include <ctype.h>
|
85 |
|
|
#include <unistd.h>
|
86 |
|
|
#include <sys/types.h>
|
87 |
|
|
#ifndef EZXML_NOMMAP
|
88 |
|
|
#include <sys/mman.h>
|
89 |
|
|
#endif // EZXML_NOMMAP
|
90 |
|
|
#include <sys/stat.h>
|
91 |
|
|
#include "ezxml.h"
|
92 |
|
|
|
93 |
|
|
#include <pkgconf/system.h>
|
94 |
|
|
|
95 |
|
|
#define EZXML_WS "\t\r\n " // whitespace
|
96 |
|
|
#define EZXML_ERRL 128 // maximum error string length
|
97 |
|
|
|
98 |
|
|
typedef struct ezxml_root *ezxml_root_t;
|
99 |
|
|
struct ezxml_root { // additional data for the root tag
|
100 |
|
|
struct ezxml xml; // is a super-struct built on top of ezxml struct
|
101 |
|
|
ezxml_t cur; // current xml tree insertion point
|
102 |
|
|
char *m; // original xml string
|
103 |
|
|
size_t len; // length of allocated memory for mmap, -1 for malloc
|
104 |
|
|
char *u; // UTF-8 conversion of string if original was UTF-16
|
105 |
|
|
char *s; // start of work area
|
106 |
|
|
char *e; // end of work area
|
107 |
|
|
char **ent; // general entities (ampersand sequences)
|
108 |
|
|
char ***attr; // default attributes
|
109 |
|
|
char ***pi; // processing instructions
|
110 |
|
|
short standalone; // non-zero if <?xml standalone="yes"?>
|
111 |
|
|
char err[EZXML_ERRL]; // error string
|
112 |
|
|
};
|
113 |
|
|
|
114 |
|
|
char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings
|
115 |
|
|
|
116 |
|
|
// returns the first child tag with the given name or NULL if not found
|
117 |
|
|
ezxml_t ezxml_child(ezxml_t xml, const char *name)
|
118 |
|
|
{
|
119 |
|
|
xml = (xml) ? xml->child : NULL;
|
120 |
|
|
while (xml && strcmp(name, xml->name)) xml = xml->sibling;
|
121 |
|
|
return xml;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
// returns the Nth tag with the same name in the same subsection or NULL if not
|
125 |
|
|
// found
|
126 |
|
|
ezxml_t ezxml_idx(ezxml_t xml, int idx)
|
127 |
|
|
{
|
128 |
|
|
for (; xml && idx; idx--) xml = xml->next;
|
129 |
|
|
return xml;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
// returns the value of the requested tag attribute or NULL if not found
|
133 |
|
|
const char *ezxml_attr(ezxml_t xml, const char *attr)
|
134 |
|
|
{
|
135 |
|
|
int i = 0, j = 1;
|
136 |
|
|
ezxml_root_t root = (ezxml_root_t)xml;
|
137 |
|
|
|
138 |
|
|
if (! xml || ! xml->attr) return NULL;
|
139 |
|
|
while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2;
|
140 |
|
|
if (xml->attr[i]) return xml->attr[i + 1]; // found attribute
|
141 |
|
|
|
142 |
|
|
while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
|
143 |
|
|
for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++);
|
144 |
|
|
if (! root->attr[i]) return NULL; // no matching default attributes
|
145 |
|
|
while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3;
|
146 |
|
|
return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
// same as ezxml_get but takes an already initialized va_list
|
150 |
|
|
ezxml_t ezxml_vget(ezxml_t xml, va_list ap)
|
151 |
|
|
{
|
152 |
|
|
char *name = va_arg(ap, char *);
|
153 |
|
|
int idx = -1;
|
154 |
|
|
|
155 |
|
|
if (name && *name) {
|
156 |
|
|
idx = va_arg(ap, int);
|
157 |
|
|
xml = ezxml_child(xml, name);
|
158 |
|
|
}
|
159 |
|
|
return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap);
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
// Traverses the xml tree to retrieve a specific subtag. Takes a variable
|
163 |
|
|
// length list of tag names and indexes. The argument list must be terminated
|
164 |
|
|
// by either an index of -1 or an empty string tag name. Example:
|
165 |
|
|
// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
|
166 |
|
|
// This retrieves the title of the 3rd book on the 1st shelf of library.
|
167 |
|
|
// Returns NULL if not found.
|
168 |
|
|
ezxml_t ezxml_get(ezxml_t xml, ...)
|
169 |
|
|
{
|
170 |
|
|
va_list ap;
|
171 |
|
|
ezxml_t r;
|
172 |
|
|
|
173 |
|
|
va_start(ap, xml);
|
174 |
|
|
r = ezxml_vget(xml, ap);
|
175 |
|
|
va_end(ap);
|
176 |
|
|
return r;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
// returns a null terminated array of processing instructions for the given
|
180 |
|
|
// target
|
181 |
|
|
const char **ezxml_pi(ezxml_t xml, const char *target)
|
182 |
|
|
{
|
183 |
|
|
ezxml_root_t root = (ezxml_root_t)xml;
|
184 |
|
|
int i = 0;
|
185 |
|
|
|
186 |
|
|
if (! root) return (const char **)EZXML_NIL;
|
187 |
|
|
while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
|
188 |
|
|
while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
|
189 |
|
|
return (const char **)((root->pi[i]) ? root->pi[i] + 1 : EZXML_NIL);
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
// set an error string and return root
|
193 |
|
|
ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...)
|
194 |
|
|
{
|
195 |
|
|
va_list ap;
|
196 |
|
|
int line = 1;
|
197 |
|
|
char *t, fmt[EZXML_ERRL];
|
198 |
|
|
|
199 |
|
|
for (t = root->s; t < s; t++) if (*t == '\n') line++;
|
200 |
|
|
snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err);
|
201 |
|
|
|
202 |
|
|
va_start(ap, err);
|
203 |
|
|
vsnprintf(root->err, EZXML_ERRL, fmt, ap);
|
204 |
|
|
va_end(ap);
|
205 |
|
|
|
206 |
|
|
return &root->xml;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
// Recursively decodes entity and character references and normalizes new lines
|
210 |
|
|
// ent is a null terminated array of alternating entity names and values. set t
|
211 |
|
|
// to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
|
212 |
|
|
// for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
|
213 |
|
|
// attribute normalization. Returns s, or if the decoded string is longer than
|
214 |
|
|
// s, returns a malloced string that must be freed.
|
215 |
|
|
char *ezxml_decode(char *s, char **ent, char t)
|
216 |
|
|
{
|
217 |
|
|
char *e, *r = s, *m = s;
|
218 |
|
|
long b, c, d, l;
|
219 |
|
|
|
220 |
|
|
for (; *s; s++) { // normalize line endings
|
221 |
|
|
while (*s == '\r') {
|
222 |
|
|
*(s++) = '\n';
|
223 |
|
|
if (*s == '\n') memmove(s, (s + 1), strlen(s));
|
224 |
|
|
}
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
for (s = r; ; ) {
|
228 |
|
|
while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++;
|
229 |
|
|
|
230 |
|
|
if (! *s) break;
|
231 |
|
|
else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference
|
232 |
|
|
if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16
|
233 |
|
|
else c = strtol(s + 2, &e, 10); // base 10
|
234 |
|
|
if (! c || *e != ';') { s++; continue; } // not a character ref
|
235 |
|
|
|
236 |
|
|
if (c < 0x80) *(s++) = c; // US-ASCII subset
|
237 |
|
|
else { // multi-byte UTF-8 sequence
|
238 |
|
|
for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
|
239 |
|
|
b = (b - 2) / 5; // number of bytes in payload
|
240 |
|
|
*(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
|
241 |
|
|
while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
|
245 |
|
|
}
|
246 |
|
|
else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) ||
|
247 |
|
|
(*s == '%' && t == '%')) { // entity reference
|
248 |
|
|
for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b]));
|
249 |
|
|
b += 2); // find entity in entity list
|
250 |
|
|
|
251 |
|
|
if (ent[b++]) { // found a match
|
252 |
|
|
if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) {
|
253 |
|
|
l = (d = (s - r)) + c + strlen(e); // new length
|
254 |
|
|
r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l);
|
255 |
|
|
e = strchr((s = r + d), ';'); // fix up pointers
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
memmove(s + c, e + 1, strlen(e)); // shift rest of string
|
259 |
|
|
strncpy(s, ent[b], c); // copy in replacement text
|
260 |
|
|
}
|
261 |
|
|
else s++; // not a known entity
|
262 |
|
|
}
|
263 |
|
|
else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' ';
|
264 |
|
|
else s++; // no decoding needed
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
if (t == '*') { // normalize spaces for non-cdata attributes
|
268 |
|
|
for (s = r; *s; s++) {
|
269 |
|
|
if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1);
|
270 |
|
|
while (*s && *s != ' ') s++;
|
271 |
|
|
}
|
272 |
|
|
if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space
|
273 |
|
|
}
|
274 |
|
|
return r;
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
// called when parser finds start of new tag
|
278 |
|
|
void ezxml_open_tag(ezxml_root_t root, char *name, char **attr)
|
279 |
|
|
{
|
280 |
|
|
ezxml_t xml = root->cur;
|
281 |
|
|
|
282 |
|
|
if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt));
|
283 |
|
|
else xml->name = name; // first open tag
|
284 |
|
|
|
285 |
|
|
xml->attr = attr;
|
286 |
|
|
root->cur = xml; // update tag insertion point
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
// called when parser finds character content between open and closing tag
|
290 |
|
|
void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t)
|
291 |
|
|
{
|
292 |
|
|
ezxml_t xml = root->cur;
|
293 |
|
|
char *m = s;
|
294 |
|
|
size_t l;
|
295 |
|
|
|
296 |
|
|
if (! xml || ! xml->name || ! len) return; // sanity check
|
297 |
|
|
|
298 |
|
|
s[len] = '\0'; // null terminate text (calling functions anticipate this)
|
299 |
|
|
len = strlen(s = ezxml_decode(s, root->ent, t)) + 1;
|
300 |
|
|
|
301 |
|
|
if (! *(xml->txt)) xml->txt = s; // initial character content
|
302 |
|
|
else { // allocate our own memory and make a copy
|
303 |
|
|
xml->txt = (xml->flags & EZXML_TXTM) // allocate some space
|
304 |
|
|
? realloc(xml->txt, (l = strlen(xml->txt)) + len)
|
305 |
|
|
: strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt);
|
306 |
|
|
strcpy(xml->txt + l, s); // add new char content
|
307 |
|
|
if (s != m) free(s); // free s if it was malloced by ezxml_decode()
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM);
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
// called when parser finds closing tag
|
314 |
|
|
ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s)
|
315 |
|
|
{
|
316 |
|
|
if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name))
|
317 |
|
|
return ezxml_err(root, s, "unexpected closing tag </%s>", name);
|
318 |
|
|
|
319 |
|
|
root->cur = root->cur->parent;
|
320 |
|
|
return NULL;
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
// checks for circular entity references, returns non-zero if no circular
|
324 |
|
|
// references are found, zero otherwise
|
325 |
|
|
int ezxml_ent_ok(char *name, char *s, char **ent)
|
326 |
|
|
{
|
327 |
|
|
int i;
|
328 |
|
|
|
329 |
|
|
for (; ; s++) {
|
330 |
|
|
while (*s && *s != '&') s++; // find next entity reference
|
331 |
|
|
if (! *s) return 1;
|
332 |
|
|
if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref.
|
333 |
|
|
for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2);
|
334 |
|
|
if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0;
|
335 |
|
|
}
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
// called when the parser finds a processing instruction
|
339 |
|
|
void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len)
|
340 |
|
|
{
|
341 |
|
|
int i = 0, j = 1;
|
342 |
|
|
char *target = s;
|
343 |
|
|
|
344 |
|
|
s[len] = '\0'; // null terminate instruction
|
345 |
|
|
if (*(s += strcspn(s, EZXML_WS))) {
|
346 |
|
|
*s = '\0'; // null terminate target
|
347 |
|
|
s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
if (! strcmp(target, "xml")) { // <?xml ... ?>
|
351 |
|
|
if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10,
|
352 |
|
|
EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1;
|
353 |
|
|
return;
|
354 |
|
|
}
|
355 |
|
|
|
356 |
|
|
if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi
|
357 |
|
|
|
358 |
|
|
while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
|
359 |
|
|
if (! root->pi[i]) { // new target
|
360 |
|
|
root->pi = realloc(root->pi, sizeof(char **) * (i + 2));
|
361 |
|
|
root->pi[i] = malloc(sizeof(char *) * 3);
|
362 |
|
|
root->pi[i][0] = target;
|
363 |
|
|
root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list
|
364 |
|
|
root->pi[i][2] = strdup(""); // empty document position list
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
while (root->pi[i][j]) j++; // find end of instruction list for this target
|
368 |
|
|
root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3));
|
369 |
|
|
root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1);
|
370 |
|
|
strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
|
371 |
|
|
root->pi[i][j + 1] = NULL; // null terminate pi list for this target
|
372 |
|
|
root->pi[i][j] = s; // set instruction
|
373 |
|
|
}
|
374 |
|
|
|
375 |
|
|
// called when the parser finds an internal doctype subset
|
376 |
|
|
short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len)
|
377 |
|
|
{
|
378 |
|
|
char q, *c, *t, *n = NULL, *v, **ent, **pe;
|
379 |
|
|
int i, j;
|
380 |
|
|
|
381 |
|
|
pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL));
|
382 |
|
|
|
383 |
|
|
for (s[len] = '\0'; s; ) {
|
384 |
|
|
while (*s && *s != '<' && *s != '%') s++; // find next declaration
|
385 |
|
|
|
386 |
|
|
if (! *s) break;
|
387 |
|
|
else if (! strncmp(s, "<!ENTITY", 8)) { // parse entity definitions
|
388 |
|
|
c = s += strspn(s + 8, EZXML_WS) + 8; // skip white space separator
|
389 |
|
|
n = s + strspn(s, EZXML_WS "%"); // find name
|
390 |
|
|
*(s = n + strcspn(n, EZXML_WS)) = ';'; // append ; to name
|
391 |
|
|
|
392 |
|
|
v = s + strspn(s + 1, EZXML_WS) + 1; // find value
|
393 |
|
|
if ((q = *(v++)) != '"' && q != '\'') { // skip externals
|
394 |
|
|
s = strchr(s, '>');
|
395 |
|
|
continue;
|
396 |
|
|
}
|
397 |
|
|
|
398 |
|
|
for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
|
399 |
|
|
ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent
|
400 |
|
|
if (*c == '%') pe = ent;
|
401 |
|
|
else root->ent = ent;
|
402 |
|
|
|
403 |
|
|
*(++s) = '\0'; // null terminate name
|
404 |
|
|
if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value
|
405 |
|
|
ent[i + 1] = ezxml_decode(v, pe, '%'); // set value
|
406 |
|
|
ent[i + 2] = NULL; // null terminate entity list
|
407 |
|
|
if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference
|
408 |
|
|
if (ent[i + 1] != v) free(ent[i + 1]);
|
409 |
|
|
ezxml_err(root, v, "circular entity declaration &%s", n);
|
410 |
|
|
break;
|
411 |
|
|
}
|
412 |
|
|
else ent[i] = n; // set entity name
|
413 |
|
|
}
|
414 |
|
|
else if (! strncmp(s, "<!ATTLIST", 9)) { // parse default attributes
|
415 |
|
|
t = s + strspn(s + 9, EZXML_WS) + 9; // skip whitespace separator
|
416 |
|
|
if (! *t) { ezxml_err(root, t, "unclosed <!ATTLIST"); break; }
|
417 |
|
|
if (*(s = t + strcspn(t, EZXML_WS ">")) == '>') continue;
|
418 |
|
|
else *s = '\0'; // null terminate tag name
|
419 |
|
|
for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
|
420 |
|
|
|
421 |
|
|
while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') {
|
422 |
|
|
if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name
|
423 |
|
|
else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
|
424 |
|
|
|
425 |
|
|
s += strspn(s + 1, EZXML_WS) + 1; // find next token
|
426 |
|
|
c = (strncmp(s, "CDATA", 5)) ? "*" : " "; // is it cdata?
|
427 |
|
|
if (! strncmp(s, "NOTATION", 8))
|
428 |
|
|
s += strspn(s + 8, EZXML_WS) + 8;
|
429 |
|
|
s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, EZXML_WS);
|
430 |
|
|
if (! s) { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
|
431 |
|
|
|
432 |
|
|
s += strspn(s, EZXML_WS ")"); // skip white space separator
|
433 |
|
|
if (! strncmp(s, "#FIXED", 6))
|
434 |
|
|
s += strspn(s + 6, EZXML_WS) + 6;
|
435 |
|
|
if (*s == '#') { // no default value
|
436 |
|
|
s += strcspn(s, EZXML_WS ">") - 1;
|
437 |
|
|
if (*c == ' ') continue; // cdata is default, nothing to do
|
438 |
|
|
v = NULL;
|
439 |
|
|
}
|
440 |
|
|
else if ((*s == '"' || *s == '\'') && // default value
|
441 |
|
|
(s = strchr(v = s + 1, *s))) *s = '\0';
|
442 |
|
|
else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
|
443 |
|
|
|
444 |
|
|
if (! root->attr[i]) { // new tag name
|
445 |
|
|
root->attr = (! i) ? malloc(2 * sizeof(char **))
|
446 |
|
|
: realloc(root->attr,
|
447 |
|
|
(i + 2) * sizeof(char **));
|
448 |
|
|
root->attr[i] = malloc(2 * sizeof(char *));
|
449 |
|
|
root->attr[i][0] = t; // set tag name
|
450 |
|
|
root->attr[i][1] = (char *)(root->attr[i + 1] = NULL);
|
451 |
|
|
}
|
452 |
|
|
|
453 |
|
|
for (j = 1; root->attr[i][j]; j += 3); // find end of list
|
454 |
|
|
root->attr[i] = realloc(root->attr[i],
|
455 |
|
|
(j + 4) * sizeof(char *));
|
456 |
|
|
|
457 |
|
|
root->attr[i][j + 3] = NULL; // null terminate list
|
458 |
|
|
root->attr[i][j + 2] = c; // is it cdata?
|
459 |
|
|
root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c)
|
460 |
|
|
: NULL;
|
461 |
|
|
root->attr[i][j] = n; // attribute name
|
462 |
|
|
}
|
463 |
|
|
}
|
464 |
|
|
else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); // comments
|
465 |
|
|
else if (! strncmp(s, "<?", 2)) { // processing instructions
|
466 |
|
|
if ((s = strstr(c = s + 2, "?>")))
|
467 |
|
|
ezxml_proc_inst(root, c, s++ - c);
|
468 |
|
|
}
|
469 |
|
|
else if (*s == '<') s = strchr(s, '>'); // skip other declarations
|
470 |
|
|
else if (*(s++) == '%' && ! root->standalone) break;
|
471 |
|
|
}
|
472 |
|
|
|
473 |
|
|
free(pe);
|
474 |
|
|
return ! *root->err;
|
475 |
|
|
}
|
476 |
|
|
|
477 |
|
|
// Converts a UTF-16 string to UTF-8. Returns a new string that must be freed
|
478 |
|
|
// or NULL if no conversion was needed.
|
479 |
|
|
char *ezxml_str2utf8(char **s, size_t *len)
|
480 |
|
|
{
|
481 |
|
|
char *u;
|
482 |
|
|
size_t l = 0, sl, max = *len;
|
483 |
|
|
long c, d;
|
484 |
|
|
int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
|
485 |
|
|
|
486 |
|
|
if (be == -1) return NULL; // not UTF-16
|
487 |
|
|
|
488 |
|
|
u = malloc(max);
|
489 |
|
|
for (sl = 2; sl < *len - 1; sl += 2) {
|
490 |
|
|
c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE
|
491 |
|
|
: (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE
|
492 |
|
|
if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half
|
493 |
|
|
d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)
|
494 |
|
|
: (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF);
|
495 |
|
|
c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;
|
496 |
|
|
}
|
497 |
|
|
|
498 |
|
|
while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE);
|
499 |
|
|
if (c < 0x80) u[l++] = c; // US-ASCII subset
|
500 |
|
|
else { // multi-byte UTF-8 sequence
|
501 |
|
|
for (b = 0, d = c; d; d /= 2) b++; // bits in c
|
502 |
|
|
b = (b - 2) / 5; // bytes in payload
|
503 |
|
|
u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head
|
504 |
|
|
while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
|
505 |
|
|
}
|
506 |
|
|
}
|
507 |
|
|
return *s = realloc(u, *len = l);
|
508 |
|
|
}
|
509 |
|
|
|
510 |
|
|
// frees a tag attribute list
|
511 |
|
|
void ezxml_free_attr(char **attr) {
|
512 |
|
|
int i = 0;
|
513 |
|
|
char *m;
|
514 |
|
|
|
515 |
|
|
if (! attr || attr == EZXML_NIL) return; // nothing to free
|
516 |
|
|
while (attr[i]) i += 2; // find end of attribute list
|
517 |
|
|
m = attr[i + 1]; // list of which names and values are malloced
|
518 |
|
|
for (i = 0; m[i]; i++) {
|
519 |
|
|
if (m[i] & EZXML_NAMEM) free(attr[i * 2]);
|
520 |
|
|
if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]);
|
521 |
|
|
}
|
522 |
|
|
free(m);
|
523 |
|
|
free(attr);
|
524 |
|
|
}
|
525 |
|
|
|
526 |
|
|
// parse the given xml string and return an ezxml structure
|
527 |
|
|
ezxml_t ezxml_parse_str(char *s, size_t len)
|
528 |
|
|
{
|
529 |
|
|
ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL);
|
530 |
|
|
char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning
|
531 |
|
|
int l, i, j;
|
532 |
|
|
|
533 |
|
|
root->m = s;
|
534 |
|
|
if (! len) return ezxml_err(root, s, "root tag missing");
|
535 |
|
|
root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8
|
536 |
|
|
root->e = (root->s = s) + len; // record start and end of work area
|
537 |
|
|
|
538 |
|
|
e = s[len - 1]; // save end char
|
539 |
|
|
s[len - 1] = '\0'; // turn end char into null terminator
|
540 |
|
|
|
541 |
|
|
while (*s && *s != '<') s++; // find first tag
|
542 |
|
|
if (! *s) return ezxml_err(root, s, "root tag missing");
|
543 |
|
|
|
544 |
|
|
for (; ; ) {
|
545 |
|
|
attr = (char **)EZXML_NIL;
|
546 |
|
|
d = ++s;
|
547 |
|
|
|
548 |
|
|
if (isalpha(*s) || *s == '_' || *s == ':') { // new tag
|
549 |
|
|
if (! root->cur)
|
550 |
|
|
return ezxml_err(root, d, "markup outside of root element");
|
551 |
|
|
|
552 |
|
|
s += strcspn(s, EZXML_WS "/>");
|
553 |
|
|
while (isspace(*s)) *(s++) = '\0'; // null terminate tag name
|
554 |
|
|
|
555 |
|
|
if (*s && *s != '/' && *s != '>') // find tag in default attr list
|
556 |
|
|
for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
|
557 |
|
|
|
558 |
|
|
for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib
|
559 |
|
|
attr = (l) ? realloc(attr, (l + 4) * sizeof(char *))
|
560 |
|
|
: malloc(4 * sizeof(char *)); // allocate space
|
561 |
|
|
attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2)
|
562 |
|
|
: malloc(2); // mem for list of maloced vals
|
563 |
|
|
strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced
|
564 |
|
|
attr[l + 2] = NULL; // null terminate list
|
565 |
|
|
attr[l + 1] = ""; // temporary attribute value
|
566 |
|
|
attr[l] = s; // set attribute name
|
567 |
|
|
|
568 |
|
|
s += strcspn(s, EZXML_WS "=/>");
|
569 |
|
|
if (*s == '=' || isspace(*s)) {
|
570 |
|
|
*(s++) = '\0'; // null terminate tag attribute name
|
571 |
|
|
q = *(s += strspn(s, EZXML_WS "="));
|
572 |
|
|
if (q == '"' || q == '\'') { // attribute value
|
573 |
|
|
attr[l + 1] = ++s;
|
574 |
|
|
while (*s && *s != q) s++;
|
575 |
|
|
if (*s) *(s++) = '\0'; // null terminate attribute val
|
576 |
|
|
else {
|
577 |
|
|
ezxml_free_attr(attr);
|
578 |
|
|
return ezxml_err(root, d, "missing %c", q);
|
579 |
|
|
}
|
580 |
|
|
|
581 |
|
|
for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3);
|
582 |
|
|
attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a
|
583 |
|
|
&& a[j]) ? *a[j + 2] : ' ');
|
584 |
|
|
if (attr[l + 1] < d || attr[l + 1] > s)
|
585 |
|
|
attr[l + 3][l / 2] = EZXML_TXTM; // value malloced
|
586 |
|
|
}
|
587 |
|
|
}
|
588 |
|
|
while (isspace(*s)) s++;
|
589 |
|
|
}
|
590 |
|
|
|
591 |
|
|
if (*s == '/') { // self closing tag
|
592 |
|
|
*(s++) = '\0';
|
593 |
|
|
if ((*s && *s != '>') || (! *s && e != '>')) {
|
594 |
|
|
if (l) ezxml_free_attr(attr);
|
595 |
|
|
return ezxml_err(root, d, "missing >");
|
596 |
|
|
}
|
597 |
|
|
ezxml_open_tag(root, d, attr);
|
598 |
|
|
ezxml_close_tag(root, d, s);
|
599 |
|
|
}
|
600 |
|
|
else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag
|
601 |
|
|
*s = '\0'; // temporarily null terminate tag name
|
602 |
|
|
ezxml_open_tag(root, d, attr);
|
603 |
|
|
*s = q;
|
604 |
|
|
}
|
605 |
|
|
else {
|
606 |
|
|
if (l) ezxml_free_attr(attr);
|
607 |
|
|
return ezxml_err(root, d, "missing >");
|
608 |
|
|
}
|
609 |
|
|
}
|
610 |
|
|
else if (*s == '/') { // close tag
|
611 |
|
|
s += strcspn(d = s + 1, EZXML_WS ">") + 1;
|
612 |
|
|
if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >");
|
613 |
|
|
*s = '\0'; // temporarily null terminate tag name
|
614 |
|
|
if (ezxml_close_tag(root, d, s)) return &root->xml;
|
615 |
|
|
if (isspace(*s = q)) s += strspn(s, EZXML_WS);
|
616 |
|
|
}
|
617 |
|
|
else if (! strncmp(s, "!--", 3)) { // comment
|
618 |
|
|
if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) ||
|
619 |
|
|
(! *s && e != '>')) return ezxml_err(root, d, "unclosed <!--");
|
620 |
|
|
}
|
621 |
|
|
else if (! strncmp(s, "![CDATA[", 8)) { // cdata
|
622 |
|
|
if ((s = strstr(s, "]]>")))
|
623 |
|
|
ezxml_char_content(root, d + 8, (s += 2) - d - 10, 'c');
|
624 |
|
|
else return ezxml_err(root, d, "unclosed <![CDATA[");
|
625 |
|
|
}
|
626 |
|
|
else if (! strncmp(s, "!DOCTYPE", 8)) { // dtd
|
627 |
|
|
for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' ||
|
628 |
|
|
*(s + strspn(s + 1, EZXML_WS) + 1) != '>')));
|
629 |
|
|
l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
|
630 |
|
|
if (! *s && e != '>')
|
631 |
|
|
return ezxml_err(root, d, "unclosed <!DOCTYPE");
|
632 |
|
|
d = (l) ? strchr(d, '[') + 1 : d;
|
633 |
|
|
if (l && ! ezxml_internal_dtd(root, d, s++ - d)) return &root->xml;
|
634 |
|
|
}
|
635 |
|
|
else if (*s == '?') { // <?...?> processing instructions
|
636 |
|
|
do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
|
637 |
|
|
if (! s || (! *s && e != '>'))
|
638 |
|
|
return ezxml_err(root, d, "unclosed <?");
|
639 |
|
|
else ezxml_proc_inst(root, d + 1, s - d - 2);
|
640 |
|
|
}
|
641 |
|
|
else return ezxml_err(root, d, "unexpected <");
|
642 |
|
|
|
643 |
|
|
if (! s || ! *s) break;
|
644 |
|
|
*s = '\0';
|
645 |
|
|
d = ++s;
|
646 |
|
|
if (*s && *s != '<') { // tag character content
|
647 |
|
|
while (*s && *s != '<') s++;
|
648 |
|
|
if (*s) ezxml_char_content(root, d, s - d, '&');
|
649 |
|
|
else break;
|
650 |
|
|
}
|
651 |
|
|
else if (! *s) break;
|
652 |
|
|
}
|
653 |
|
|
|
654 |
|
|
if (! root->cur) return &root->xml;
|
655 |
|
|
else if (! root->cur->name) return ezxml_err(root, d, "root tag missing");
|
656 |
|
|
else return ezxml_err(root, d, "unclosed tag <%s>", root->cur->name);
|
657 |
|
|
}
|
658 |
|
|
|
659 |
|
|
#ifdef CYGPKG_IO_FILEIO
|
660 |
|
|
// Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
|
661 |
|
|
// stream into memory and then parses it. For xml files, use ezxml_parse_file()
|
662 |
|
|
// or ezxml_parse_fd()
|
663 |
|
|
ezxml_t ezxml_parse_fp(FILE *fp)
|
664 |
|
|
{
|
665 |
|
|
ezxml_root_t root;
|
666 |
|
|
size_t l, len = 0;
|
667 |
|
|
char *s;
|
668 |
|
|
|
669 |
|
|
if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
|
670 |
|
|
do {
|
671 |
|
|
len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
|
672 |
|
|
if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
|
673 |
|
|
} while (s && l == EZXML_BUFSIZE);
|
674 |
|
|
|
675 |
|
|
if (! s) return NULL;
|
676 |
|
|
root = (ezxml_root_t)ezxml_parse_str(s, len);
|
677 |
|
|
root->len = -1; // so we know to free s in ezxml_free()
|
678 |
|
|
return &root->xml;
|
679 |
|
|
}
|
680 |
|
|
|
681 |
|
|
// A wrapper for ezxml_parse_str() that accepts a file descriptor. First
|
682 |
|
|
// attempts to mem map the file. Failing that, reads the file into memory.
|
683 |
|
|
// Returns NULL on failure.
|
684 |
|
|
ezxml_t ezxml_parse_fd(int fd)
|
685 |
|
|
{
|
686 |
|
|
ezxml_root_t root;
|
687 |
|
|
struct stat st;
|
688 |
|
|
size_t l;
|
689 |
|
|
void *m;
|
690 |
|
|
|
691 |
|
|
if (fd < 0) return NULL;
|
692 |
|
|
fstat(fd, &st);
|
693 |
|
|
|
694 |
|
|
#ifndef EZXML_NOMMAP
|
695 |
|
|
l = (st.st_size + sysconf(_SC_PAGESIZE) - 1) & ~(sysconf(_SC_PAGESIZE) -1);
|
696 |
|
|
if ((m = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
|
697 |
|
|
MAP_FAILED) {
|
698 |
|
|
madvise(m, l, MADV_SEQUENTIAL); // optimize for sequential access
|
699 |
|
|
root = (ezxml_root_t)ezxml_parse_str(m, st.st_size);
|
700 |
|
|
madvise(m, root->len = l, MADV_NORMAL); // put it back to normal
|
701 |
|
|
}
|
702 |
|
|
else { // mmap failed, read file into memory
|
703 |
|
|
#endif // EZXML_NOMMAP
|
704 |
|
|
l = read(fd, m = malloc(st.st_size), st.st_size);
|
705 |
|
|
root = (ezxml_root_t)ezxml_parse_str(m, l);
|
706 |
|
|
root->len = -1; // so we know to free s in ezxml_free()
|
707 |
|
|
#ifndef EZXML_NOMMAP
|
708 |
|
|
}
|
709 |
|
|
#endif // EZXML_NOMMAP
|
710 |
|
|
return &root->xml;
|
711 |
|
|
}
|
712 |
|
|
|
713 |
|
|
// a wrapper for ezxml_parse_fd that accepts a file name
|
714 |
|
|
ezxml_t ezxml_parse_file(const char *file)
|
715 |
|
|
{
|
716 |
|
|
int fd = open(file, O_RDONLY, 0);
|
717 |
|
|
ezxml_t xml = ezxml_parse_fd(fd);
|
718 |
|
|
|
719 |
|
|
if (fd >= 0) close(fd);
|
720 |
|
|
return xml;
|
721 |
|
|
}
|
722 |
|
|
#endif // CYGPKG_IO_FILEIO
|
723 |
|
|
|
724 |
|
|
// Encodes ampersand sequences appending the results to *dst, reallocating *dst
|
725 |
|
|
// if length excedes max. a is non-zero for attribute encoding. Returns *dst
|
726 |
|
|
char *ezxml_ampencode(const char *s, size_t len, char **dst, size_t *dlen,
|
727 |
|
|
size_t *max, short a)
|
728 |
|
|
{
|
729 |
|
|
const char *e;
|
730 |
|
|
|
731 |
|
|
for (e = s + len; s != e; s++) {
|
732 |
|
|
while (*dlen + 10 > *max) *dst = realloc(*dst, *max += EZXML_BUFSIZE);
|
733 |
|
|
|
734 |
|
|
switch (*s) {
|
735 |
|
|
case '\0': return *dst;
|
736 |
|
|
case '&': *dlen += sprintf(*dst + *dlen, "&"); break;
|
737 |
|
|
case '<': *dlen += sprintf(*dst + *dlen, "<"); break;
|
738 |
|
|
case '>': *dlen += sprintf(*dst + *dlen, ">"); break;
|
739 |
|
|
case '"': *dlen += sprintf(*dst + *dlen, (a) ? """ : "\""); break;
|
740 |
|
|
case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "
" : "\n"); break;
|
741 |
|
|
case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "	" : "\t"); break;
|
742 |
|
|
case '\r': *dlen += sprintf(*dst + *dlen, "
"); break;
|
743 |
|
|
default: (*dst)[(*dlen)++] = *s;
|
744 |
|
|
}
|
745 |
|
|
}
|
746 |
|
|
return *dst;
|
747 |
|
|
}
|
748 |
|
|
|
749 |
|
|
// Recursively converts each tag to xml appending it to *s. Reallocates *s if
|
750 |
|
|
// its length excedes max. start is the location of the previous tag in the
|
751 |
|
|
// parent tag's character content. Returns *s.
|
752 |
|
|
char *ezxml_toxml_r(ezxml_t xml, char **s, size_t *len, size_t *max,
|
753 |
|
|
size_t start, char ***attr)
|
754 |
|
|
{
|
755 |
|
|
int i, j;
|
756 |
|
|
char *txt = (xml->parent) ? xml->parent->txt : "";
|
757 |
|
|
size_t off = 0;
|
758 |
|
|
|
759 |
|
|
// parent character content up to this tag
|
760 |
|
|
*s = ezxml_ampencode(txt + start, xml->off - start, s, len, max, 0);
|
761 |
|
|
|
762 |
|
|
while (*len + strlen(xml->name) + 4 > *max) // reallocate s
|
763 |
|
|
*s = realloc(*s, *max += EZXML_BUFSIZE);
|
764 |
|
|
|
765 |
|
|
*len += sprintf(*s + *len, "<%s", xml->name); // open tag
|
766 |
|
|
for (i = 0; xml->attr[i]; i += 2) { // tag attributes
|
767 |
|
|
if (ezxml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
|
768 |
|
|
while (*len + strlen(xml->attr[i]) + 7 > *max) // reallocate s
|
769 |
|
|
*s = realloc(*s, *max += EZXML_BUFSIZE);
|
770 |
|
|
|
771 |
|
|
*len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
|
772 |
|
|
ezxml_ampencode(xml->attr[i + 1], -1, s, len, max, 1);
|
773 |
|
|
*len += sprintf(*s + *len, "\"");
|
774 |
|
|
}
|
775 |
|
|
|
776 |
|
|
for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
|
777 |
|
|
for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes
|
778 |
|
|
if (! attr[i][j + 1] || ezxml_attr(xml, attr[i][j]) != attr[i][j + 1])
|
779 |
|
|
continue; // skip duplicates and non-values
|
780 |
|
|
while (*len + strlen(attr[i][j]) + 7 > *max) // reallocate s
|
781 |
|
|
*s = realloc(*s, *max += EZXML_BUFSIZE);
|
782 |
|
|
|
783 |
|
|
*len += sprintf(*s + *len, " %s=\"", attr[i][j]);
|
784 |
|
|
ezxml_ampencode(attr[i][j + 1], -1, s, len, max, 1);
|
785 |
|
|
*len += sprintf(*s + *len, "\"");
|
786 |
|
|
}
|
787 |
|
|
*len += sprintf(*s + *len, ">");
|
788 |
|
|
|
789 |
|
|
*s = (xml->child) ? ezxml_toxml_r(xml->child, s, len, max, 0, attr) //child
|
790 |
|
|
: ezxml_ampencode(xml->txt, -1, s, len, max, 0); //data
|
791 |
|
|
|
792 |
|
|
while (*len + strlen(xml->name) + 4 > *max) // reallocate s
|
793 |
|
|
*s = realloc(*s, *max += EZXML_BUFSIZE);
|
794 |
|
|
|
795 |
|
|
*len += sprintf(*s + *len, "</%s>", xml->name); // close tag
|
796 |
|
|
|
797 |
|
|
while (txt[off] && off < xml->off) off++; // make sure off is within bounds
|
798 |
|
|
return (xml->ordered) ? ezxml_toxml_r(xml->ordered, s, len, max, off, attr)
|
799 |
|
|
: ezxml_ampencode(txt + off, -1, s, len, max, 0);
|
800 |
|
|
}
|
801 |
|
|
|
802 |
|
|
// converts an ezxml structure back to xml, returning it as a string that must
|
803 |
|
|
// be freed
|
804 |
|
|
char *ezxml_toxml(ezxml_t xml)
|
805 |
|
|
{
|
806 |
|
|
ezxml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL;
|
807 |
|
|
ezxml_root_t root = (ezxml_root_t)xml;
|
808 |
|
|
size_t len = 0, max = EZXML_BUFSIZE;
|
809 |
|
|
char *s = strcpy(malloc(max), ""), *t, *n;
|
810 |
|
|
int i, j, k;
|
811 |
|
|
|
812 |
|
|
if (! xml || ! xml->name) return realloc(s, len + 1);
|
813 |
|
|
while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
|
814 |
|
|
|
815 |
|
|
for (i = 0; ! p && root->pi[i]; i++) { // pre-root processing instructions
|
816 |
|
|
for (k = 2; root->pi[i][k - 1]; k++);
|
817 |
|
|
for (j = 1; (n = root->pi[i][j]); j++) {
|
818 |
|
|
if (root->pi[i][k][j - 1] == '>') continue; // not pre-root
|
819 |
|
|
while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
|
820 |
|
|
s = realloc(s, max += EZXML_BUFSIZE);
|
821 |
|
|
len += sprintf(s + len, "<?%s%s%s?>\n", t, *n ? " " : "", n);
|
822 |
|
|
}
|
823 |
|
|
}
|
824 |
|
|
|
825 |
|
|
xml->parent = xml->ordered = NULL;
|
826 |
|
|
s = ezxml_toxml_r(xml, &s, &len, &max, 0, root->attr);
|
827 |
|
|
xml->parent = p;
|
828 |
|
|
xml->ordered = o;
|
829 |
|
|
|
830 |
|
|
for (i = 0; ! p && root->pi[i]; i++) { // post-root processing instructions
|
831 |
|
|
for (k = 2; root->pi[i][k - 1]; k++);
|
832 |
|
|
for (j = 1; (n = root->pi[i][j]); j++) {
|
833 |
|
|
if (root->pi[i][k][j - 1] == '<') continue; // not post-root
|
834 |
|
|
while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
|
835 |
|
|
s = realloc(s, max += EZXML_BUFSIZE);
|
836 |
|
|
len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
|
837 |
|
|
}
|
838 |
|
|
}
|
839 |
|
|
return realloc(s, len + 1);
|
840 |
|
|
}
|
841 |
|
|
|
842 |
|
|
// free the memory allocated for the ezxml structure
|
843 |
|
|
void ezxml_free(ezxml_t xml)
|
844 |
|
|
{
|
845 |
|
|
ezxml_root_t root = (ezxml_root_t)xml;
|
846 |
|
|
int i, j;
|
847 |
|
|
char **a, *s;
|
848 |
|
|
|
849 |
|
|
if (! xml) return;
|
850 |
|
|
ezxml_free(xml->child);
|
851 |
|
|
ezxml_free(xml->ordered);
|
852 |
|
|
|
853 |
|
|
if (! xml->parent) { // free root tag allocations
|
854 |
|
|
for (i = 10; root->ent[i]; i += 2) // 0 - 9 are default entites (<>&"')
|
855 |
|
|
if ((s = root->ent[i + 1]) < root->s || s > root->e) free(s);
|
856 |
|
|
free(root->ent); // free list of general entities
|
857 |
|
|
|
858 |
|
|
for (i = 0; (a = root->attr[i]); i++) {
|
859 |
|
|
for (j = 1; a[j++]; j += 2) // free malloced attribute values
|
860 |
|
|
if (a[j] && (a[j] < root->s || a[j] > root->e)) free(a[j]);
|
861 |
|
|
free(a);
|
862 |
|
|
}
|
863 |
|
|
if (root->attr[0]) free(root->attr); // free default attribute list
|
864 |
|
|
|
865 |
|
|
for (i = 0; root->pi[i]; i++) {
|
866 |
|
|
for (j = 1; root->pi[i][j]; j++);
|
867 |
|
|
free(root->pi[i][j + 1]);
|
868 |
|
|
free(root->pi[i]);
|
869 |
|
|
}
|
870 |
|
|
if (root->pi[0]) free(root->pi); // free processing instructions
|
871 |
|
|
|
872 |
|
|
if (root->len == -1) free(root->m); // malloced xml data
|
873 |
|
|
#ifndef EZXML_NOMMAP
|
874 |
|
|
else if (root->len) munmap(root->m, root->len); // mem mapped xml data
|
875 |
|
|
#endif // EZXML_NOMMAP
|
876 |
|
|
if (root->u) free(root->u); // utf8 conversion
|
877 |
|
|
}
|
878 |
|
|
|
879 |
|
|
ezxml_free_attr(xml->attr); // tag attributes
|
880 |
|
|
if ((xml->flags & EZXML_TXTM)) free(xml->txt); // character content
|
881 |
|
|
if ((xml->flags & EZXML_NAMEM)) free(xml->name); // tag name
|
882 |
|
|
free(xml);
|
883 |
|
|
}
|
884 |
|
|
|
885 |
|
|
// return parser error message or empty string if none
|
886 |
|
|
const char *ezxml_error(ezxml_t xml)
|
887 |
|
|
{
|
888 |
|
|
while (xml && xml->parent) xml = xml->parent; // find root tag
|
889 |
|
|
return (xml) ? ((ezxml_root_t)xml)->err : "";
|
890 |
|
|
}
|
891 |
|
|
|
892 |
|
|
// returns a new empty ezxml structure with the given root tag name
|
893 |
|
|
ezxml_t ezxml_new(const char *name)
|
894 |
|
|
{
|
895 |
|
|
static char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """,
|
896 |
|
|
"apos;", "'", "amp;", "&", NULL };
|
897 |
|
|
ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)),
|
898 |
|
|
'\0', sizeof(struct ezxml_root));
|
899 |
|
|
root->xml.name = (char *)name;
|
900 |
|
|
root->cur = &root->xml;
|
901 |
|
|
strcpy(root->err, root->xml.txt = "");
|
902 |
|
|
root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
|
903 |
|
|
root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL);
|
904 |
|
|
return &root->xml;
|
905 |
|
|
}
|
906 |
|
|
|
907 |
|
|
// Adds a child tag. off is the offset of the child tag relative to the start
|
908 |
|
|
// of the parent tag's character content. returns the child tag
|
909 |
|
|
ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off)
|
910 |
|
|
{
|
911 |
|
|
ezxml_t cur, head, child;
|
912 |
|
|
|
913 |
|
|
if (! xml) return NULL;
|
914 |
|
|
child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0',
|
915 |
|
|
sizeof(struct ezxml));
|
916 |
|
|
child->name = (char *)name;
|
917 |
|
|
child->attr = EZXML_NIL;
|
918 |
|
|
child->off = off;
|
919 |
|
|
child->parent = xml;
|
920 |
|
|
child->txt = "";
|
921 |
|
|
|
922 |
|
|
if ((head = xml->child)) { // already have sub tags
|
923 |
|
|
if (head->off <= off) { // not first subtag
|
924 |
|
|
for (cur = head; cur->ordered && cur->ordered->off <= off;
|
925 |
|
|
cur = cur->ordered);
|
926 |
|
|
child->ordered = cur->ordered;
|
927 |
|
|
cur->ordered = child;
|
928 |
|
|
}
|
929 |
|
|
else { // first subtag
|
930 |
|
|
child->ordered = head;
|
931 |
|
|
xml->child = child;
|
932 |
|
|
}
|
933 |
|
|
|
934 |
|
|
for (cur = head; cur->sibling && strcmp(cur->name, name);
|
935 |
|
|
cur = cur->sibling); // find tag type
|
936 |
|
|
if (! strcmp(cur->name, name) && cur->off <= off) { //not first of type
|
937 |
|
|
while (cur->next && cur->next->off <= off) cur = cur->next;
|
938 |
|
|
child->next = cur->next;
|
939 |
|
|
cur->next = child;
|
940 |
|
|
}
|
941 |
|
|
else { // first tag of this type
|
942 |
|
|
if (cur->off > off) child->next = cur; // not only tag of this type
|
943 |
|
|
for (cur = head; cur->sibling && cur->sibling->off <= off;
|
944 |
|
|
cur = cur->sibling);
|
945 |
|
|
child->sibling = cur->sibling;
|
946 |
|
|
cur->sibling = child;
|
947 |
|
|
}
|
948 |
|
|
}
|
949 |
|
|
else xml->child = child; // only sub tag
|
950 |
|
|
|
951 |
|
|
return child;
|
952 |
|
|
}
|
953 |
|
|
|
954 |
|
|
// sets the character content for the given tag and returns the tag
|
955 |
|
|
ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt)
|
956 |
|
|
{
|
957 |
|
|
if (! xml) return NULL;
|
958 |
|
|
if (xml->flags & EZXML_TXTM) free(xml->txt); // existing txt was malloced
|
959 |
|
|
xml->flags &= ~EZXML_TXTM;
|
960 |
|
|
xml->txt = (char *)txt;
|
961 |
|
|
return xml;
|
962 |
|
|
}
|
963 |
|
|
|
964 |
|
|
// Sets the given tag attribute or adds a new attribute if not found. A value
|
965 |
|
|
// of NULL will remove the specified attribute.
|
966 |
|
|
void ezxml_set_attr(ezxml_t xml, const char *name, const char *value)
|
967 |
|
|
{
|
968 |
|
|
int l = 0, c;
|
969 |
|
|
|
970 |
|
|
if (! xml) return;
|
971 |
|
|
while (xml->attr[l] && strcmp(xml->attr[l], name)) l += 2;
|
972 |
|
|
if (! xml->attr[l]) { // not found, add as new attribute
|
973 |
|
|
if (! value) return; // nothing to do
|
974 |
|
|
if (xml->attr == EZXML_NIL) { // first attribute
|
975 |
|
|
xml->attr = malloc(4 * sizeof(char *));
|
976 |
|
|
xml->attr[1] = strdup(""); // empty list of malloced names/vals
|
977 |
|
|
}
|
978 |
|
|
else xml->attr = realloc(xml->attr, (l + 4) * sizeof(char *));
|
979 |
|
|
|
980 |
|
|
xml->attr[l] = (char *)name; // set attribute name
|
981 |
|
|
xml->attr[l + 2] = NULL; // null terminate attribute list
|
982 |
|
|
xml->attr[l + 3] = realloc(xml->attr[l + 1],
|
983 |
|
|
(c = strlen(xml->attr[l + 1])) + 2);
|
984 |
|
|
strcpy(xml->attr[l + 3] + c, " "); // set name/value as not malloced
|
985 |
|
|
if (xml->flags & EZXML_DUP) xml->attr[l + 3][c] = EZXML_NAMEM;
|
986 |
|
|
}
|
987 |
|
|
else if (xml->flags & EZXML_DUP) free((char *)name); // name was strduped
|
988 |
|
|
|
989 |
|
|
for (c = l; xml->attr[c]; c += 2); // find end of attribute list
|
990 |
|
|
if (xml->attr[c + 1][l / 2] & EZXML_TXTM) free(xml->attr[l + 1]); //old val
|
991 |
|
|
if (xml->flags & EZXML_DUP) xml->attr[c + 1][l / 2] |= EZXML_TXTM;
|
992 |
|
|
else xml->attr[c + 1][l / 2] &= ~EZXML_TXTM;
|
993 |
|
|
|
994 |
|
|
if (value) xml->attr[l + 1] = (char *)value; // set attribute value
|
995 |
|
|
else { // remove attribute
|
996 |
|
|
if (xml->attr[c + 1][l / 2] & EZXML_NAMEM) free(xml->attr[l]);
|
997 |
|
|
memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
|
998 |
|
|
xml->attr = realloc(xml->attr, (c + 2) * sizeof(char *));
|
999 |
|
|
memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
|
1000 |
|
|
(c / 2) - (l / 2)); // fix list of which name/vals are malloced
|
1001 |
|
|
}
|
1002 |
|
|
xml->flags &= ~EZXML_DUP; // clear strdup() flag
|
1003 |
|
|
}
|
1004 |
|
|
|
1005 |
|
|
// sets a flag for the given tag and returns the tag
|
1006 |
|
|
ezxml_t ezxml_set_flag(ezxml_t xml, short flag)
|
1007 |
|
|
{
|
1008 |
|
|
if (xml) xml->flags |= flag;
|
1009 |
|
|
return xml;
|
1010 |
|
|
}
|
1011 |
|
|
|
1012 |
|
|
// removes a tag along with all its subtags
|
1013 |
|
|
void ezxml_remove(ezxml_t xml)
|
1014 |
|
|
{
|
1015 |
|
|
ezxml_t cur;
|
1016 |
|
|
|
1017 |
|
|
if (! xml) return; // nothing to do
|
1018 |
|
|
if (xml->next) xml->next->sibling = xml->sibling; // patch sibling list
|
1019 |
|
|
|
1020 |
|
|
if (xml->parent) { // not root tag
|
1021 |
|
|
cur = xml->parent->child; // find head of subtag list
|
1022 |
|
|
if (cur == xml) xml->parent->child = xml->ordered; // first subtag
|
1023 |
|
|
else { // not first subtag
|
1024 |
|
|
while (cur->ordered != xml) cur = cur->ordered;
|
1025 |
|
|
cur->ordered = cur->ordered->ordered; // patch ordered list
|
1026 |
|
|
|
1027 |
|
|
cur = xml->parent->child; // go back to head of subtag list
|
1028 |
|
|
if (strcmp(cur->name, xml->name)) { // not in first sibling list
|
1029 |
|
|
while (strcmp(cur->sibling->name, xml->name))
|
1030 |
|
|
cur = cur->sibling;
|
1031 |
|
|
if (cur->sibling == xml) { // first of a sibling list
|
1032 |
|
|
cur->sibling = (xml->next) ? xml->next
|
1033 |
|
|
: cur->sibling->sibling;
|
1034 |
|
|
}
|
1035 |
|
|
else cur = cur->sibling; // not first of a sibling list
|
1036 |
|
|
}
|
1037 |
|
|
|
1038 |
|
|
while (cur->next && cur->next != xml) cur = cur->next;
|
1039 |
|
|
if (cur->next) cur->next = cur->next->next; // patch next list
|
1040 |
|
|
}
|
1041 |
|
|
}
|
1042 |
|
|
xml->ordered = NULL; // prevent ezxml_free() from clobbering ordered list
|
1043 |
|
|
ezxml_free(xml);
|
1044 |
|
|
}
|
1045 |
|
|
|
1046 |
|
|
#ifdef EZXML_TEST // test harness
|
1047 |
|
|
int main(int argc, char **argv)
|
1048 |
|
|
{
|
1049 |
|
|
ezxml_t xml;
|
1050 |
|
|
char *s;
|
1051 |
|
|
int i;
|
1052 |
|
|
|
1053 |
|
|
if (argc != 2) return fprintf(stderr, "usage: %s xmlfile\n", argv[0]);
|
1054 |
|
|
|
1055 |
|
|
xml = ezxml_parse_file(argv[1]);
|
1056 |
|
|
printf("%s\n", (s = ezxml_toxml(xml)));
|
1057 |
|
|
free(s);
|
1058 |
|
|
i = fprintf(stderr, "%s", ezxml_error(xml));
|
1059 |
|
|
ezxml_free(xml);
|
1060 |
|
|
return (i) ? 1 : 0;
|
1061 |
|
|
}
|
1062 |
|
|
#endif // EZXML_TEST
|