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