Imported Upstream version 2.3
[ossec-hids.git] / src / os_xml / os_xml_node_access.c
1 /* @(#) $Id: os_xml_node_access.c,v 1.6 2009/06/24 17:06:32 dcid Exp $ */
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 3) 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                 free(node[i]->element);
36             if(node[i]->content)
37                 free(node[i]->content);
38             if(node[i]->attributes)
39             {
40                 int j=0;
41                 while(node[i]->attributes[j])
42                 {
43                     free(node[i]->attributes[j]);
44                     j++;
45                 }
46                 free(node[i]->attributes);
47             }
48             if(node[i]->values)
49             {
50                 int j=0;
51                 while(node[i]->values[j])
52                 {
53                     free(node[i]->values[j]);
54                     j++;
55                 }
56                 free(node[i]->values);
57             }
58
59             node[i]->element=NULL;
60             node[i]->content=NULL;
61             node[i]->attributes=NULL;
62             node[i]->values=NULL;
63             free(node[i]);
64             node[i]=NULL;
65             i++;    
66         }
67         free(node);
68         node=NULL;
69     }
70 }
71
72
73 /** xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
74  * Get the elements by node.
75  */
76 xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
77 {
78     int i,j,k=0;
79     xml_node **ret=NULL;
80
81     if(node == NULL)
82     {
83         j = -1;
84         i = 0;
85     }
86     else
87     {
88         i = node->key;
89         j = _lxml->rl[i++];
90     }
91         
92             
93     for(;i<_lxml->cur;i++)
94     {
95         if(_lxml->tp[i] == XML_ELEM)
96         {
97             if((_lxml->rl[i] == j+1) && (_lxml->el[i] != NULL))
98             {
99                 int l=i+1;
100                 /* Allocating for xml_node ** */
101                 ret = (xml_node**)realloc(ret,(k+1)*sizeof(xml_node*));
102                 if(ret == NULL)
103                     return(NULL);
104                     
105                 /* Allocating for the xml_node * */
106                 ret[k] = (xml_node *)calloc(1,sizeof(xml_node));
107                 if(ret[k] == NULL)
108                     return(NULL);
109     
110                 ret[k]->element = NULL;
111                 ret[k]->content = NULL;
112                 ret[k]->attributes = NULL;
113                 ret[k]->values = NULL;
114                                 
115                 /* Getting the element */
116                 ret[k]->element=strdup(_lxml->el[i]);
117                 if(ret[k]->element == NULL)
118                 {
119                     free(ret);
120                     return(NULL);
121                 }
122                 
123                 /* Getting the content */
124                 if(_lxml->ct[i])
125                 {
126                     ret[k]->content=strdup(_lxml->ct[i]);
127                     if(ret[k]->content == NULL)
128                         return(NULL);
129                 }
130                 /* Assigning the key */
131                 ret[k]->key = i;
132
133                 /* Getting attributes */
134                 while(l < _lxml->cur)
135                 {
136                     if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == j+1)&&
137                         (_lxml->el[l]) && (_lxml->ct[l]))
138                         {
139                             ret[k]->attributes = 
140                                 (char**)realloc(ret[k]->attributes,
141                                                 (l-i+1)*sizeof(char*));
142                             ret[k]->values = 
143                                 (char**)realloc(ret[k]->values,
144                                                 (l-i+1)*sizeof(char*));
145                             if(!(ret[k]->attributes) || 
146                                     !(ret[k]->values))
147                                 return(NULL);
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]))
152                                 return(NULL);
153                             l++;                    
154                         }
155                     else
156                     {
157                         break;
158                     }
159                 }
160                 if(ret[k]->attributes)
161                 {
162                     ret[k]->attributes[l-i-1] = NULL;
163                     ret[k]->values[l-i-1] = NULL;
164                 }
165                 k++;
166                 continue;
167             }
168         }
169         if((_lxml->tp[i] == XML_ELEM)&&(j+1 > _lxml->rl[i]))
170         {
171             if(j == -1)
172                 continue;
173             else
174                 break;
175         }
176     }
177     
178     if(ret ==NULL)
179         return(NULL);
180
181     ret = (xml_node **)realloc(ret,(k+1)*sizeof(xml_node *));
182     if(ret == NULL)
183         return(NULL);
184     ret[k]=NULL;
185     return(ret);
186 }
187
188
189 /* EOF */