1 /* @(#) $Id: os_xml_node_access.c,v 1.6 2009/06/24 17:06:32 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
13 * Available at http://www.ossec.net/
25 * Clear the Node structure
27 void OS_ClearNode(xml_node **node)
35 free(node[i]->element);
37 free(node[i]->content);
38 if(node[i]->attributes)
41 while(node[i]->attributes[j])
43 free(node[i]->attributes[j]);
46 free(node[i]->attributes);
51 while(node[i]->values[j])
53 free(node[i]->values[j]);
56 free(node[i]->values);
59 node[i]->element=NULL;
60 node[i]->content=NULL;
61 node[i]->attributes=NULL;
73 /** xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
74 * Get the elements by node.
76 xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
93 for(;i<_lxml->cur;i++)
95 if(_lxml->tp[i] == XML_ELEM)
97 if((_lxml->rl[i] == j+1) && (_lxml->el[i] != NULL))
100 /* Allocating for xml_node ** */
101 ret = (xml_node**)realloc(ret,(k+1)*sizeof(xml_node*));
105 /* Allocating for the xml_node * */
106 ret[k] = (xml_node *)calloc(1,sizeof(xml_node));
110 ret[k]->element = NULL;
111 ret[k]->content = NULL;
112 ret[k]->attributes = NULL;
113 ret[k]->values = NULL;
115 /* Getting the element */
116 ret[k]->element=strdup(_lxml->el[i]);
117 if(ret[k]->element == NULL)
123 /* Getting the content */
126 ret[k]->content=strdup(_lxml->ct[i]);
127 if(ret[k]->content == NULL)
130 /* Assigning the key */
133 /* Getting attributes */
134 while(l < _lxml->cur)
136 if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == j+1)&&
137 (_lxml->el[l]) && (_lxml->ct[l]))
140 (char**)realloc(ret[k]->attributes,
141 (l-i+1)*sizeof(char*));
143 (char**)realloc(ret[k]->values,
144 (l-i+1)*sizeof(char*));
145 if(!(ret[k]->attributes) ||
148 ret[k]->attributes[l-i-1]=strdup(_lxml->el[l]);
149 ret[k]->values[l-i-1] = strdup(_lxml->ct[l]);
150 if(!(ret[k]->attributes[l-i-1]) ||
151 !(ret[k]->values[l-i-1]))
160 if(ret[k]->attributes)
162 ret[k]->attributes[l-i-1] = NULL;
163 ret[k]->values[l-i-1] = NULL;
169 if((_lxml->tp[i] == XML_ELEM)&&(j+1 > _lxml->rl[i]))
181 ret = (xml_node **)realloc(ret,(k+1)*sizeof(xml_node *));