X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?p=ossec-hids.git;a=blobdiff_plain;f=src%2Fshared%2Fmem_op.c;h=8ba4ccb6c122cee9b88aa94933e7f3c33b0e6239;hp=b58385d1eb027eda83497df3cf0e25c3fda29c0c;hb=6ef2f786c6c8ead94841b5f93baf9f43421f08c8;hpb=914feba5d54f979cd5d7e69c349c3d01f630042a diff --git a/src/shared/mem_op.c b/src/shared/mem_op.c index b58385d..8ba4ccb 100755 --- a/src/shared/mem_op.c +++ b/src/shared/mem_op.c @@ -1,11 +1,12 @@ -/* @(#) $Id: mem_op.c,v 1.7 2009/06/24 18:53:08 dcid Exp $ */ +/* @(#) $Id: ./src/shared/mem_op.c, 2011/09/08 dcid Exp $ + */ /* 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 */ @@ -13,6 +14,49 @@ #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) { @@ -42,22 +86,22 @@ void os_FreeArray(char *ch1, char **ch2) free(ch1); ch1 = NULL; } - + /* Cleaning chat ** */ if(ch2) { char **nch2 = ch2; - + while(*ch2 != NULL) { free(*ch2); ch2++; } - + free(nch2); nch2 = NULL; } - + return; } @@ -72,50 +116,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; + int finalsize = strsize + strlen(at) + 1; - if((atsize > OS_SIZE_2048) || (strsize > OS_SIZE_2048)) - { - merror(SIZE_ERROR,ARGV0,str); - return(NULL); - } - - at = realloc(at, (finalsize)*sizeof(char)); - - 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); }