izmjene licence
[ossec-hids.git] / src / os_xml / os_xml_node_access.c
1 /* @(#) $Id: ./src/os_xml/os_xml_node_access.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation
11  */
12
13 /* os_xml C Library.
14  * Available at http://www.ossec.net/
15  */
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "os_xml.h"
23 #include "os_xml_internal.h"
24
25
26 /* OS_ClearNode v0,1
27  * Clear the Node structure
28  */
29 void OS_ClearNode(xml_node **node)
30 {
31     if(node)
32     {
33         int i=0;
34         while(node[i])
35         {
36             if(node[i]->element)
37             {
38                 free(node[i]->element);
39             }
40             if(node[i]->content)
41             {
42                 free(node[i]->content);
43             }
44             if(node[i]->attributes)
45             {
46                 int j=0;
47                 while(node[i]->attributes[j])
48                 {
49                     free(node[i]->attributes[j]);
50                     j++;
51                 }
52                 free(node[i]->attributes);
53             }
54             if(node[i]->values)
55             {
56                 int j=0;
57                 while(node[i]->values[j])
58                 {
59                     free(node[i]->values[j]);
60                     j++;
61                 }
62                 free(node[i]->values);
63             }
64
65             node[i]->element=NULL;
66             node[i]->content=NULL;
67             node[i]->attributes=NULL;
68             node[i]->values=NULL;
69             free(node[i]);
70             node[i]=NULL;
71             i++;
72         }
73         free(node);
74     }
75 }
76
77
78 /** xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
79  * Get the elements by node.
80  */
81 xml_node **OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node)
82 {
83     unsigned int i, k =0,m;
84     xml_node **ret=NULL;
85     xml_node **ret_tmp=NULL;
86
87     if(node == NULL)
88     {
89         m = 0;
90         i = 0;
91     }
92     else
93     {
94         i = node->key;
95         m = _lxml->rl[i++] + 1;
96     }
97
98
99     for(;i<_lxml->cur;i++)
100     {
101         if(_lxml->tp[i] == XML_ELEM)
102         {
103             if((_lxml->rl[i] == m) && (_lxml->el[i] != NULL))
104             {
105                 unsigned int l=i+1;
106                 /* Allocating for xml_node ** */
107                 ret_tmp = (xml_node**)realloc(ret,(k+2)*sizeof(xml_node*));
108                 if(ret_tmp == NULL)
109                     goto fail;
110                 ret = ret_tmp;
111
112                 /* Allocating for the xml_node * */
113                 ret[k] = (xml_node *)calloc(1,sizeof(xml_node));
114                 ret[k+1] = NULL;
115                 if(ret[k] == NULL)
116                     goto fail;
117
118                 ret[k]->element = NULL;
119                 ret[k]->content = NULL;
120                 ret[k]->attributes = NULL;
121                 ret[k]->values = NULL;
122
123                 /* Getting the element */
124                 ret[k]->element=strdup(_lxml->el[i]);
125                 if(ret[k]->element == NULL)
126                 {
127                     goto fail;
128                 }
129
130                 /* Getting the content */
131                 if(_lxml->ct[i])
132                 {
133                     ret[k]->content=strdup(_lxml->ct[i]);
134                     if(ret[k]->content == NULL)
135                         goto fail;
136                 }
137                 /* Assigning the key */
138                 ret[k]->key = i;
139
140                 /* Getting attributes */
141                 while(l < _lxml->cur)
142                 {
143                     if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == m)&&
144                         (_lxml->el[l]) && (_lxml->ct[l]))
145                         {
146                                 char **tmp;
147                                 tmp = (char**)realloc(ret[k]->attributes, (l-i+1)*sizeof(char*));
148                                 if(tmp == NULL)
149                                         goto fail;
150                                 ret[k]->attributes = tmp;
151                                 ret[k]->attributes[l-i] = NULL;
152                                 tmp = (char**)realloc(ret[k]->values, (l-i+1)*sizeof(char*));
153                                 if(tmp == NULL)
154                                         goto fail;
155                             ret[k]->values = tmp;
156                             ret[k]->values[l-i] = NULL;
157
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]))
162                                 goto fail;
163                             l++;
164                         }
165                     else
166                     {
167                         break;
168                     }
169                 }
170                 k++;
171                 continue;
172             }
173         }
174         if((_lxml->tp[i] == XML_ELEM)&&(m > _lxml->rl[i]))
175         {
176             if(node == NULL)
177                 continue;
178             else
179                 break;
180         }
181     }
182
183     return(ret);
184
185     fail:
186     OS_ClearNode(ret);
187         return (NULL);
188 }
189
190
191 /* EOF */