1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
15 #include "os_xml_internal.h"
18 /* Clear the Node structure */
19 void OS_ClearNode(xml_node **node)
24 if (node[i]->element) {
25 free(node[i]->element);
27 if (node[i]->content) {
28 free(node[i]->content);
30 if (node[i]->attributes) {
32 while (node[i]->attributes[j]) {
33 free(node[i]->attributes[j]);
36 free(node[i]->attributes);
38 if (node[i]->values) {
40 while (node[i]->values[j]) {
41 free(node[i]->values[j]);
44 free(node[i]->values);
47 node[i]->element = NULL;
48 node[i]->content = NULL;
49 node[i]->attributes = NULL;
50 node[i]->values = NULL;
60 /* Get the elements by node */
61 xml_node **OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node)
63 unsigned int i, k = 0, m;
64 xml_node **ret = NULL;
65 xml_node **ret_tmp = NULL;
72 m = _lxml->rl[i++] + 1;
75 for (; i < _lxml->cur; i++) {
76 if (_lxml->tp[i] == XML_ELEM) {
77 if ((_lxml->rl[i] == m) && (_lxml->el[i] != NULL)) {
78 unsigned int l = i + 1;
79 /* Allocate for xml_node ** */
80 ret_tmp = (xml_node **)realloc(ret, (k + 2) * sizeof(xml_node *));
81 if (ret_tmp == NULL) {
86 /* Allocate for the xml_node * */
87 ret[k] = (xml_node *)calloc(1, sizeof(xml_node));
93 ret[k]->element = NULL;
94 ret[k]->content = NULL;
95 ret[k]->attributes = NULL;
96 ret[k]->values = NULL;
99 ret[k]->element = strdup(_lxml->el[i]);
100 if (ret[k]->element == NULL) {
106 ret[k]->content = strdup(_lxml->ct[i]);
107 if (ret[k]->content == NULL) {
115 while (l < _lxml->cur) {
116 if ((_lxml->tp[l] == XML_ATTR) && (_lxml->rl[l] == m) &&
117 (_lxml->el[l]) && (_lxml->ct[l])) {
119 tmp = (char **)realloc(ret[k]->attributes, (l - i + 1) * sizeof(char *));
123 ret[k]->attributes = tmp;
124 ret[k]->attributes[l - i] = NULL;
125 tmp = (char **)realloc(ret[k]->values, (l - i + 1) * sizeof(char *));
129 ret[k]->values = tmp;
130 ret[k]->values[l - i] = NULL;
132 ret[k]->attributes[l - i - 1] = strdup(_lxml->el[l]);
133 ret[k]->values[l - i - 1] = strdup(_lxml->ct[l]);
134 if (!(ret[k]->attributes[l - i - 1]) ||
135 !(ret[k]->values[l - i - 1])) {
147 if ((_lxml->tp[i] == XML_ELEM) && (m > _lxml->rl[i])) {