novi upstream verzije 2.8.3
[ossec-hids.git] / src / os_xml / os_xml_node_access.c
index 6fba555..619eec9 100755 (executable)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 
 #include "os_xml.h"
+#include "os_xml_internal.h"
 
 
 /* OS_ClearNode v0,1
@@ -70,7 +71,6 @@ void OS_ClearNode(xml_node **node)
             i++;
         }
         free(node);
-        node=NULL;
     }
 }
 
@@ -78,20 +78,21 @@ void OS_ClearNode(xml_node **node)
 /** xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
  * Get the elements by node.
  */
-xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
+xml_node **OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node)
 {
-    int i,j,k=0;
+    unsigned int i, k =0,m;
     xml_node **ret=NULL;
+    xml_node **ret_tmp=NULL;
 
     if(node == NULL)
     {
-        j = -1;
+        m = 0;
         i = 0;
     }
     else
     {
         i = node->key;
-        j = _lxml->rl[i++];
+        m = _lxml->rl[i++] + 1;
     }
 
 
@@ -99,18 +100,20 @@ xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
     {
         if(_lxml->tp[i] == XML_ELEM)
         {
-            if((_lxml->rl[i] == j+1) && (_lxml->el[i] != NULL))
+            if((_lxml->rl[i] == m) && (_lxml->el[i] != NULL))
             {
-                int l=i+1;
+                unsigned int l=i+1;
                 /* Allocating for xml_node ** */
-                ret = (xml_node**)realloc(ret,(k+1)*sizeof(xml_node*));
-                if(ret == NULL)
-                    return(NULL);
+                ret_tmp = (xml_node**)realloc(ret,(k+2)*sizeof(xml_node*));
+                if(ret_tmp == NULL)
+                    goto fail;
+                ret = ret_tmp;
 
                 /* Allocating for the xml_node * */
                 ret[k] = (xml_node *)calloc(1,sizeof(xml_node));
+                ret[k+1] = NULL;
                 if(ret[k] == NULL)
-                    return(NULL);
+                    goto fail;
 
                 ret[k]->element = NULL;
                 ret[k]->content = NULL;
@@ -121,8 +124,7 @@ xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
                 ret[k]->element=strdup(_lxml->el[i]);
                 if(ret[k]->element == NULL)
                 {
-                    free(ret);
-                    return(NULL);
+                    goto fail;
                 }
 
                 /* Getting the content */
@@ -130,7 +132,7 @@ xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
                 {
                     ret[k]->content=strdup(_lxml->ct[i]);
                     if(ret[k]->content == NULL)
-                        return(NULL);
+                        goto fail;
                 }
                 /* Assigning the key */
                 ret[k]->key = i;
@@ -138,23 +140,26 @@ xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
                 /* Getting attributes */
                 while(l < _lxml->cur)
                 {
-                    if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == j+1)&&
+                    if((_lxml->tp[l] == XML_ATTR)&&(_lxml->rl[l] == m)&&
                         (_lxml->el[l]) && (_lxml->ct[l]))
                         {
-                            ret[k]->attributes =
-                                (char**)realloc(ret[k]->attributes,
-                                                (l-i+1)*sizeof(char*));
-                            ret[k]->values =
-                                (char**)realloc(ret[k]->values,
-                                                (l-i+1)*sizeof(char*));
-                            if(!(ret[k]->attributes) ||
-                                    !(ret[k]->values))
-                                return(NULL);
+                               char **tmp;
+                               tmp = (char**)realloc(ret[k]->attributes, (l-i+1)*sizeof(char*));
+                               if(tmp == NULL)
+                                       goto fail;
+                               ret[k]->attributes = tmp;
+                               ret[k]->attributes[l-i] = NULL;
+                               tmp = (char**)realloc(ret[k]->values, (l-i+1)*sizeof(char*));
+                               if(tmp == NULL)
+                                       goto fail;
+                            ret[k]->values = tmp;
+                            ret[k]->values[l-i] = NULL;
+
                             ret[k]->attributes[l-i-1]=strdup(_lxml->el[l]);
                             ret[k]->values[l-i-1] = strdup(_lxml->ct[l]);
                             if(!(ret[k]->attributes[l-i-1]) ||
                                     !(ret[k]->values[l-i-1]))
-                                return(NULL);
+                                goto fail;
                             l++;
                         }
                     else
@@ -162,32 +167,24 @@ xml_node **OS_GetElementsbyNode(OS_XML *_lxml, xml_node *node)
                         break;
                     }
                 }
-                if(ret[k]->attributes)
-                {
-                    ret[k]->attributes[l-i-1] = NULL;
-                    ret[k]->values[l-i-1] = NULL;
-                }
                 k++;
                 continue;
             }
         }
-        if((_lxml->tp[i] == XML_ELEM)&&(j+1 > _lxml->rl[i]))
+        if((_lxml->tp[i] == XML_ELEM)&&(m > _lxml->rl[i]))
         {
-            if(j == -1)
+            if(node == NULL)
                 continue;
             else
                 break;
         }
     }
 
-    if(ret ==NULL)
-        return(NULL);
-
-    ret = (xml_node **)realloc(ret,(k+1)*sizeof(xml_node *));
-    if(ret == NULL)
-        return(NULL);
-    ret[k]=NULL;
     return(ret);
+
+    fail:
+    OS_ClearNode(ret);
+       return (NULL);
 }