Imported Upstream version 2.7
[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
24
25 /* OS_ClearNode v0,1
26  * Clear the Node structure
27  */
28 void OS_ClearNode(xml_node **node)
29 {
30     if(node)
31     {
32         int i=0;
33         while(node[i])
34         {
35             if(node[i]->element)
36             {
37                 free(node[i]->element);
38             }
39             if(node[i]->content)
40             {
41                 free(node[i]->content);
42             }
43             if(node[i]->attributes)
44             {
45                 int j=0;
46                 while(node[i]->attributes[j])
47                 {
48                     free(node[i]->attributes[j]);
49                     j++;
50                 }
51                 free(node[i]->attributes);
52             }
53             if(node[i]->values)
54             {
55                 int j=0;
56                 while(node[i]->values[j])
57                 {
58                     free(node[i]->values[j]);
59                     j++;
60                 }
61                 free(node[i]->values);
62             }
63
64             node[i]->element=NULL;
65             node[i]->content=NULL;
66             node[i]->attributes=NULL;
67             node[i]->values=NULL;
68             free(node[i]);
69             node[i]=NULL;
70             i++;
71         }
72         free(node);
73         node=NULL;
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(OS_XML *_lxml, xml_node *node)
82 {
83     int i,j,k=0;
84     xml_node **ret=NULL;
85
86     if(node == NULL)
87     {
88         j = -1;
89         i = 0;
90     }
91     else
92     {
93         i = node->key;
94         j = _lxml->rl[i++];
95     }
96
97
98     for(;i<_lxml->cur;i++)
99     {
100         if(_lxml->tp[i] == XML_ELEM)
101         {
102             if((_lxml->rl[i] == j+1) && (_lxml->el[i] != NULL))
103             {
104                 int l=i+1;
105                 /* Allocating for xml_node ** */
106                 ret = (xml_node**)realloc(ret,(k+1)*sizeof(xml_node*));
107                 if(ret == NULL)
108                     return(NULL);
109
110                 /* Allocating for the xml_node * */
111                 ret[k] = (xml_node *)calloc(1,sizeof(xml_node));
112                 if(ret[k] == NULL)
113                     return(NULL);
114
115                 ret[k]->element = NULL;
116                 ret[k]->content = NULL;
117                 ret[k]->attributes = NULL;
118                 ret[k]->values = NULL;
119
120                 /* Getting the element */
121                 ret[k]->element=strdup(_lxml->el[i]);
122                 if(ret[k]->element == NULL)
123                 {
124                     free(ret);
125                     return(NULL);
126                 }
127
128                 /* Getting the content */
129                 if(_lxml->ct[i])
130                 {
131                     ret[k]->content=strdup(_lxml->ct[i]);
132                     if(ret[k]->content == NULL)
133                         return(NULL);
134                 }
135                 /* Assigning the key */
136                 ret[k]->key = i;
137
138                 /* Getting attributes */
139                 while(l < _lxml->cur)
140                 {
141                     if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == j+1)&&
142                         (_lxml->el[l]) && (_lxml->ct[l]))
143                         {
144                             ret[k]->attributes =
145                                 (char**)realloc(ret[k]->attributes,
146                                                 (l-i+1)*sizeof(char*));
147                             ret[k]->values =
148                                 (char**)realloc(ret[k]->values,
149                                                 (l-i+1)*sizeof(char*));
150                             if(!(ret[k]->attributes) ||
151                                     !(ret[k]->values))
152                                 return(NULL);
153                             ret[k]->attributes[l-i-1]=strdup(_lxml->el[l]);
154                             ret[k]->values[l-i-1] = strdup(_lxml->ct[l]);
155                             if(!(ret[k]->attributes[l-i-1]) ||
156                                     !(ret[k]->values[l-i-1]))
157                                 return(NULL);
158                             l++;
159                         }
160                     else
161                     {
162                         break;
163                     }
164                 }
165                 if(ret[k]->attributes)
166                 {
167                     ret[k]->attributes[l-i-1] = NULL;
168                     ret[k]->values[l-i-1] = NULL;
169                 }
170                 k++;
171                 continue;
172             }
173         }
174         if((_lxml->tp[i] == XML_ELEM)&&(j+1 > _lxml->rl[i]))
175         {
176             if(j == -1)
177                 continue;
178             else
179                 break;
180         }
181     }
182
183     if(ret ==NULL)
184         return(NULL);
185
186     ret = (xml_node **)realloc(ret,(k+1)*sizeof(xml_node *));
187     if(ret == NULL)
188         return(NULL);
189     ret[k]=NULL;
190     return(ret);
191 }
192
193
194 /* EOF */