5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1998 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
27 SilcBuffer is very simple and easy to use, yet you can do to the
28 buffer almost anything you want with its method functions. The buffer
29 is constructed of four different data sections that in whole creates
30 the allocated data area. Following short description of the fields
35 True length of the buffer. This is set at the allocation of the
36 buffer and it should not be touched after that. This field should
37 be considered read-only.
41 Length of the currently valid data area. Tells the length of the
42 data at the buffer. This is set to zero at the allocation of the
43 buffer and should not be updated by hand. Method functions of the
44 buffer automatically updates this field. However, it is not
45 read-only field and can be updated manually if necessary.
49 Head of the allocated buffer. This is the start of the allocated
50 data area and remains as same throughout the lifetime of the buffer.
51 However, the end of the head area or the start of the currently valid
52 data area is variable.
54 --------------------------------
55 | head | data | tail |
56 --------------------------------
59 Current head section in the buffer is sb->data - sb->head.
63 Currently valid data area. This is the start of the currently valid
64 main data area. The data area is variable in all directions.
66 --------------------------------
67 | head | data | tail |
68 --------------------------------
71 Current valid data area in the buffer is sb->tail - sb->data.
75 Tail of the buffer. This is the end of the currently valid data area
76 or start of the tail area. The start of the tail area is variable.
78 --------------------------------
79 | head | data | tail |
80 --------------------------------
83 Current tail section in the buffer is sb->end - sb->tail.
87 End of the allocated buffer. This is the end of the allocated data
88 area and remains as same throughout the lifetime of the buffer.
89 Usually this field is not needed except when checking the size
92 --------------------------------
93 | head | data | tail |
94 --------------------------------
97 Length of the entire buffer is (ie. truelen) sb->end - sb->head.
99 Currently valid data area is considered to be the main data area in
100 the buffer. However, the entire buffer is of course valid data and can
101 be used as such. Usually head section of the buffer includes different
102 kind of headers or similiar. Data section includes the main data of
103 the buffer. Tail section can be seen as a reserve space of the data
104 section. Tail section can be pulled towards end thus the data section
107 This buffer scheme is based on Linux kernel's Socket Buffer, the
108 idea were taken directly from there and credits should go there.
112 typedef struct SilcBufferStruct {
113 unsigned int truelen;
121 typedef SilcBufferObject *SilcBuffer;
125 /* Returns the true length of the buffer. This is used to pull
126 the buffer area to the end of the buffer. */
127 #define SILC_BUFFER_END(x) ((x)->end - (x)->head)
129 #ifndef SILC_DEBUG /* When we are not doing debugging we use
130 optimized inline buffer functions. */
132 * Optimized buffer managing routines. These are short inline
137 SilcBuffer silc_buffer_alloc(unsigned int len)
142 /* Allocate new SilcBuffer */
143 sb = silc_calloc(1, sizeof(*sb));
145 /* Allocate the actual data area */
146 data = silc_calloc(len, sizeof(*data));
148 /* Set pointers to the new buffer */
154 sb->end = data + sb->truelen;
159 /* Free's a SilcBuffer */
162 void silc_buffer_free(SilcBuffer sb)
165 memset(sb->head, 'F', sb->truelen);
171 /* Pulls current data area towards end. The length of the currently
172 valid data area is also decremented. Returns pointer to the data
176 ---------------------------------
177 | head | data | tail |
178 ---------------------------------
180 Pulls the start of the data area.
182 ---------------------------------
183 | head | data | tail |
184 ---------------------------------
189 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
191 unsigned char *old_data = sb->data;
193 assert(len <= (sb->tail - sb->data));
201 /* Pushes current data area towards beginning. Length of the currently
202 valid data area is also incremented. Returns a pointer to the
203 data area before pushing.
206 ---------------------------------
207 | head | data | tail |
208 ---------------------------------
210 Pushes the start of the data area.
212 ---------------------------------
213 | head | data | tail |
214 ---------------------------------
219 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
221 unsigned char *old_data = sb->data;
223 assert((sb->data - len) >= sb->head);
231 /* Pulls current tail section towards end. Length of the current valid
232 data area is also incremented. Returns a pointer to the data area
236 ---------------------------------
237 | head | data | tail |
238 ---------------------------------
240 Pulls the start of the tail section.
242 ---------------------------------
243 | head | data | tail |
244 ---------------------------------
249 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
251 unsigned char *old_tail = sb->tail;
253 assert((sb->end - sb->tail) >= len);
261 /* Pushes current tail section towards beginning. Length of the current
262 valid data area is also decremented. Returns a pointer to the
263 tail section before pushing.
266 ---------------------------------
267 | head | data | tail |
268 ---------------------------------
270 Pushes the start of the tail section.
272 ---------------------------------
273 | head | data | tail |
274 ---------------------------------
279 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
281 unsigned char *old_tail = sb->tail;
283 assert((sb->tail - len) >= sb->data);
291 /* Puts data at the head of the buffer. Returns pointer to the copied
295 ---------------------------------
296 | head | data | tail |
297 ---------------------------------
299 Puts data to the head section.
303 unsigned char *silc_buffer_put_head(SilcBuffer sb,
307 assert((sb->data - sb->head) >= len);
308 return memcpy(sb->head, data, len);
311 /* Puts data at the start of the valid data area. Returns a pointer
312 to the copied data area.
315 ---------------------------------
316 | head | data | tail |
317 ---------------------------------
319 Puts data to the data section.
323 unsigned char *silc_buffer_put(SilcBuffer sb,
327 assert((sb->tail - sb->data) >= len);
328 return memcpy(sb->data, data, len);
331 /* Puts data at the tail of the buffer. Returns pointer to the copied
335 ---------------------------------
336 | head | data | tail |
337 ---------------------------------
339 Puts data to the tail section.
343 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
347 assert((sb->end - sb->tail) >= len);
348 return memcpy(sb->tail, data, len);
351 #endif /* !SILC_DEBUG */
355 SilcBuffer silc_buffer_alloc(unsigned int len);
356 void silc_buffer_free(SilcBuffer sb);
357 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len);
358 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len);
359 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len);
360 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len);
361 unsigned char *silc_buffer_put_head(SilcBuffer sb,
364 unsigned char *silc_buffer_put(SilcBuffer sb,
367 unsigned char *silc_buffer_put_tail(SilcBuffer sb,