1 /*
2   dbuf.c - Dynamic buffer implementation
3   version 1.4.0, 2011-03-27
4 
5   Copyright (c) 2002-2011 Borut Razem
6 
7   This software is provided 'as-is', without any express or implied
8   warranty.  In no event will the authors be held liable for any damages
9   arising from the use of this software.
10 
11   Permission is granted to anyone to use this software for any purpose,
12   including commercial applications, and to alter it and redistribute it
13   freely, subject to the following restrictions:
14 
15   1. The origin of this software must not be misrepresented; you must not
16      claim that you wrote the original software. If you use this software
17      in a product, an acknowledgment in the product documentation would be
18      appreciated but is not required.
19   2. Altered source versions must be plainly marked as such, and must not be
20      misrepresented as being the original software.
21   3. This notice may not be removed or altered from any source distribution.
22 
23   Borut Razem
24   borut.razem@siol.net
25 */
26 
27 
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include "dbuf.h"
32 
33 
34 /*
35  * Assure that the buffer is large enough to hold
36  * current length + size bytes; enlarge it if necessary.
37  *
38  * Intended for internal use.
39  */
40 
_dbuf_expand(struct dbuf_s * dbuf,size_t size)41 int _dbuf_expand(struct dbuf_s *dbuf, size_t size)
42 {
43   assert(dbuf->alloc != 0);
44   assert(dbuf->buf != NULL);
45 
46   if (dbuf->len + size > dbuf->alloc) {
47     /* new_allocated_size = current_allocated_size * 2^n */
48     /* can this be optimized? */
49     do {
50       dbuf->alloc += dbuf->alloc;
51     }
52     while (dbuf->len + size > dbuf->alloc);
53 
54     if ((dbuf->buf = realloc(dbuf->buf, dbuf->alloc)) == NULL)
55       return 0;
56   }
57 
58   return 1;
59 }
60 
61 
62 /*
63  * Initialize the dbuf structure and
64  * allocate buffer to hold size bytes.
65  */
66 
dbuf_init(struct dbuf_s * dbuf,size_t size)67 int dbuf_init(struct dbuf_s *dbuf, size_t size)
68 {
69   assert(size != 0);
70 
71   if (size == 0)
72     size = 1;
73 
74   dbuf->len = 0;
75   dbuf->alloc = size;
76   return ((dbuf->buf = malloc(dbuf->alloc)) != NULL);
77 }
78 
79 
80 /*
81  * Test if dbuf is initialized.
82  * NOTE: the dbuf structure should be set
83  * to all zeroes at definition:
84  * struct dbuf_s dbuf = { 0 };
85  *
86  * See: dbuf_init()
87  */
88 
dbuf_is_initialized(struct dbuf_s * dbuf)89 int dbuf_is_initialized (struct dbuf_s *dbuf)
90 {
91   if (dbuf->buf == NULL) {
92     assert(dbuf->alloc == 0);
93     assert(dbuf->len == 0);
94     return 0;
95   }
96   else {
97     assert(dbuf->alloc != 0);
98     assert(dbuf->len >= 0 && dbuf->len <= dbuf->alloc);
99     return 1;
100   }
101 }
102 
103 /*
104  * Allocate new dbuf structure on the heap
105  * and initialize it.
106  *
107  * See: dbuf_delete()
108  */
109 
dbuf_new(size_t size)110 struct dbuf_s *dbuf_new(size_t size)
111 {
112   struct dbuf_s *dbuf;
113 
114   dbuf = (struct dbuf_s *)malloc(sizeof(struct dbuf_s));
115   if (dbuf != NULL) {
116     if (dbuf_init(dbuf, size) == 0) {
117       free(dbuf);
118       return NULL;
119     }
120   }
121   return dbuf;
122 }
123 
124 
125 /*
126  * Set the buffer size. Buffer size can be only decreased.
127  */
128 
dbuf_set_length(struct dbuf_s * dbuf,size_t len)129 int dbuf_set_length(struct dbuf_s *dbuf, size_t len)
130 {
131   if (!dbuf_is_initialized (dbuf))
132     dbuf_init (dbuf, len ? len : 1);
133 
134   assert(dbuf != NULL);
135   assert(dbuf->alloc != 0);
136   assert(len <= dbuf->len);
137 
138   if (len <= dbuf->len) {
139     dbuf->len = len;
140     return 1;
141   }
142 
143   return 0;
144 }
145 
146 
147 /*
148  * Append the buf to the end of the buffer.
149  */
150 
dbuf_append(struct dbuf_s * dbuf,const void * buf,size_t len)151 int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t len)
152 {
153   assert(dbuf != NULL);
154   assert(dbuf->alloc != 0);
155   assert(dbuf->buf != NULL);
156 
157   if (_dbuf_expand(dbuf, len) != 0) {
158     memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, len);
159     dbuf->len += len;
160     return 1;
161   }
162 
163   return 0;
164 }
165 
166 /*
167  * Prepend the buf to the beginning of the buffer.
168  */
169 
dbuf_prepend(struct dbuf_s * dbuf,const void * buf,size_t len)170 int dbuf_prepend(struct dbuf_s *dbuf, const void *buf, size_t len)
171 {
172   assert(dbuf);
173   assert(dbuf->alloc);
174   assert(dbuf->buf);
175 
176   if (_dbuf_expand(dbuf, len) != 0) {
177     memmove(&(((char *)dbuf->buf)[len]), dbuf->buf, dbuf->len);
178     memcpy(dbuf->buf, buf, len);
179     dbuf->len += len;
180     return 1;
181   }
182 
183   return 0;
184 }
185 
186 /*
187  * Add '\0' character at the end of the buffer without
188  * count it in the dbuf->len.
189  */
190 
dbuf_c_str(struct dbuf_s * dbuf)191 const char *dbuf_c_str(struct dbuf_s *dbuf)
192 {
193   assert(dbuf != NULL);
194   assert(dbuf->alloc != 0);
195   assert(dbuf->buf != NULL);
196 
197   if (_dbuf_expand(dbuf, 1) != 0) {
198     ((char *)dbuf->buf)[dbuf->len] = '\0';
199     return dbuf->buf;
200   }
201 
202   return NULL;
203 }
204 
205 
206 /*
207  * Get the buffer pointer.
208  */
209 
dbuf_get_buf(const struct dbuf_s * dbuf)210 const void *dbuf_get_buf(const struct dbuf_s *dbuf)
211 {
212   assert(dbuf != NULL);
213   assert(dbuf->alloc != 0);
214   assert(dbuf->buf != NULL);
215 
216   return dbuf->buf;
217 }
218 
219 
220 /*
221  * Get the buffer length.
222  */
223 
dbuf_get_length(const struct dbuf_s * dbuf)224 size_t dbuf_get_length(const struct dbuf_s *dbuf)
225 {
226   assert(dbuf != NULL);
227   assert(dbuf->alloc != 0);
228   assert(dbuf->buf != NULL);
229 
230   return dbuf->len;
231 }
232 
233 
234 /*
235  * Trim the allocated buffer to required size
236  */
237 
dbuf_trim(struct dbuf_s * dbuf)238 int dbuf_trim(struct dbuf_s *dbuf)
239 {
240   void *buf;
241 
242   assert(dbuf != NULL);
243   assert(dbuf->alloc != 0);
244   assert(dbuf->buf != NULL);
245 
246   buf = realloc(dbuf->buf, dbuf->len);
247 
248   if (buf != NULL) {
249     dbuf->alloc = dbuf->len;
250     dbuf->buf = buf;
251   }
252 
253   return buf != NULL;
254 }
255 
256 
257 /*
258  * Detach the buffer from dbuf structure.
259  * The dbuf structure can be reused by
260  * reinitializing it.
261  *
262  * See: dbuf_init()
263  */
264 
dbuf_detach(struct dbuf_s * dbuf)265 void *dbuf_detach(struct dbuf_s *dbuf)
266 {
267   void *ret;
268 
269   assert(dbuf != NULL);
270   assert(dbuf->alloc != 0);
271   assert(dbuf->buf != NULL);
272 
273   ret = dbuf->buf;
274   dbuf->buf = NULL;
275   dbuf->len = 0;
276   dbuf->alloc = 0;
277 
278   return ret;
279 }
280 
281 
282 /*
283  * Add '\0' character at the end of the buffer without
284  * count it in the dbuf->len and detach the buffer from dbuf structure.
285  * The dbuf structure can be reused by reinitializing it.
286  *
287  * See: dbuf_init()
288  */
289 
dbuf_detach_c_str(struct dbuf_s * dbuf)290 char *dbuf_detach_c_str(struct dbuf_s *dbuf)
291 {
292   dbuf_c_str(dbuf);
293   return dbuf_detach(dbuf);
294 }
295 
296 
297 /*
298  * Destroy the dbuf structure and
299  * free the buffer
300  */
301 
dbuf_destroy(struct dbuf_s * dbuf)302 void dbuf_destroy(struct dbuf_s *dbuf)
303 {
304   free(dbuf_detach(dbuf));
305 }
306 
307 
308 /*
309  * Delete dbuf structure on the heap:
310  * destroy it and free the allocated space.
311  * The user's responsablity is not to use
312  * the pointer any more: the best think to do
313  * is to set the pointer to NULL value.
314  *
315  * See dbuf_new()
316  */
317 
dbuf_delete(struct dbuf_s * dbuf)318 void dbuf_delete(struct dbuf_s *dbuf)
319 {
320   dbuf_destroy(dbuf);
321   free(dbuf);
322 }
323 
324 
325 /*
326  * Free detached buffer.
327  *
328  * See dbuf_detach()
329  */
330 
dbuf_free(const void * buf)331 void dbuf_free(const void *buf)
332 {
333   free((void *)buf);
334 }
335