new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / mem_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "mem_op.h"
11 #include "shared.h"
12
13
14 /* Add pointer to array */
15 void **os_AddPtArray(void *pt, void **array)
16 {
17     size_t i = 0;
18     void **ret = NULL;
19
20     if (array) {
21         while (array[i]) {
22             i++;
23         }
24     }
25
26     os_realloc(array, (i + 2)*sizeof(void *), ret);
27     ret[i] = pt;
28     ret[i + 1] = NULL;
29
30     return (ret);
31 }
32
33 /* Add a string to an array */
34 char **os_AddStrArray(const char *str, char **array)
35 {
36     size_t i = 0;
37     char **ret = NULL;
38     if (array) {
39         while (array[i]) {
40             i++;
41         }
42     }
43
44     os_realloc(array, (i + 2)*sizeof(char *), ret);
45     os_strdup(str, ret[i]);
46     ret[i + 1] = NULL;
47
48     return (ret);
49 }
50
51 /* Check if String is on array (Must be NULL terminated) */
52 int os_IsStrOnArray(const char *str, char **array)
53 {
54     if (!str || !array) {
55         return (0);
56     }
57
58     while (*array) {
59         if (strcmp(*array, str) == 0) {
60             return (1);
61         }
62         array++;
63     }
64     return (0);
65 }
66
67 /* Clear the memory of one char and one char** */
68 void os_FreeArray(char *ch1, char **ch2)
69 {
70     /* Clean char * */
71     if (ch1) {
72         free(ch1);
73         ch1 = NULL;
74     }
75
76     /* Clean chat ** */
77     if (ch2) {
78         char **nch2 = ch2;
79
80         while (*ch2 != NULL) {
81             free(*ch2);
82             ch2++;
83         }
84
85         free(nch2);
86         nch2 = NULL;
87     }
88
89     return;
90 }
91
92 /* Allocate memory at "*at" and copy *str to it
93  * If *at already exist, realloc the memory and strcat str on it
94  * It will return the new string on success or NULL on memory error
95  */
96 char *os_LoadString(char *at, const char *str)
97 {
98     if (at == NULL) {
99         at = strdup(str);
100         if (!at) {
101             merror(MEM_ERROR, __local_name, errno, strerror(errno));
102         }
103         return (at);
104     } else { /* at is not null. Need to reallocate its memory and copy str to it */
105         char *newat;
106         size_t strsize = strlen(str);
107         size_t finalsize = strsize + strlen(at) + 1;
108
109         newat = (char *) realloc(at, finalsize * sizeof(char));
110         if (newat == NULL) {
111             free(at);
112             merror(MEM_ERROR, __local_name, errno, strerror(errno));
113             return (NULL);
114         }
115         at = newat;
116
117         strncat(at, str, strsize);
118         at[finalsize - 1] = '\0';
119
120         return (at);
121     }
122
123     return (NULL);
124 }
125
126 /* Clear memory regardless of compiler optimizations
127  * v = memory to clear
128  * c = character to set
129  * n = memory size to clear
130  */
131 void *memset_secure(void *v, int c, size_t n)
132 {
133     volatile unsigned char *p = (volatile unsigned char *)v;
134     while (n--) {
135         *p++ = (unsigned char) c;
136     }
137
138     return v;
139 }
140