1 /* @(#) $Id: ./src/os_xml/os_xml_node_access.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
14 * Available at http://www.ossec.net/
23 #include "os_xml_internal.h"
27 * Clear the Node structure
29 void OS_ClearNode(xml_node **node)
38 free(node[i]->element);
42 free(node[i]->content);
44 if(node[i]->attributes)
47 while(node[i]->attributes[j])
49 free(node[i]->attributes[j]);
52 free(node[i]->attributes);
57 while(node[i]->values[j])
59 free(node[i]->values[j]);
62 free(node[i]->values);
65 node[i]->element=NULL;
66 node[i]->content=NULL;
67 node[i]->attributes=NULL;
78 /** xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
79 * Get the elements by node.
81 xml_node **OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node)
83 unsigned int i, k =0,m;
85 xml_node **ret_tmp=NULL;
95 m = _lxml->rl[i++] + 1;
99 for(;i<_lxml->cur;i++)
101 if(_lxml->tp[i] == XML_ELEM)
103 if((_lxml->rl[i] == m) && (_lxml->el[i] != NULL))
106 /* Allocating for xml_node ** */
107 ret_tmp = (xml_node**)realloc(ret,(k+2)*sizeof(xml_node*));
112 /* Allocating for the xml_node * */
113 ret[k] = (xml_node *)calloc(1,sizeof(xml_node));
118 ret[k]->element = NULL;
119 ret[k]->content = NULL;
120 ret[k]->attributes = NULL;
121 ret[k]->values = NULL;
123 /* Getting the element */
124 ret[k]->element=strdup(_lxml->el[i]);
125 if(ret[k]->element == NULL)
130 /* Getting the content */
133 ret[k]->content=strdup(_lxml->ct[i]);
134 if(ret[k]->content == NULL)
137 /* Assigning the key */
140 /* Getting attributes */
141 while(l < _lxml->cur)
143 if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == m)&&
144 (_lxml->el[l]) && (_lxml->ct[l]))
147 tmp = (char**)realloc(ret[k]->attributes, (l-i+1)*sizeof(char*));
150 ret[k]->attributes = tmp;
151 ret[k]->attributes[l-i] = NULL;
152 tmp = (char**)realloc(ret[k]->values, (l-i+1)*sizeof(char*));
155 ret[k]->values = tmp;
156 ret[k]->values[l-i] = NULL;
158 ret[k]->attributes[l-i-1]=strdup(_lxml->el[l]);
159 ret[k]->values[l-i-1] = strdup(_lxml->ct[l]);
160 if(!(ret[k]->attributes[l-i-1]) ||
161 !(ret[k]->values[l-i-1]))
174 if((_lxml->tp[i] == XML_ELEM)&&(m > _lxml->rl[i]))