3 * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Modified for Palacios by Jack Lange <jarusl@cs.northwestern.edu>
31 #include <palacios/vmm_xml.h>
32 #include <palacios/vmm_sprintf.h>
34 #include <palacios/vmm.h>
36 #define V3_XML_BUFSIZE 1024 // size of internal memory buffers
38 // Flags for struct v3_xml
39 #define V3_XML_NAMEM 0x80 // name is malloced
40 #define V3_XML_TXTM 0x40 // txt is malloced
41 #define V3_XML_DUP 0x20 // attribute name and value are strduped
44 static char * V3_XML_NIL[] = { NULL }; // empty, null terminated array of strings
47 #define V3_XML_WS "\t\r\n " // whitespace
48 #define V3_XML_ERRL 128 // maximum error string length
50 struct v3_xml_root { // additional data for the root tag
51 struct v3_xml xml; // is a super-struct built on top of v3_xml struct
52 struct v3_xml * cur; // current xml tree insertion point
53 char *str_ptr; // original xml string
54 char *tmp_start; // start of work area
55 char *tmp_end; // end of work area
56 char **ent; // general entities (ampersand sequences)
57 char ***attr; // default attributes
58 short standalone; // non-zero if <?xml standalone="yes"?>
59 char err[V3_XML_ERRL]; // error string
62 static char * empty_attrib_list[] = { NULL }; // empty, null terminated array of strings
65 static void * tmp_realloc(void * old_ptr, size_t old_size, size_t new_size) {
66 void * new_buf = V3_Malloc(new_size);
68 if (new_buf == NULL) {
72 memcpy(new_buf, old_ptr, old_size);
82 // set an error string and return root
83 static void v3_xml_err(struct v3_xml_root * root, char * xml_str, const char * err, ...) {
87 char fmt[V3_XML_ERRL];
89 for (tmp = root->tmp_start; tmp < xml_str; tmp++) {
95 snprintf(fmt, V3_XML_ERRL, "[error near line %d]: %s", line, err);
98 vsnprintf(root->err, V3_XML_ERRL, fmt, ap);
101 PrintError("XML Error: %s\n", root->err);
104 v3_xml_free(&(root->xml));
111 // returns the first child tag with the given name or NULL if not found
112 struct v3_xml * v3_xml_child(struct v3_xml * xml, const char * name) {
113 struct v3_xml * child = NULL;
119 while ((child) && (strcasecmp(name, child->name) != 0)) {
120 child = child->sibling;
126 // returns the Nth tag with the same name in the same subsection or NULL if not
128 struct v3_xml * v3_xml_idx(struct v3_xml * xml, int idx) {
129 for (; xml && idx; idx--) {
136 // returns the value of the requested tag attribute or NULL if not found
137 const char * v3_xml_attr(struct v3_xml * xml, const char * attr) {
140 struct v3_xml_root * root = (struct v3_xml_root *)xml;
142 if ((!xml) || (!xml->attr)) {
146 while ((xml->attr[i]) && (strcasecmp(attr, xml->attr[i]) != 0)) {
150 if (xml->attr[i] != NULL) {
151 return xml->attr[i + 1]; // found attribute
154 while (root->xml.parent != NULL) {
155 root = (struct v3_xml_root *)root->xml.parent; // root tag
159 ( (root->attr[i] != NULL) &&
160 (strcasecmp(xml->name, root->attr[i][0]) != 0) );
163 if (! root->attr[i]) {
164 return NULL; // no matching default attributes
167 while ((root->attr[i][j] != NULL) && (strcasecmp(attr, root->attr[i][j]) != 0)) {
171 return (root->attr[i][j] != NULL) ? root->attr[i][j + 1] : NULL; // found default
174 // same as v3_xml_get but takes an already initialized va_list
175 static struct v3_xml * v3_xml_vget(struct v3_xml * xml, va_list ap) {
176 char * name = va_arg(ap, char *);
179 if ((name != NULL) && (*name != 0)) {
180 idx = va_arg(ap, int);
181 xml = v3_xml_child(xml, name);
183 return (idx < 0) ? xml : v3_xml_vget(v3_xml_idx(xml, idx), ap);
186 // Traverses the xml tree to retrieve a specific subtag. Takes a variable
187 // length list of tag names and indexes. The argument list must be terminated
188 // by either an index of -1 or an empty string tag name. Example:
189 // title = v3_xml_get(library, "shelf", 0, "book", 2, "title", -1);
190 // This retrieves the title of the 3rd book on the 1st shelf of library.
191 // Returns NULL if not found.
192 struct v3_xml * v3_xml_get(struct v3_xml * xml, ...) {
197 r = v3_xml_vget(xml, ap);
203 // sets a flag for the given tag and returns the tag
204 static struct v3_xml * v3_xml_set_flag(struct v3_xml * xml, short flag)
216 // Recursively decodes entity and character references and normalizes new lines
217 // ent is a null terminated array of alternating entity names and values. set t
218 // to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
219 // for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
220 // attribute normalization. Returns s, or if the decoded string is longer than
221 // s, returns a malloced string that must be freed.
222 static char * v3_xml_decode(char * s, char ** ent, char t) {
228 // normalize line endings
234 memmove(s, (s + 1), strlen(s));
253 if ((t != 'c') && (strncmp(s, "&#", 2) == 0)) { // character reference
255 c = strtox(s + 3, &e); // base 16
257 c = strtoi(s + 2, &e); // base 10
260 if ((!c) || (*e != ';')) {
261 // not a character ref
268 memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
269 } else if ( ( (*s == '&') &&
270 ((t == '&') || (t == ' ') || (t == '*'))) ||
271 ( (*s == '%') && (t == '%'))) {
275 (ent[b]) && (strncmp(s + 1, ent[b], strlen(ent[b])) != 0);
276 (b += 2)); // find entity in entity list
278 if (ent[b++]) { // found a match
279 if (((c = strlen(ent[b])) - 1) > ((e = strchr(s, ';')) - s)) {
280 l = (d = (s - r)) + c + strlen(e); // new length
281 r = ((r == m) ? strcpy(V3_Malloc(l), r) : tmp_realloc(r, strlen(r), l));
282 e = strchr((s = r + d), ';'); // fix up pointers
285 memmove(s + c, e + 1, strlen(e)); // shift rest of string
286 strncpy(s, ent[b], c); // copy in replacement text
288 // not a known entity
291 } else if ( ( (t == ' ') || (t == '*')) &&
295 // no decoding needed
301 // normalize spaces for non-cdata attributes
302 for (s = r; *s; s++) {
303 if ((l = strspn(s, " "))) {
304 memmove(s, s + l, strlen(s + l) + 1);
307 while ((*s) && (*s != ' ')) {
312 if ((--s >= r) && (*s == ' ')) {
313 // trim any trailing space
322 // called when parser finds character content between open and closing tag
323 static void v3_xml_char_content(struct v3_xml_root * root, char * s, size_t len, char t) {
324 struct v3_xml * xml = root->cur;
328 if ((xml == NULL) || (xml->name == NULL) || (len == 0)) {
333 s[len] = '\0'; // null terminate text (calling functions anticipate this)
334 len = strlen(s = v3_xml_decode(s, root->ent, t)) + 1;
337 // initial character content
340 // allocate our own memory and make a copy
341 xml->txt = (xml->flags & V3_XML_TXTM) ?
342 (tmp_realloc(xml->txt, strlen(xml->txt), (l = strlen(xml->txt)) + len)) :
343 (strcpy(V3_Malloc((l = strlen(xml->txt)) + len), xml->txt));
345 strcpy(xml->txt + l, s); // add new char content
348 V3_Free(s); // free s if it was malloced by v3_xml_decode()
353 v3_xml_set_flag(xml, V3_XML_TXTM);
357 // called when parser finds closing tag
358 static int v3_xml_close_tag(struct v3_xml_root * root, char * name, char * s) {
359 if ( (root->cur == NULL) ||
360 (root->cur->name == NULL) ||
361 (strcasecmp(name, root->cur->name))) {
362 v3_xml_err(root, s, "unexpected closing tag </%s>", name);
366 root->cur = root->cur->parent;
371 // checks for circular entity references, returns non-zero if no circular
372 // references are found, zero otherwise
373 static int v3_xml_ent_ok(char * name, char * s, char ** ent) {
377 while ((*s != '\0') && (*s != '&')) {
378 // find next entity reference
386 if (strncmp(s + 1, name, strlen(name)) == 0) {
391 for (i = 0; (ent[i]) && (strncmp(ent[i], s + 1, strlen(ent[i]))); i += 2);
393 if ((ent[i] != NULL) && (v3_xml_ent_ok(name, ent[i + 1], ent) == 0)) {
402 // frees a tag attribute list
403 static void v3_xml_free_attr(char **attr) {
407 if ((attr == NULL) || (attr == empty_attrib_list)) {
413 // find end of attribute list
417 m = attr[i + 1]; // list of which names and values are malloced
419 for (i = 0; m[i]; i++) {
420 if (m[i] & V3_XML_NAMEM) {
421 V3_Free(attr[i * 2]);
424 if (m[i] & V3_XML_TXTM) {
425 V3_Free(attr[(i * 2) + 1]);
438 // returns a new empty v3_xml structure with the given root tag name
439 static struct v3_xml * v3_xml_new(const char * name) {
440 static char * ent[] = { "lt;", "<", "gt;", ">", "quot;", """,
441 "apos;", "'", "amp;", "&", NULL };
443 struct v3_xml_root * root = (struct v3_xml_root *)V3_Malloc(sizeof(struct v3_xml_root));
444 memset(root, 0, sizeof(struct v3_xml_root));
446 root->xml.name = (char *)name;
447 root->cur = &root->xml;
449 memset(root->err, 0, V3_XML_ERRL);
451 root->ent = V3_Malloc(sizeof(ent));
452 memcpy(root->ent, ent, sizeof(ent));
454 root->xml.attr = empty_attrib_list;
455 root->attr = (char ***)(empty_attrib_list);
460 // inserts an existing tag into an v3_xml structure
461 struct v3_xml * v3_xml_insert(struct v3_xml * xml, struct v3_xml * dest, size_t off) {
462 struct v3_xml * cur, * prev, * head;
471 if ((head = dest->child)) {
472 // already have sub tags
474 if (head->off <= off) {
478 ((cur->ordered) && (cur->ordered->off <= off));
481 xml->ordered = cur->ordered;
490 for (cur = head, prev = NULL;
491 ((cur) && (strcasecmp(cur->name, xml->name) != 0));
492 prev = cur, cur = cur->sibling);
495 if (cur && cur->off <= off) {
498 while (cur->next && cur->next->off <= off) {
502 xml->next = cur->next;
505 // first tag of this type
509 prev->sibling = cur->sibling;
512 xml->next = cur; // old first tag is now next
514 // new sibling insert point
515 for (cur = head, prev = NULL;
516 ((cur) && (cur->off <= off));
517 prev = cur, cur = cur->sibling);
534 // Adds a child tag. off is the offset of the child tag relative to the start
535 // of the parent tag's character content. Returns the child tag.
536 static struct v3_xml * v3_xml_add_child(struct v3_xml * xml, const char * name, size_t off) {
537 struct v3_xml * child;
543 child = (struct v3_xml *)V3_Malloc(sizeof(struct v3_xml));
544 memset(child, 0, sizeof(struct v3_xml));
546 child->name = (char *)name;
547 child->attr = empty_attrib_list;
550 return v3_xml_insert(child, xml, off);
554 // called when parser finds start of new tag
555 static void v3_xml_open_tag(struct v3_xml_root * root, char * name, char ** attr) {
556 struct v3_xml * xml = root->cur;
559 xml = v3_xml_add_child(xml, name, strlen(xml->txt));
566 root->cur = xml; // update tag insertion point
575 // parse the given xml string and return an v3_xml structure
576 static struct v3_xml * parse_str(char * buf, size_t len) {
577 struct v3_xml_root * root = (struct v3_xml_root *)v3_xml_new(NULL);
582 char ** tmp_attr = NULL; // initialize a to avoid compile warning
589 v3_xml_err(root, NULL, "Empty XML String\n");
593 root->tmp_start = buf;
594 root->tmp_end = buf + len; // record start and end of work area
596 last_char = buf[len - 1]; // save end char
597 buf[len - 1] = '\0'; // turn end char into null terminator
599 while ((*buf) && (*buf != '<')) {
605 v3_xml_err(root, buf, "root tag missing");
610 attr = (char **)empty_attrib_list;
611 tag_ptr = ++buf; // skip first '<'
613 if (isalpha(*buf) || (*buf == '_') || (*buf == ':') || (*buf < '\0')) {
616 if (root->cur == NULL) {
617 v3_xml_err(root, tag_ptr, "markup outside of root element");
621 buf += strcspn(buf, V3_XML_WS "/>");
623 while (isspace(*buf)) {
624 // null terminate tag name,
625 // this writes '\0' to spaces after first tag
629 // check if attribute follows tag
630 if ((*buf) && (*buf != '/') && (*buf != '>')) {
631 // there is an attribute
632 // find attributes for correct tag
634 ((tmp_attr = root->attr[i]) &&
635 (strcasecmp(tmp_attr[0], tag_ptr) != 0));
638 // 'tmp_attr' now points to the attribute list associated with 'tag_ptr'
641 // attributes are name value pairs,
642 // 2nd to last entry is null (end of list)
643 // last entry points to a string map marking whether values have been malloced...
644 // loop through attributes until hitting the closing bracket
646 (*buf) && (*buf != '/') && (*buf != '>');
648 // buf is incremented later on
650 int attr_cnt = (attr_idx / 2) + 1;
651 int val_idx = attr_idx + 1;
652 int term_idx = attr_idx + 2;
653 int last_idx = attr_idx + 3;
655 // attr = allocated space
656 // attr[val_idx] = mem for list of maloced vals
658 attr = tmp_realloc(attr,
659 (((attr_cnt - 1) * (2 * sizeof(char *))) +
660 (2 * sizeof(char *))),
661 ((attr_cnt * (2 * sizeof(char *))) +
662 (2 * sizeof(char *))));
664 attr[last_idx] = tmp_realloc(attr[last_idx - 2],
668 attr = V3_Malloc(4 * sizeof(char *));
669 attr[last_idx] = V3_Malloc(2);
673 attr[attr_idx] = buf; // set attribute name
674 attr[val_idx] = ""; // temporary attribute value
675 attr[term_idx] = NULL; // null terminate list
676 strcpy(attr[last_idx] + attr_cnt, " "); // value is not malloc'd, offset into the stringmap
678 buf += strcspn(buf, V3_XML_WS "=/>");
680 if ((*buf == '=') || isspace(*buf)) {
682 *(buf++) = '\0'; // null terminate tag attribute name
684 // eat whitespace (and more multiple '=' ?)
685 buf += strspn(buf, V3_XML_WS "=");
689 if ((quote_char == '"') || (quote_char == '\'')) { // attribute value
690 attr[val_idx] = ++buf;
692 while ((*buf) && (*buf != quote_char)) {
697 // null terminate attribute val
700 v3_xml_free_attr(attr);
701 v3_xml_err(root, tag_ptr, "missing %c", quote_char);
706 ( (tmp_attr) && (tmp_attr[j]) &&
707 (strcasecmp(tmp_attr[j], attr[attr_idx]) != 0));
710 attr[val_idx] = v3_xml_decode(attr[val_idx], root->ent,
711 ((tmp_attr && tmp_attr[j]) ?
715 if ( (attr[val_idx] < tag_ptr) ||
716 (attr[val_idx] > buf) ) {
717 attr[last_idx][attr_cnt - 1] = V3_XML_TXTM; // value malloced
722 while (isspace(*buf)) {
731 if ( ((*buf) && (*buf != '>')) ||
732 ((!*buf) && (last_char != '>'))) {
735 v3_xml_free_attr(attr);
737 v3_xml_err(root, tag_ptr, "missing >");
740 v3_xml_open_tag(root, tag_ptr, attr);
741 v3_xml_close_tag(root, tag_ptr, buf);
742 } else if (((quote_char = *buf) == '>') ||
743 ((!*buf) && (last_char == '>'))) {
745 *buf = '\0'; // temporarily null terminate tag name
746 v3_xml_open_tag(root, tag_ptr, attr);
750 v3_xml_free_attr(attr);
752 v3_xml_err(root, tag_ptr, "missing >");
755 } else if (*buf == '/') {
758 buf += strcspn(tag_ptr = buf + 1, V3_XML_WS ">") + 1;
761 if ((*buf == '\0') && (last_char != '>')) {
762 v3_xml_err(root, tag_ptr, "missing >");
766 *buf = '\0'; // temporarily null terminate tag name
768 if (v3_xml_close_tag(root, tag_ptr, buf) == -1) {
774 buf += strspn(buf, V3_XML_WS);
776 } else if (strncmp(buf, "!--", 3) == 0) {
778 if ( ((buf = strstr(buf + 3, "--")) == 0) ||
779 ((*(buf += 2) != '>') && (*buf)) ||
780 ((!*buf) && (last_char != '>'))) {
781 v3_xml_err(root, tag_ptr, "unclosed <!--");
784 } else if (! strncmp(buf, "![CDATA[", 8)) {
786 if ((buf = strstr(buf, "]]>"))) {
787 v3_xml_char_content(root, tag_ptr + 8, (buf += 2) - tag_ptr - 10, 'c');
789 v3_xml_err(root, tag_ptr, "unclosed <![CDATA[");
793 v3_xml_err(root, tag_ptr, "unexpected <");
797 if (! buf || ! *buf) {
804 if (*buf && (*buf != '<')) {
805 // tag character content
806 while (*buf && (*buf != '<')) {
811 v3_xml_char_content(root, tag_ptr, buf - tag_ptr, '&');
815 } else if (*buf == '\0') {
820 if (root->cur == NULL) {
822 } else if (root->cur->name == NULL) {
823 v3_xml_err(root, tag_ptr, "root tag missing");
826 v3_xml_err(root, tag_ptr, "unclosed tag <%s>", root->cur->name);
832 struct v3_xml * v3_xml_parse(char * buf) {
834 char * xml_buf = NULL;
840 str_len = strlen(buf);
841 xml_buf = (char *)V3_Malloc(str_len + 1);
842 strcpy(xml_buf, buf);
844 return parse_str(xml_buf, str_len);
849 // free the memory allocated for the v3_xml structure
850 void v3_xml_free(struct v3_xml * xml) {
851 struct v3_xml_root * root = (struct v3_xml_root *)xml;
859 v3_xml_free(xml->child);
860 v3_xml_free(xml->ordered);
862 if (xml->parent == NULL) {
863 // free root tag allocations
865 for (i = 10; root->ent[i]; i += 2) {
866 // 0 - 9 are default entites (<>&"')
867 if ((s = root->ent[i + 1]) < root->tmp_start || s > root->tmp_end) {
872 V3_Free(root->ent); // free list of general entities
874 for (i = 0; (a = root->attr[i]); i++) {
875 for (j = 1; a[j++]; j += 2) {
876 // free malloced attribute values
877 if (a[j] && (a[j] < root->tmp_start || a[j] > root->tmp_end)) {
885 // free default attribute list
889 V3_Free(root->str_ptr); // malloced xml data
892 v3_xml_free_attr(xml->attr); // tag attributes
894 if ((xml->flags & V3_XML_TXTM)) {
899 if ((xml->flags & V3_XML_NAMEM)) {
913 // sets the character content for the given tag and returns the tag
914 struct v3_xml * v3_xml_set_txt(struct v3_xml * xml, const char *txt) {
919 if (xml->flags & V3_XML_TXTM) {
920 // existing txt was malloced
924 xml->flags &= ~V3_XML_TXTM;
925 xml->txt = (char *)txt;
929 // Sets the given tag attribute or adds a new attribute if not found. A value
930 // of NULL will remove the specified attribute. Returns the tag given.
931 struct v3_xml * v3_xml_set_attr(struct v3_xml * xml, const char * name, const char * value) {
939 while (xml->attr[l] && strcmp(xml->attr[l], name)) {
943 if (! xml->attr[l]) {
944 // not found, add as new attribute
951 if (xml->attr == V3_XML_NIL) {
953 xml->attr = V3_Malloc(4 * sizeof(char *));
955 // empty list of malloced names/vals
956 xml->attr[1] = strdup("");
958 xml->attr = tmp_realloc(xml->attr, l * sizeof(char *), (l + 4) * sizeof(char *));
961 // set attribute name
962 xml->attr[l] = (char *)name;
964 // null terminate attribute list
965 xml->attr[l + 2] = NULL;
967 xml->attr[l + 3] = tmp_realloc(xml->attr[l + 1],
968 strlen(xml->attr[l + 1]),
969 (c = strlen(xml->attr[l + 1])) + 2);
971 // set name/value as not malloced
972 strcpy(xml->attr[l + 3] + c, " ");
974 if (xml->flags & V3_XML_DUP) {
975 xml->attr[l + 3][c] = V3_XML_NAMEM;
977 } else if (xml->flags & V3_XML_DUP) {
979 V3_Free((char *)name);
983 // find end of attribute list
984 for (c = l; xml->attr[c]; c += 2);
986 if (xml->attr[c + 1][l / 2] & V3_XML_TXTM) {
988 V3_Free(xml->attr[l + 1]);
991 if (xml->flags & V3_XML_DUP) {
992 xml->attr[c + 1][l / 2] |= V3_XML_TXTM;
994 xml->attr[c + 1][l / 2] &= ~V3_XML_TXTM;
999 // set attribute value
1000 xml->attr[l + 1] = (char *)value;
1004 if (xml->attr[c + 1][l / 2] & V3_XML_NAMEM) {
1005 V3_Free(xml->attr[l]);
1008 memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
1010 xml->attr = tmp_realloc(xml->attr, c * sizeof(char *), (c + 2) * sizeof(char *));
1012 // fix list of which name/vals are malloced
1013 memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
1017 // clear strdup() flag
1018 xml->flags &= ~V3_XML_DUP;
1023 // removes a tag along with its subtags without freeing its memory
1024 struct v3_xml * v3_xml_cut(struct v3_xml * xml) {
1025 struct v3_xml * cur;
1033 // patch sibling list
1034 xml->next->sibling = xml->sibling;
1041 // find head of subtag list
1042 cur = xml->parent->child;
1046 xml->parent->child = xml->ordered;
1050 while (cur->ordered != xml) {
1054 // patch ordered list
1055 cur->ordered = cur->ordered->ordered;
1057 // go back to head of subtag list
1058 cur = xml->parent->child;
1060 if (strcmp(cur->name, xml->name)) {
1061 // not in first sibling list
1063 while (strcmp(cur->sibling->name, xml->name)) {
1067 if (cur->sibling == xml) {
1068 // first of a sibling list
1069 cur->sibling = (xml->next) ? xml->next
1070 : cur->sibling->sibling;
1072 // not first of a sibling list
1077 while (cur->next && cur->next != xml) {
1083 cur->next = cur->next->next;
1087 xml->ordered = xml->sibling = xml->next = NULL;
1094 /* ************************** */
1095 /* *** XML ENCODING *** */
1096 /* ************************** */
1098 // Encodes ampersand sequences appending the results to *dst, reallocating *dst
1099 // if length excedes max. a is non-zero for attribute encoding. Returns *dst
1100 static char *ampencode(const char *s, size_t len, char **dst, size_t *dlen,
1101 size_t * max, short a)
1105 for (e = s + len; s != e; s++) {
1106 while (*dlen + 10 > *max) {
1107 *dst = tmp_realloc(*dst, *max, *max + V3_XML_BUFSIZE);
1108 *max += V3_XML_BUFSIZE;
1112 case '\0': return *dst;
1113 case '&': *dlen += sprintf(*dst + *dlen, "&"); break;
1114 case '<': *dlen += sprintf(*dst + *dlen, "<"); break;
1115 case '>': *dlen += sprintf(*dst + *dlen, ">"); break;
1116 case '"': *dlen += sprintf(*dst + *dlen, (a) ? """ : "\""); break;
1117 case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "
" : "\n"); break;
1118 case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "	" : "\t"); break;
1119 case '\r': *dlen += sprintf(*dst + *dlen, "
"); break;
1120 default: (*dst)[(*dlen)++] = *s;
1128 // Recursively converts each tag to xml appending it to *s. Reallocates *s if
1129 // its length excedes max. start is the location of the previous tag in the
1130 // parent tag's character content. Returns *s.
1131 static char *toxml_r(struct v3_xml * xml, char **s, size_t *len, size_t *max,
1132 size_t start, char ***attr) {
1134 char *txt = (xml->parent) ? xml->parent->txt : "";
1137 // parent character content up to this tag
1138 *s = ampencode(txt + start, xml->off - start, s, len, max, 0);
1140 while (*len + strlen(xml->name) + 4 > *max) {
1142 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1143 *max += V3_XML_BUFSIZE;
1147 *len += sprintf(*s + *len, "<%s", xml->name); // open tag
1148 for (i = 0; xml->attr[i]; i += 2) { // tag attributes
1149 if (v3_xml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
1150 while (*len + strlen(xml->attr[i]) + 7 > *max) {
1152 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1153 *max += V3_XML_BUFSIZE;
1156 *len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
1157 ampencode(xml->attr[i + 1], -1, s, len, max, 1);
1158 *len += sprintf(*s + *len, "\"");
1161 for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
1162 for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes
1163 if (! attr[i][j + 1] || v3_xml_attr(xml, attr[i][j]) != attr[i][j + 1])
1164 continue; // skip duplicates and non-values
1165 while (*len + strlen(attr[i][j]) + 7 > *max) {
1167 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1168 *max += V3_XML_BUFSIZE;
1171 *len += sprintf(*s + *len, " %s=\"", attr[i][j]);
1172 ampencode(attr[i][j + 1], -1, s, len, max, 1);
1173 *len += sprintf(*s + *len, "\"");
1175 *len += sprintf(*s + *len, ">");
1177 *s = (xml->child) ? toxml_r(xml->child, s, len, max, 0, attr) //child
1178 : ampencode(xml->txt, -1, s, len, max, 0); //data
1180 while (*len + strlen(xml->name) + 4 > *max) {
1182 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1183 *max += V3_XML_BUFSIZE;
1186 *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
1188 while (txt[off] && off < xml->off) off++; // make sure off is within bounds
1189 return (xml->ordered) ? toxml_r(xml->ordered, s, len, max, off, attr)
1190 : ampencode(txt + off, -1, s, len, max, 0);
1193 // Converts an xml structure back to xml. Returns a string of xml data that
1195 char * v3_xml_tostr(struct v3_xml * xml) {
1196 struct v3_xml * p = (xml) ? xml->parent : NULL;
1197 struct v3_xml * o = (xml) ? xml->ordered : NULL;
1198 struct v3_xml_root * root = (struct v3_xml_root *)xml;
1199 size_t len = 0, max = V3_XML_BUFSIZE;
1200 char *s = strcpy(V3_Malloc(max), "");
1202 if (! xml || ! xml->name) return tmp_realloc(s, max, len + 1);
1203 while (root->xml.parent) root = (struct v3_xml_root *)root->xml.parent; // root tag
1206 xml->parent = xml->ordered = NULL;
1207 s = toxml_r(xml, &s, &len, &max, 0, root->attr);
1212 return tmp_realloc(s, max, len + 1);