2 Unix SMB/Netbios implementation.
4 Samba memory buffer functions
5 Copyright (C) Andrew Tridgell 1992-1997
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1997
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /*******************************************************************
25 * Description: memory buffer / stream management.
26 * Author : Luke K C Leighton
30 * this module is intended for use in streaming data in and out of
31 * buffers. it is intended that a single data stream be subdivided
32 * into manageable sections.
34 * for example, an rpc header contains a length field, but until the
35 * data has been created, the length is unknown. using this module,
36 * the header section can be tacked onto the front of the data memory
37 * list once the size of the data section preceding it is known.
39 * the "margin" can be used to over-run and retrospectively lengthen
40 * the buffer. this is to save time in some of the loops, where it is
41 * not particularly desirable to realloc data by 1, 2 or 4 bytes
44 * each memory buffer contains a start and end offset. the end of
45 * one buffer should equal to the start of the next in the chain.
46 * (end - start = len, instead of end - start + 1 = len)
48 * the debug log levels are very high in some of the routines: you
49 * have no idea how boring it gets staring at debug output from these
51 ********************************************************************/
56 extern int DEBUGLEVEL;
58 /*******************************************************************
59 initialise a memory buffer.
60 ********************************************************************/
61 void mem_init(struct mem_buf *buf, int margin)
72 buf->offset.start = 0;
76 /*******************************************************************
77 initialise a memory buffer.
79 dynamic indicates memory has been dynamically allocated.
80 if mem_free is called, the memory will be freed.
81 ********************************************************************/
82 void mem_create(struct mem_buf *buf, char *data, int offset, int size, int margin, BOOL dynamic)
84 buf->dynamic = dynamic;
86 buf->data_size = size;
87 buf->data_used = size;
93 buf->offset.start = offset;
94 buf->offset.end = offset + size;
97 /*******************************************************************
98 allocate a memory buffer. assume it's empty
99 ********************************************************************/
100 BOOL mem_alloc_data(struct mem_buf *buf, int size)
104 DEBUG(3,("mem_alloc_data: warning - memory buffer type is set to static\n"));
107 buf->data_size = size + buf->margin;
108 buf->data_used = size;
110 buf->data = malloc(buf->data_size);
112 if (buf->data == NULL && size != 0)
114 DEBUG(3,("mem_alloc: could not malloc size %d\n",
116 mem_init(buf, buf->margin);
121 bzero(buf->data, buf->data_size);
126 /*******************************************************************
127 allocates a memory buffer structure
128 ********************************************************************/
129 BOOL mem_buf_copy(char *copy_into, struct mem_buf *buf,
130 uint32 offset, uint32 len)
132 uint32 end = offset + len;
134 uint32 data_len = mem_buf_len(buf);
135 uint32 start_offset = offset;
136 struct mem_buf **bcp = &buf;
138 if (buf == NULL || copy_into == NULL) return False;
140 DEBUG(200,("mem_buf_copy: data[%d..%d] offset %d len %d\n",
141 buf->offset.start, data_len, offset, len));
143 /* there's probably an off-by-one bug, here, and i haven't even tested the code :-) */
144 while (offset < end && ((q = mem_data(bcp, offset)) != NULL))
146 uint32 copy_len = (*bcp)->offset.end - offset;
148 DEBUG(200,("\tdata[%d..%d] - offset %d len %d\n",
149 (*bcp)->offset.start, (*bcp)->offset.end,
152 memcpy(copy_into, q, copy_len);
155 copy_into += copy_len;
160 DEBUG(200,("mem_buf_copy: copied %d bytes\n", offset - start_offset));
164 DEBUG(200,("mem_buf_copy: failed\n"));
170 /*******************************************************************
171 allocates a memory buffer structure
172 ********************************************************************/
173 BOOL mem_buf_init(struct mem_buf **buf, uint32 margin)
175 if (buf == NULL) return False;
179 (*buf) = malloc(sizeof(**buf));
182 mem_init((*buf), margin);
188 (*buf)->margin = margin;
194 /*******************************************************************
195 frees up a memory buffer.
196 ********************************************************************/
197 void mem_buf_free(struct mem_buf **buf)
199 if (buf == NULL) return;
200 if ((*buf) == NULL) return;
202 mem_free_data(*buf); /* delete memory data */
203 free(*buf); /* delete item */
207 /*******************************************************************
208 frees a memory buffer chain. assumes that all items are malloced.
209 ********************************************************************/
210 static void mem_free_chain(struct mem_buf **buf)
212 if (buf == NULL) return;
213 if ((*buf) == NULL) return;
215 if ((*buf)->next != NULL)
217 mem_free_chain(&((*buf)->next)); /* delete all other items in chain */
222 /*******************************************************************
223 frees a memory buffer.
224 ********************************************************************/
225 void mem_free_data(struct mem_buf *buf)
227 if (buf == NULL) return;
229 if (buf->data != NULL && buf->dynamic)
231 free(buf->data); /* delete data in this structure */
233 mem_init(buf, buf->margin);
236 /*******************************************************************
237 reallocate a memory buffer, including a safety margin
238 ********************************************************************/
239 BOOL mem_realloc_data(struct mem_buf *buf, int new_size)
245 DEBUG(3,("mem_realloc_data: memory buffer has not been dynamically allocated!\n"));
255 new_data = Realloc(buf->data, new_size + buf->margin);
257 if (new_data != NULL)
259 buf->data = new_data;
260 buf->data_size = new_size + buf->margin;
261 buf->data_used = new_size;
263 else if (buf->data_size <= new_size)
265 DEBUG(3,("mem_realloc: warning - could not realloc to %d(+%d)\n",
266 new_size, buf->margin));
268 buf->data_used = new_size;
272 DEBUG(3,("mem_realloc: error - could not realloc to %d\n",
282 /*******************************************************************
283 reallocate a memory buffer, retrospectively :-)
284 ********************************************************************/
285 BOOL mem_grow_data(struct mem_buf **buf, BOOL io, int new_size, BOOL force_grow)
287 if (new_size + (*buf)->margin >= (*buf)->data_size)
289 if (io && !force_grow)
291 DEBUG(3,("mem_grow_data: cannot resize when reading from a data stream\n"));
295 return mem_realloc_data((*buf), new_size);
301 /*******************************************************************
302 search for a memory buffer that falls within the specified offset
303 ********************************************************************/
304 static BOOL mem_find(struct mem_buf **buf, uint32 offset)
307 if (buf == NULL) return False;
311 DEBUG(200,("mem_find: data[%d..%d] offset: %d\n",
312 f->offset.start, f->offset.end, offset));
314 while (f != NULL && offset >= f->offset.end)
316 DEBUG(200,("mem_find: next[%d..%d]\n",
317 f->offset.start, f->offset.end));
326 DEBUG(200,("mem_find: found data[%d..%d]\n",
327 (*buf)->offset.start,(*buf)->offset.end));
334 /*******************************************************************
335 add up the lengths of all sections.
336 ********************************************************************/
337 uint32 mem_buf_len(struct mem_buf *buf)
342 len += buf->offset.end - buf->offset.start;
349 /*******************************************************************
350 return the memory location specified by offset. may return NULL.
351 ********************************************************************/
352 char *mem_data(struct mem_buf **buf, uint32 offset)
354 if (mem_find(buf, offset))
356 return &((*buf)->data[offset - (*buf)->offset.start]);