]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/array.c
Only log "IDENT ... no result" when IDENT was looked up
[ngircd-alex.git] / src / ngircd / array.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  * Please read the file COPYING, README and AUTHORS for more information.
7  *
8  * libarray - dynamically allocate arrays.
9  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
10  */
11
12 /**
13  * @file
14  * Functions to dynamically allocate arrays.
15  */
16
17 #include "array.h"
18
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "log.h"
24
25 /* Enable more Debug messages in alloc / append / memmove code. */
26 /* #define DEBUG_ARRAY */
27
28 #define array_UNUSABLE(x)       ( !(x)->mem )
29
30
31 static bool
32 safemult_sizet(size_t a, size_t b, size_t *res)
33 {
34         size_t tmp = a * b;
35
36         if (b && (tmp / b != a))
37                 return false;
38
39         *res = tmp;
40         return true;
41 }
42
43
44 void
45 array_init(array *a)
46 {
47         assert(a != NULL);
48         a->mem = NULL;
49         a->allocated = 0;
50         a->used = 0;
51 }
52
53
54 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
55 void *
56 array_alloc(array * a, size_t size, size_t pos)
57 {
58         size_t alloc, pos_plus1 = pos + 1;
59         char *tmp;
60
61         assert(size > 0);
62
63         if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
64                 return NULL;
65
66         if (a->allocated < alloc) {
67 #ifdef DEBUG_ARRAY
68                 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
69                     a->allocated, alloc);
70 #endif
71                 tmp = realloc(a->mem, alloc);
72                 if (!tmp)
73                         return NULL;
74
75                 a->mem = tmp;
76                 a->allocated = alloc;
77                 memset(a->mem + a->used, 0, a->allocated - a->used);
78                 a->used = alloc;
79         }
80
81         assert(a->allocated >= a->used);
82
83         return a->mem + (pos * size);
84 }
85
86
87 /*return number of initialized ELEMS in a. */
88 size_t
89 array_length(const array * const a, size_t membersize)
90 {
91         assert(a != NULL);
92         assert(membersize > 0);
93
94         if (array_UNUSABLE(a))
95                 return 0;
96
97         assert(a->allocated);
98         return membersize ? a->used / membersize : 0;
99 }
100
101
102 /* copy array src to array dest */
103 bool
104 array_copy(array * dest, const array * const src)
105 {
106         if (array_UNUSABLE(src))
107                 return false;
108
109         assert(src->allocated);
110         return array_copyb(dest, src->mem, src->used);
111 }
112
113
114 /* return false on failure (realloc failure, invalid src/dest array) */
115 bool
116 array_copyb(array * dest, const char *src, size_t len)
117 {
118         assert(dest != NULL);
119         assert(src != NULL );
120
121         if (!src || !dest)
122                 return false;
123
124         array_trunc(dest);
125         return array_catb(dest, src, len);
126 }
127
128
129 /* copy string to dest */
130 bool
131 array_copys(array * dest, const char *src)
132 {
133         return array_copyb(dest, src, strlen(src));
134 }
135
136
137 /* append len bytes from src to the array dest.
138 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
139 bool
140 array_catb(array * dest, const char *src, size_t len)
141 {
142         size_t tmp;
143         size_t used;
144         char *ptr;
145
146         assert(dest != NULL);
147         assert(src != NULL);
148
149         if (!len)
150                 return true;
151
152         if (!src || !dest)
153                 return false;
154
155         used = dest->used;
156         tmp = used + len;
157
158         if (tmp < used || tmp < len)    /* integer overflow */
159                 return false;
160
161         if (!array_alloc(dest, 1, tmp))
162                 return false;
163
164         ptr = dest->mem;
165
166         assert(ptr != NULL);
167
168 #ifdef DEBUG_ARRAY
169         Log(LOG_DEBUG,
170             "array_catb(): appending %u bytes to array (now %u bytes in array).",
171             len, tmp);
172 #endif
173         memcpy(ptr + used, src, len);
174         dest->used = tmp;
175         return true;
176 }
177
178
179 /* append string to dest */
180 bool
181 array_cats(array * dest, const char *src)
182 {
183         return array_catb(dest, src, strlen(src));
184 }
185
186
187 /* append trailing NUL byte to array */
188 bool
189 array_cat0(array * a)
190 {
191         return array_catb(a, "", 1);
192 }
193
194
195 /* append trailing NUL byte to array, but do not count it. */
196 bool
197 array_cat0_temporary(array * a)
198 {
199         char *endpos = array_alloc(a, 1, array_bytes(a));
200         if (!endpos)
201                 return false;
202
203         *endpos = '\0';
204         return true;
205 }
206
207 /* add contents of array src to array dest. */
208 bool
209 array_cat(array * dest, const array * const src)
210 {
211         if (array_UNUSABLE(src))
212                 return false;
213
214         return array_catb(dest, src->mem, src->used);
215 }
216
217
218 /* return pointer to the element at pos.
219    return NULL if the array is unallocated, or if pos is larger than
220    the number of elements stored int the array. */
221 void *
222 array_get(array * a, size_t membersize, size_t pos)
223 {
224         size_t totalsize;
225         size_t posplus1 = pos + 1;
226
227         assert(membersize > 0);
228         assert(a != NULL);
229
230         if (!posplus1 || array_UNUSABLE(a))
231                 return NULL;
232
233         if (!safemult_sizet(posplus1, membersize, &totalsize))
234                 return NULL;
235
236         if (a->allocated < totalsize)
237                 return NULL;
238
239         totalsize = pos * membersize;
240         return a->mem + totalsize;
241 }
242
243
244 void
245 array_free(array * a)
246 {
247         assert(a != NULL);
248 #ifdef DEBUG_ARRAY
249         Log(LOG_DEBUG,
250             "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
251             a->allocated, a->used);
252 #endif
253         free(a->mem);
254         a->mem = NULL;
255         a->allocated = 0;
256         a->used = 0;
257 }
258
259 void
260 array_free_wipe(array *a)
261 {
262         size_t bytes = a->allocated;
263         if (bytes)
264                 memset(a->mem, 0, bytes);
265         array_free(a);
266 }
267
268 void *
269 array_start(const array * const a)
270 {
271         assert(a != NULL);
272         return a->mem;
273 }
274
275
276 void
277 array_trunc(array * a)
278 {
279         assert(a != NULL);
280         a->used = 0;
281 }
282
283
284 void
285 array_truncate(array * a, size_t membersize, size_t len)
286 {
287         size_t newlen;
288         assert(a != NULL);
289         if (!safemult_sizet(membersize, len, &newlen))
290                 return;
291
292         if (newlen <= a->allocated)
293                 a->used = newlen;
294 }
295
296
297 /* move elements starting at pos to beginning of array */
298 void
299 array_moveleft(array * a, size_t membersize, size_t pos)
300 {
301         size_t bytepos;
302
303         assert(a != NULL);
304         assert(membersize > 0);
305
306         if (!safemult_sizet(membersize, pos, &bytepos)) {
307                 a->used = 0;
308                 return;
309         }
310
311         if (!bytepos)
312                 return; /* nothing to do */
313
314 #ifdef DEBUG_ARRAY
315         Log(LOG_DEBUG,
316             "array_moveleft(): %u bytes used in array, starting at position %u.",
317             a->used, bytepos);
318 #endif
319         if (a->used <= bytepos) {
320                 a->used = 0;
321                 return;
322         }
323
324         a->used -= bytepos;
325         memmove(a->mem, a->mem + bytepos, a->used);
326 }
327
328 /* -eof- */