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 short standalone; // non-zero if <?xml standalone="yes"?>
57 char err[V3_XML_ERRL]; // error string
60 static char * empty_attrib_list[] = { NULL }; // empty, null terminated array of strings
64 static void * tmp_realloc(void * old_ptr, size_t old_size, size_t new_size) {
65 void * new_buf = NULL;
67 new_buf = V3_Malloc(new_size);
69 if (new_buf == NULL) {
70 PrintError("Cannot allocate in tmp_realloc in xml\n");
74 memset(new_buf, 0, new_size);
76 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 if ((!xml) || (!xml->attr)) {
144 while ((xml->attr[i]) && (strcasecmp(attr, xml->attr[i]) != 0)) {
148 if (xml->attr[i] != NULL) {
149 return xml->attr[i + 1]; // found attribute
152 return NULL; // found default
155 // same as v3_xml_get but takes an already initialized va_list
156 static struct v3_xml * v3_xml_vget(struct v3_xml * xml, va_list ap) {
157 char * name = va_arg(ap, char *);
160 if ((name != NULL) && (*name != 0)) {
161 idx = va_arg(ap, int);
162 xml = v3_xml_child(xml, name);
164 return (idx < 0) ? xml : v3_xml_vget(v3_xml_idx(xml, idx), ap);
167 // Traverses the xml tree to retrieve a specific subtag. Takes a variable
168 // length list of tag names and indexes. The argument list must be terminated
169 // by either an index of -1 or an empty string tag name. Example:
170 // title = v3_xml_get(library, "shelf", 0, "book", 2, "title", -1);
171 // This retrieves the title of the 3rd book on the 1st shelf of library.
172 // Returns NULL if not found.
173 struct v3_xml * v3_xml_get(struct v3_xml * xml, ...) {
178 r = v3_xml_vget(xml, ap);
184 // sets a flag for the given tag and returns the tag
185 static struct v3_xml * v3_xml_set_flag(struct v3_xml * xml, short flag)
197 // Recursively decodes entity and character references and normalizes new lines
198 // ent is a null terminated array of alternating entity names and values. set t
199 // to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
200 // for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
201 // attribute normalization. Returns s, or if the decoded string is longer than
202 // s, returns a malloced string that must be freed.
203 static char * v3_xml_decode(char * s, char t) {
208 // normalize line endings
214 memmove(s, (s + 1), strlen(s));
233 if ((t != 'c') && (strncmp(s, "&#", 2) == 0)) { // character reference
235 c = strtox(s + 3, &e); // base 16
237 c = strtoi(s + 2, &e); // base 10
240 if ((!c) || (*e != ';')) {
241 // not a character ref
248 memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
249 } else if ( ( (t == ' ') || (t == '*')) &&
253 // no decoding needed
259 // normalize spaces for non-cdata attributes
260 for (s = r; *s; s++) {
261 if ((l = strspn(s, " "))) {
262 memmove(s, s + l, strlen(s + l) + 1);
265 while ((*s) && (*s != ' ')) {
270 if ((--s >= r) && (*s == ' ')) {
271 // trim any trailing space
280 // called when parser finds character content between open and closing tag
281 static void v3_xml_char_content(struct v3_xml_root * root, char * s, size_t len, char t) {
282 struct v3_xml * xml = root->cur;
286 if ((xml == NULL) || (xml->name == NULL) || (len == 0)) {
291 s[len] = '\0'; // null terminate text (calling functions anticipate this)
292 len = strlen(s = v3_xml_decode(s, t)) + 1;
294 if (xml->txt[0] == '\0') { // empty string
295 // initial character content
299 // allocate our own memory and make a copy
300 if (xml->flags & V3_XML_TXTM) {
301 xml->txt = (tmp_realloc(xml->txt, strlen(xml->txt), (l = strlen(xml->txt)) + len));
305 tmp = V3_Malloc((l = strlen(xml->txt)) + len);
308 PrintError("Cannot allocate in xml char content\n");
312 strcpy(tmp, xml->txt);
316 strcpy(xml->txt + l, s); // add new char content
319 V3_Free(s); // free s if it was malloced by v3_xml_decode()
324 v3_xml_set_flag(xml, V3_XML_TXTM);
328 // called when parser finds closing tag
329 static int v3_xml_close_tag(struct v3_xml_root * root, char * name, char * s) {
330 if ( (root->cur == NULL) ||
331 (root->cur->name == NULL) ||
332 (strcasecmp(name, root->cur->name))) {
333 v3_xml_err(root, s, "unexpected closing tag </%s>", name);
337 root->cur = root->cur->parent;
342 // frees a tag attribute list
343 static void v3_xml_free_attr(char **attr) {
347 if ((attr == NULL) || (attr == empty_attrib_list)) {
353 // find end of attribute list
357 m = attr[i + 1]; // list of which names and values are malloced
368 // returns a new empty v3_xml structure with the given root tag name
369 static struct v3_xml * v3_xml_new(const char * name) {
371 struct v3_xml_root * root = (struct v3_xml_root *)V3_Malloc(sizeof(struct v3_xml_root));
374 PrintError("Cannot allocate in xml_new\n");
378 memset(root, 0, sizeof(struct v3_xml_root));
380 root->xml.name = (char *)name;
381 root->cur = &root->xml;
383 memset(root->err, 0, V3_XML_ERRL);
389 // inserts an existing tag into an v3_xml structure
390 struct v3_xml * v3_xml_insert(struct v3_xml * xml, struct v3_xml * dest, size_t off) {
391 struct v3_xml * cur, * prev, * head;
400 if ((head = dest->child)) {
401 // already have sub tags
403 if (head->off <= off) {
407 ((cur->ordered) && (cur->ordered->off <= off));
410 xml->ordered = cur->ordered;
419 for (cur = head, prev = NULL;
420 ((cur) && (strcasecmp(cur->name, xml->name) != 0));
421 prev = cur, cur = cur->sibling);
424 if (cur && cur->off <= off) {
427 while (cur->next && cur->next->off <= off) {
431 xml->next = cur->next;
434 // first tag of this type
438 prev->sibling = cur->sibling;
441 xml->next = cur; // old first tag is now next
443 // new sibling insert point
444 for (cur = head, prev = NULL;
445 ((cur) && (cur->off <= off));
446 prev = cur, cur = cur->sibling);
463 // Adds a child tag. off is the offset of the child tag relative to the start
464 // of the parent tag's character content. Returns the child tag.
465 static struct v3_xml * v3_xml_add_child(struct v3_xml * xml, const char * name, size_t off) {
466 struct v3_xml * child;
472 child = (struct v3_xml *)V3_Malloc(sizeof(struct v3_xml));
475 PrintError("Cannot allocate in xml_add_child\n");
479 memset(child, 0, sizeof(struct v3_xml));
481 child->name = (char *)name;
482 child->attr = empty_attrib_list;
485 return v3_xml_insert(child, xml, off);
489 // called when parser finds start of new tag
490 static void v3_xml_open_tag(struct v3_xml_root * root, char * name, char ** attr) {
491 struct v3_xml * xml = root->cur;
494 xml = v3_xml_add_child(xml, name, strlen(xml->txt));
501 root->cur = xml; // update tag insertion point
510 // parse the given xml string and return an v3_xml structure
511 static struct v3_xml * parse_str(char * buf, size_t len) {
512 struct v3_xml_root * root = (struct v3_xml_root *)v3_xml_new(NULL);
522 v3_xml_err(root, NULL, "Empty XML String\n");
526 root->tmp_start = buf;
527 root->tmp_end = buf + len; // record start and end of work area
529 last_char = buf[len - 1]; // save end char
530 buf[len - 1] = '\0'; // turn end char into null terminator
532 while ((*buf) && (*buf != '<')) {
538 v3_xml_err(root, buf, "root tag missing");
543 attr = (char **)empty_attrib_list;
544 tag_ptr = ++buf; // skip first '<'
546 if (isalpha(*buf) || (*buf == '_') || (*buf == ':') || (*buf < '\0')) {
549 if (root->cur == NULL) {
550 v3_xml_err(root, tag_ptr, "markup outside of root element");
554 buf += strcspn(buf, V3_XML_WS "/>");
556 while (isspace(*buf)) {
557 // null terminate tag name,
558 // this writes '\0' to spaces after first tag
564 // attributes are name value pairs,
565 // 2nd to last entry is null (end of list)
566 // last entry points to a string map marking whether values have been malloced...
567 // loop through attributes until hitting the closing bracket
569 (*buf) && (*buf != '/') && (*buf != '>');
571 // buf is incremented later on
573 int attr_cnt = (attr_idx / 2) + 1;
574 int val_idx = attr_idx + 1;
575 int term_idx = attr_idx + 2;
576 int last_idx = attr_idx + 3;
578 // attr = allocated space
579 // attr[val_idx] = mem for list of maloced vals
581 attr = tmp_realloc(attr,
582 (((attr_cnt - 1) * (2 * sizeof(char *))) +
583 (2 * sizeof(char *))),
584 ((attr_cnt * (2 * sizeof(char *))) +
585 (2 * sizeof(char *))));
588 PrintError("Cannot reallocate in xml parse string\n");
592 attr[last_idx] = tmp_realloc(attr[last_idx - 2],
596 if (!attr[last_idx]) {
597 PrintError("Cannot reallocate in xml parse string\n");
602 attr = V3_Malloc(4 * sizeof(char *));
604 PrintError("Cannot allocate in xml parse string\n");
607 attr[last_idx] = V3_Malloc(2);
608 if (!attr[last_idx]) {
609 PrintError("Cannot alloocate in xml parse string\n");
614 attr[attr_idx] = buf; // set attribute name
615 attr[val_idx] = ""; // temporary attribute value
616 attr[term_idx] = NULL; // null terminate list
617 strcpy(attr[last_idx] + attr_cnt, " "); // value is not malloc'd, offset into the stringmap
619 buf += strcspn(buf, V3_XML_WS "=/>");
621 if ((*buf == '=') || isspace(*buf)) {
623 *(buf++) = '\0'; // null terminate tag attribute name
625 // eat whitespace (and more multiple '=' ?)
626 buf += strspn(buf, V3_XML_WS "=");
630 if ((quote_char == '"') || (quote_char == '\'')) { // attribute value
631 attr[val_idx] = ++buf;
633 while ((*buf) && (*buf != quote_char)) {
638 // null terminate attribute val
641 v3_xml_free_attr(attr);
642 v3_xml_err(root, tag_ptr, "missing %c", quote_char);
646 attr[val_idx] = v3_xml_decode(attr[val_idx], ' ');
650 while (isspace(*buf)) {
659 if ( ((*buf) && (*buf != '>')) ||
660 ((!*buf) && (last_char != '>'))) {
663 v3_xml_free_attr(attr);
665 v3_xml_err(root, tag_ptr, "missing >");
668 v3_xml_open_tag(root, tag_ptr, attr);
669 v3_xml_close_tag(root, tag_ptr, buf);
670 } else if (((quote_char = *buf) == '>') ||
671 ((!*buf) && (last_char == '>'))) {
673 *buf = '\0'; // temporarily null terminate tag name
674 v3_xml_open_tag(root, tag_ptr, attr);
678 v3_xml_free_attr(attr);
680 v3_xml_err(root, tag_ptr, "missing >");
683 } else if (*buf == '/') {
686 buf += strcspn(tag_ptr = buf + 1, V3_XML_WS ">") + 1;
689 if ((*buf == '\0') && (last_char != '>')) {
690 v3_xml_err(root, tag_ptr, "missing >");
694 *buf = '\0'; // temporarily null terminate tag name
696 if (v3_xml_close_tag(root, tag_ptr, buf) == -1) {
702 buf += strspn(buf, V3_XML_WS);
704 } else if (strncmp(buf, "!--", 3) == 0) {
706 if ( ((buf = strstr(buf + 3, "--")) == 0) ||
707 ((*(buf += 2) != '>') && (*buf)) ||
708 ((!*buf) && (last_char != '>'))) {
709 v3_xml_err(root, tag_ptr, "unclosed <!--");
712 } else if (! strncmp(buf, "![CDATA[", 8)) {
714 if ((buf = strstr(buf, "]]>"))) {
715 v3_xml_char_content(root, tag_ptr + 8, (buf += 2) - tag_ptr - 10, 'c');
717 v3_xml_err(root, tag_ptr, "unclosed <![CDATA[");
721 v3_xml_err(root, tag_ptr, "unexpected <");
725 if (! buf || ! *buf) {
732 /* Eat leading whitespace */
733 while (*buf && isspace(*buf)) {
737 if (*buf && (*buf != '<')) {
738 // tag character content
739 while (*buf && (*buf != '<')) {
744 v3_xml_char_content(root, tag_ptr, buf - tag_ptr, '&');
748 } else if (*buf == '\0') {
753 if (root->cur == NULL) {
755 } else if (root->cur->name == NULL) {
756 v3_xml_err(root, tag_ptr, "root tag missing");
759 v3_xml_err(root, tag_ptr, "unclosed tag <%s>", root->cur->name);
765 struct v3_xml * v3_xml_parse(char * buf) {
767 char * xml_buf = NULL;
773 str_len = strlen(buf);
774 xml_buf = (char *)V3_Malloc(str_len + 1);
777 PrintError("Cannot allocate in xml parse\n");
781 strcpy(xml_buf, buf);
783 return parse_str(xml_buf, str_len);
788 // free the memory allocated for the v3_xml structure
789 void v3_xml_free(struct v3_xml * xml) {
790 struct v3_xml_root * root = (struct v3_xml_root *)xml;
796 v3_xml_free(xml->child);
797 v3_xml_free(xml->ordered);
799 if (xml->parent == NULL) {
800 // free root tag allocations
801 V3_Free(root->str_ptr); // malloced xml data
804 v3_xml_free_attr(xml->attr); // tag attributes
808 if ((xml->flags & V3_XML_TXTM)) {
813 if ((xml->flags & V3_XML_NAMEM)) {
825 /* Adding XML data */
830 // sets the character content for the given tag and returns the tag
831 struct v3_xml * v3_xml_set_txt(struct v3_xml * xml, const char *txt) {
836 if (xml->flags & V3_XML_TXTM) {
837 // existing txt was malloced
841 xml->flags &= ~V3_XML_TXTM;
842 xml->txt = (char *)txt;
846 // Sets the given tag attribute or adds a new attribute if not found. A value
847 // of NULL will remove the specified attribute. Returns the tag given.
848 struct v3_xml * v3_xml_set_attr(struct v3_xml * xml, const char * name, const char * value) {
856 while (xml->attr[l] && strcmp(xml->attr[l], name)) {
860 if (! xml->attr[l]) {
861 // not found, add as new attribute
868 if (xml->attr == V3_XML_NIL) {
870 xml->attr = V3_Malloc(4 * sizeof(char *));
873 PrintError("Cannot allocate in xml set attr\n");
877 // empty list of malloced names/vals
878 xml->attr[1] = strdup("");
881 PrintError("Cannot strdup in xml set attr\n");
886 xml->attr = tmp_realloc(xml->attr, l * sizeof(char *), (l + 4) * sizeof(char *));
889 PrintError("Cannot reallocate in xml set attr\n");
894 // set attribute name
895 xml->attr[l] = (char *)name;
897 // null terminate attribute list
898 xml->attr[l + 2] = NULL;
900 xml->attr[l + 3] = tmp_realloc(xml->attr[l + 1],
901 strlen(xml->attr[l + 1]),
902 (c = strlen(xml->attr[l + 1])) + 2);
905 if (!xml->attr[l + 3]) {
906 PrintError("Cannot reallocate in xml set attr\n");
910 // set name/value as not malloced
911 strcpy(xml->attr[l + 3] + c, " ");
913 if (xml->flags & V3_XML_DUP) {
914 xml->attr[l + 3][c] = V3_XML_NAMEM;
916 } else if (xml->flags & V3_XML_DUP) {
918 V3_Free((char *)name);
922 // find end of attribute list
923 for (c = l; xml->attr[c]; c += 2);
925 if (xml->attr[c + 1][l / 2] & V3_XML_TXTM) {
927 V3_Free(xml->attr[l + 1]);
930 if (xml->flags & V3_XML_DUP) {
931 xml->attr[c + 1][l / 2] |= V3_XML_TXTM;
933 xml->attr[c + 1][l / 2] &= ~V3_XML_TXTM;
938 // set attribute value
939 xml->attr[l + 1] = (char *)value;
943 if (xml->attr[c + 1][l / 2] & V3_XML_NAMEM) {
944 V3_Free(xml->attr[l]);
947 memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
949 xml->attr = tmp_realloc(xml->attr, c * sizeof(char *), (c + 2) * sizeof(char *));
951 // fix list of which name/vals are malloced
952 memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
956 // clear strdup() flag
957 xml->flags &= ~V3_XML_DUP;
962 // removes a tag along with its subtags without freeing its memory
963 struct v3_xml * v3_xml_cut(struct v3_xml * xml) {
972 // patch sibling list
973 xml->next->sibling = xml->sibling;
980 // find head of subtag list
981 cur = xml->parent->child;
985 xml->parent->child = xml->ordered;
989 while (cur->ordered != xml) {
993 // patch ordered list
994 cur->ordered = cur->ordered->ordered;
996 // go back to head of subtag list
997 cur = xml->parent->child;
999 if (strcmp(cur->name, xml->name)) {
1000 // not in first sibling list
1002 while (strcmp(cur->sibling->name, xml->name)) {
1006 if (cur->sibling == xml) {
1007 // first of a sibling list
1008 cur->sibling = (xml->next) ? xml->next
1009 : cur->sibling->sibling;
1011 // not first of a sibling list
1016 while (cur->next && cur->next != xml) {
1022 cur->next = cur->next->next;
1026 xml->ordered = xml->sibling = xml->next = NULL;
1033 /* ************************** */
1034 /* *** XML ENCODING *** */
1035 /* ************************** */
1037 // Encodes ampersand sequences appending the results to *dst, reallocating *dst
1038 // if length excedes max. a is non-zero for attribute encoding. Returns *dst
1039 static char *ampencode(const char *s, size_t len, char **dst, size_t *dlen,
1040 size_t * max, short a)
1044 for (e = s + len; s != e; s++) {
1045 while (*dlen + 10 > *max) {
1046 *dst = tmp_realloc(*dst, *max, *max + V3_XML_BUFSIZE);
1047 *max += V3_XML_BUFSIZE;
1051 case '\0': return *dst;
1052 case '&': *dlen += sprintf(*dst + *dlen, "&"); break;
1053 case '<': *dlen += sprintf(*dst + *dlen, "<"); break;
1054 case '>': *dlen += sprintf(*dst + *dlen, ">"); break;
1055 case '"': *dlen += sprintf(*dst + *dlen, (a) ? """ : "\""); break;
1056 case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "
" : "\n"); break;
1057 case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "	" : "\t"); break;
1058 case '\r': *dlen += sprintf(*dst + *dlen, "
"); break;
1059 default: (*dst)[(*dlen)++] = *s;
1067 // Recursively converts each tag to xml appending it to *s. Reallocates *s if
1068 // its length excedes max. start is the location of the previous tag in the
1069 // parent tag's character content. Returns *s.
1070 static char *toxml_r(struct v3_xml * xml, char **s, size_t *len, size_t *max,
1073 char *txt = (xml->parent) ? xml->parent->txt : "";
1076 // parent character content up to this tag
1077 *s = ampencode(txt + start, xml->off - start, s, len, max, 0);
1079 while (*len + strlen(xml->name) + 4 > *max) {
1081 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1082 *max += V3_XML_BUFSIZE;
1086 *len += sprintf(*s + *len, "<%s", xml->name); // open tag
1087 for (i = 0; xml->attr[i]; i += 2) { // tag attributes
1088 if (v3_xml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
1089 while (*len + strlen(xml->attr[i]) + 7 > *max) {
1091 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1092 *max += V3_XML_BUFSIZE;
1095 *len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
1096 ampencode(xml->attr[i + 1], -1, s, len, max, 1);
1097 *len += sprintf(*s + *len, "\"");
1101 *len += sprintf(*s + *len, ">");
1103 *s = (xml->child) ? toxml_r(xml->child, s, len, max, 0) //child
1104 : ampencode(xml->txt, -1, s, len, max, 0); //data
1106 while (*len + strlen(xml->name) + 4 > *max) {
1108 *s = tmp_realloc(*s, *max, *max + V3_XML_BUFSIZE);
1109 *max += V3_XML_BUFSIZE;
1112 *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
1114 while (txt[off] && off < xml->off) off++; // make sure off is within bounds
1115 return (xml->ordered) ? toxml_r(xml->ordered, s, len, max, off)
1116 : ampencode(txt + off, -1, s, len, max, 0);
1119 // Converts an xml structure back to xml. Returns a string of xml data that
1121 char * v3_xml_tostr(struct v3_xml * xml) {
1122 struct v3_xml * p = (xml) ? xml->parent : NULL;
1123 struct v3_xml * o = (xml) ? xml->ordered : NULL;
1124 struct v3_xml_root * root = (struct v3_xml_root *)xml;
1125 size_t len = 0, max = V3_XML_BUFSIZE;
1126 char *s = V3_Malloc(max);
1129 PrintError("Cannot allocate in xml tostrr\n");
1135 if (! xml || ! xml->name) return tmp_realloc(s, max, len + 1);
1136 while (root->xml.parent) root = (struct v3_xml_root *)root->xml.parent; // root tag
1139 xml->parent = xml->ordered = NULL;
1140 s = toxml_r(xml, &s, &len, &max, 0);
1145 return tmp_realloc(s, max, len + 1);