Imported Upstream version 2.9.0
[debian/cc1111] / support / Util / dbuf.c
1 /*
2   dbuf.c - Dynamic buffer implementation
3   version 1.2.0, January 6th, 2007
4
5   Copyright (c) 2002-2007 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
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
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  * Allocate new dbuf structure on the heap
82  * and initialize it.
83  *
84  * See: dbuf_delete()
85  */
86
87 struct dbuf_s *dbuf_new(size_t size)
88 {
89   struct dbuf_s *dbuf;
90
91   dbuf = (struct dbuf_s *)malloc(sizeof(struct dbuf_s));
92   if (dbuf != NULL) {
93     if (dbuf_init(dbuf, size) == 0) {
94       free(dbuf);
95       return NULL;
96     }
97   }
98   return dbuf;
99 }
100
101
102 /*
103  * Set the buffer size. Buffer size can be only decreased.
104  */
105
106 int dbuf_set_length(struct dbuf_s *dbuf, size_t len)
107 {
108   assert(dbuf != NULL);
109   assert(dbuf->alloc != 0);
110   assert(len <= dbuf->len);
111
112   if (len <= dbuf->len) {
113     dbuf->len = len;
114     return 1;
115   }
116
117   return 0;
118 }
119
120
121 /*
122  * Append the buf to the end of the buffer.
123  */
124
125 int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t len)
126 {
127   assert(dbuf != NULL);
128   assert(dbuf->alloc != 0);
129   assert(dbuf->buf != NULL);
130
131   if (_dbuf_expand(dbuf, len) != 0) {
132     memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, len);
133     dbuf->len += len;
134     return 1;
135   }
136
137   return 0;
138 }
139
140
141 /*
142  * Add '\0' character at the end of the buffer without
143  * count it in the dbuf->len.
144  */
145
146 const char *dbuf_c_str(struct dbuf_s *dbuf)
147 {
148   assert(dbuf != NULL);
149   assert(dbuf->alloc != 0);
150   assert(dbuf->buf != NULL);
151
152   if (_dbuf_expand(dbuf, 1) != 0) {
153     ((char *)dbuf->buf)[dbuf->len] = '\0';
154     return dbuf->buf;
155   }
156
157   return NULL;
158 }
159
160
161 /*
162  * Get the buffer pointer.
163  */
164
165 const void *dbuf_get_buf(struct dbuf_s *dbuf)
166 {
167   assert(dbuf != NULL);
168   assert(dbuf->alloc != 0);
169   assert(dbuf->buf != NULL);
170
171   return dbuf->buf;
172 }
173
174
175 /*
176  * Get the buffer length.
177  */
178
179 size_t dbuf_get_length(struct dbuf_s *dbuf)
180 {
181   assert(dbuf != NULL);
182   assert(dbuf->alloc != 0);
183   assert(dbuf->buf != NULL);
184
185   return dbuf->len;
186 }
187
188
189 /*
190  * Trim the allocated buffer to required size
191  */
192
193 int dbuf_trim(struct dbuf_s *dbuf)
194 {
195   void *buf;
196
197   assert(dbuf != NULL);
198   assert(dbuf->alloc != 0);
199   assert(dbuf->buf != NULL);
200
201   buf = realloc(dbuf->buf, dbuf->len);
202
203   if (buf != NULL) {
204     dbuf->alloc = dbuf->len;
205     dbuf->buf = buf;
206   }
207
208   return buf != NULL;
209 }
210
211
212 /*
213  * Detach the buffer from dbuf structure.
214  * The dbuf structure can be reused by
215  * reinitializing it.
216  *
217  * See: dbuf_init()
218  */
219
220 void *dbuf_detach(struct dbuf_s *dbuf)
221 {
222   void *ret;
223
224   assert(dbuf != NULL);
225   assert(dbuf->alloc != 0);
226   assert(dbuf->buf != NULL);
227
228   ret = dbuf->buf;
229   dbuf->buf = NULL;
230   dbuf->len = 0;
231   dbuf->alloc = 0;
232
233   return ret;
234 }
235
236
237 /*
238  * Destroy the dbuf structure and
239  * free the buffer
240  */
241
242 void dbuf_destroy(struct dbuf_s *dbuf)
243 {
244   free(dbuf_detach(dbuf));
245 }
246
247
248 /*
249  * Delete dbuf structure on the heap:
250  * destroy it and free the allocated space.
251  * The user's responsablity is not to use
252  * the pointer any more: the best think to do
253  * is to set the pointer to NULL value.
254  *
255  * See dbuf_new()
256  */
257
258 void dbuf_delete(struct dbuf_s *dbuf)
259 {
260   dbuf_destroy(dbuf);
261   free(dbuf);
262 }
263
264
265 /*
266  * Free detached buffer.
267  *
268  * See dbuf_detach()
269  */
270
271 void dbuf_free(const void *buf)
272 {
273   free((void *)buf);
274 }