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.
23 * Revision 1.2 2000/10/31 19:48:32 priikone
24 * A LOT updates. Cannot separate. :)
26 * Revision 1.1 2000/09/13 17:45:15 priikone
27 * Splitted SILC core library. Core library includes now only
28 * SILC protocol specific stuff. New utility library includes the
29 * old stuff from core library that is more generic purpose stuff.
31 * Revision 1.2 2000/07/05 06:06:35 priikone
32 * Global cosmetic change.
34 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
35 * Imported from internal CVS/Added Log headers.
40 #include "silcincludes.h"
41 #include "silcbuffer.h"
43 #ifdef SILC_DEBUG /* If we are doing debugging we won't
44 have the optimized inline buffer functions
45 available as optimization is not set
46 to compiler. These normal routines are
47 used in debugging mode. */
49 /* XXX These are currenly obsolete as SILC is compiled always with -O
50 flag thus inline functions maybe used always. So, fix these. */
52 /* Allocates a new SilcBuffer and returns a pointer to it. The data
53 area of the new buffer is set to the real beginning of the buffer.
55 Buffer after allocation:
56 ---------------------------------
58 ---------------------------------
59 ^ head, data, tail ^ end
63 SilcBuffer silc_buffer_alloc(unsigned int len)
68 /* Allocate new SilcBuffer */
69 sb = silc_calloc(1, sizeof(*sb));
71 /* Allocate the actual data area */
72 data = silc_calloc(len, sizeof(*data));
75 /* Set pointers to the new buffer */
81 sb->end = data + sb->truelen;
86 /* Free's a SilcBuffer */
88 void silc_buffer_free(SilcBuffer sb)
91 memset(sb->head, 'F', sb->truelen);
97 /* Pulls current data area towards end. The length of the currently
98 valid data area is also decremented. Returns pointer to the data
102 ---------------------------------
103 | head | data | tail |
104 ---------------------------------
106 Pulls the start of the data area.
108 ---------------------------------
109 | head | data | tail |
110 ---------------------------------
114 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
116 unsigned char *old_data = sb->data;
118 assert(len <= (sb->tail - sb->data));
126 /* Pushes current data area towards beginning. Length of the currently
127 valid data area is also incremented. Returns a pointer to the
128 data area before pushing.
131 ---------------------------------
132 | head | data | tail |
133 ---------------------------------
135 Pushes the start of the data area.
137 ---------------------------------
138 | head | data | tail |
139 ---------------------------------
143 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
145 unsigned char *old_data = sb->data;
147 assert((sb->data - len) >= sb->head);
155 /* Pulls current tail section towards end. Length of the current valid
156 data area is also incremented. Returns a pointer to the data area
160 ---------------------------------
161 | head | data | tail |
162 ---------------------------------
164 Pulls the start of the tail section.
166 ---------------------------------
167 | head | data | tail |
168 ---------------------------------
172 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
174 unsigned char *old_tail = sb->tail;
176 assert((sb->end - sb->tail) >= len);
184 /* Pushes current tail section towards beginning. Length of the current
185 valid data area is also decremented. Returns a pointer to the
186 tail section before pushing.
189 ---------------------------------
190 | head | data | tail |
191 ---------------------------------
193 Pushes the start of the tail section.
195 ---------------------------------
196 | head | data | tail |
197 ---------------------------------
201 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
203 unsigned char *old_tail = sb->tail;
205 assert((sb->tail - len) >= sb->data);
213 /* Puts data at the head of the buffer. Returns pointer to the copied
217 ---------------------------------
218 | head | data | tail |
219 ---------------------------------
221 Puts data to the head section.
224 unsigned char *silc_buffer_put_head(SilcBuffer sb,
228 assert((sb->data - sb->head) >= len);
229 return memcpy(sb->head, data, len);
232 /* Puts data at the start of the valid data area. Returns a pointer
233 to the copied data area.
236 ---------------------------------
237 | head | data | tail |
238 ---------------------------------
240 Puts data to the data section.
243 unsigned char *silc_buffer_put(SilcBuffer sb,
247 assert((sb->tail - sb->data) >= len);
248 return memcpy(sb->data, data, len);
251 /* Puts data at the tail of the buffer. Returns pointer to the copied
255 ---------------------------------
256 | head | data | tail |
257 ---------------------------------
259 Puts data to the tail section.
262 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
266 assert((sb->end - sb->tail) >= len);
267 return memcpy(sb->tail, data, len);