Merge commit 'v2.5.1'
[ossec-hids.git] / src / shared / mem_op.c
index b58385d..d54bc02 100755 (executable)
@@ -1,11 +1,11 @@
-/* @(#) $Id: mem_op.c,v 1.7 2009/06/24 18:53:08 dcid Exp $ */
+/* @(#) $Id$ */
 
 /* Copyright (C) 2009 Trend Micro Inc.
  * All rights reserved.
  *
  * This program is a free software; you can redistribute it
  * and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
  * Foundation
  */
 
 #include "mem_op.h"
 
 
+/* Add pointer to array. */
+void **os_AddPtArray(void *pt, void **array)
+{
+    int i = 0;
+    void **ret = NULL;
+
+    if(array)
+    {
+        while(array[i])
+        {
+            i++;
+        }
+    }
+
+    os_realloc(array, (i + 2)*sizeof(char *), ret);
+    ret[i] = pt;
+    ret[i + 1] = NULL;
+
+    return(ret);
+}
+
+
+/* Add a string to an array. */
+char **os_AddStrArray(char *str, char **array)
+{
+    int i = 0;
+    char **ret = NULL;
+    if(array)
+    {
+        while(array[i])
+        {
+            i++;
+        }
+    }
+
+    os_realloc(array, (i + 2)*sizeof(char *), ret);
+    os_strdup(str, ret[i]);
+    ret[i + 1] = NULL;
+
+    return(ret);
+}
+
+
 /* Check if String is on array (Must be NULL terminated) */
 int os_IsStrOnArray(char *str, char **array)
 {
@@ -72,50 +115,34 @@ char *os_LoadString(char *at, char *str)
 {
     if(at == NULL)
     {
-        int strsize = 0;
-        if((strsize = strlen(str)) < OS_SIZE_2048)
+        at = strdup(str);
+        if(!at)
         {
-            at = calloc(strsize+1,sizeof(char));
-            if(at == NULL)
-            {
-                merror(MEM_ERROR,ARGV0);
-                return(NULL);
-            }
-            strncpy(at, str, strsize);
-            return(at);
-        }
-        else
-        {
-            merror(SIZE_ERROR,ARGV0,str);
-            return(NULL);
+            merror(MEM_ERROR,ARGV0);
         }
+        return(at);
     }
     else /*at is not null. Need to reallocat its memory and copy str to it*/
     {
+        char *newat;
         int strsize = strlen(str);
-        int atsize = strlen(at);
-        int finalsize = atsize+strsize+1;
-
-        if((atsize > OS_SIZE_2048) || (strsize > OS_SIZE_2048))
-        {
-            merror(SIZE_ERROR,ARGV0,str);
-            return(NULL);
-        }
-
-        at = realloc(at, (finalsize)*sizeof(char));
+        int finalsize = strsize + strlen(at) + 1;
 
-        if(at == NULL)
+        newat = realloc(at, finalsize*sizeof(char));
+        if(newat == NULL)
         {
+            free(at);
             merror(MEM_ERROR,ARGV0);
             return(NULL);
         }
+        at = newat;
 
-        strncat(at,str,strsize);
-
-        at[finalsize-1] = '\0';
+        strncat(at, str, strsize);
+        at[finalsize -1] = '\0';
 
         return(at);
     }
+
     return(NULL);
 }