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/07/05 06:06:35 priikone
24 * Global cosmetic change.
26 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
27 * Imported from internal CVS/Added Log headers.
32 #include "silcincludes.h"
33 #include "silcbuffer.h"
35 static unsigned char *silc_buffer_pull_i(SilcBuffer sb, unsigned int len)
37 return silc_buffer_pull(sb, len);
40 static unsigned char *silc_buffer_push_i(SilcBuffer sb, unsigned int len)
42 return silc_buffer_push(sb, len);
45 static unsigned char *silc_buffer_pull_tail_i(SilcBuffer sb, unsigned int len)
47 return silc_buffer_pull_tail(sb, len);
50 static unsigned char *silc_buffer_push_tail_i(SilcBuffer sb, unsigned int len)
52 return silc_buffer_push_tail(sb, len);
55 static unsigned char *silc_buffer_put_head_i(SilcBuffer sb,
59 return silc_buffer_put_head(sb, data, len);
62 static unsigned char *silc_buffer_put_i(SilcBuffer sb,
66 return silc_buffer_put(sb, data, len);
69 static unsigned char *silc_buffer_put_tail_i(SilcBuffer sb,
73 return silc_buffer_put_tail(sb, data, len);
76 /* Allocates a new SilcBuffer and returns a pointer to it. The data
77 area of the new buffer is set to the real beginning of the buffer.
79 Buffer after allocation:
80 ---------------------------------
82 ---------------------------------
83 ^ head, data, tail ^ end
87 SilcBuffer silc_buffer_alloc(unsigned int len)
92 /* Allocate new SilcBuffer */
93 sb = silc_calloc(1, sizeof(*sb));
95 /* Allocate the actual data area */
96 data = silc_calloc(len, sizeof(*data));
99 /* Set pointers to the new buffer */
105 sb->end = data + sb->truelen;
107 /* Set the function pointers */
108 sb->pull = silc_buffer_pull_i;
109 sb->push = silc_buffer_push_i;
110 sb->pull_tail = silc_buffer_pull_tail_i;
111 sb->push_tail = silc_buffer_push_tail_i;
112 sb->put = silc_buffer_put_i;
113 sb->put_head = silc_buffer_put_head_i;
114 sb->put_tail = silc_buffer_put_tail_i;
119 /* Free's a SilcBuffer */
121 void silc_buffer_free(SilcBuffer sb)
124 memset(sb->head, 'F', sb->truelen);
130 #ifdef SILC_DEBUG /* If we are doing debugging we won't
131 have the optimized inline buffer functions
132 available as optimization is not set
133 to compiler. These normal routines are
134 used in debugging mode. */
136 /* XXX These are currenly obsolte as SILC is compiled always with -O
137 flag thus inline functions maybe used. So, fix these. */
139 /* Pulls current data area towards end. The length of the currently
140 valid data area is also decremented. Returns pointer to the data
144 ---------------------------------
145 | head | data | tail |
146 ---------------------------------
148 Pulls the start of the data area.
150 ---------------------------------
151 | head | data | tail |
152 ---------------------------------
156 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
158 unsigned char *old_data = sb->data;
160 assert(len <= (sb->tail - sb->data));
168 /* Pushes current data area towards beginning. Length of the currently
169 valid data area is also incremented. Returns a pointer to the
170 data area before pushing.
173 ---------------------------------
174 | head | data | tail |
175 ---------------------------------
177 Pushes the start of the data area.
179 ---------------------------------
180 | head | data | tail |
181 ---------------------------------
185 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
187 unsigned char *old_data = sb->data;
189 assert((sb->data - len) >= sb->head);
197 /* Pulls current tail section towards end. Length of the current valid
198 data area is also incremented. Returns a pointer to the data area
202 ---------------------------------
203 | head | data | tail |
204 ---------------------------------
206 Pulls the start of the tail section.
208 ---------------------------------
209 | head | data | tail |
210 ---------------------------------
214 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
216 unsigned char *old_tail = sb->tail;
218 assert((sb->end - sb->tail) >= len);
226 /* Pushes current tail section towards beginning. Length of the current
227 valid data area is also decremented. Returns a pointer to the
228 tail section before pushing.
231 ---------------------------------
232 | head | data | tail |
233 ---------------------------------
235 Pushes the start of the tail section.
237 ---------------------------------
238 | head | data | tail |
239 ---------------------------------
243 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
245 unsigned char *old_tail = sb->tail;
247 assert((sb->tail - len) >= sb->data);
255 /* Puts data at the head of the buffer. Returns pointer to the copied
259 ---------------------------------
260 | head | data | tail |
261 ---------------------------------
263 Puts data to the head section.
266 unsigned char *silc_buffer_put_head(SilcBuffer sb,
270 assert((sb->data - sb->head) >= len);
271 return memcpy(sb->head, data, len);
274 /* Puts data at the start of the valid data area. Returns a pointer
275 to the copied data area.
278 ---------------------------------
279 | head | data | tail |
280 ---------------------------------
282 Puts data to the data section.
285 unsigned char *silc_buffer_put(SilcBuffer sb,
289 assert((sb->tail - sb->data) >= len);
290 return memcpy(sb->data, data, len);
293 /* Puts data at the tail of the buffer. Returns pointer to the copied
297 ---------------------------------
298 | head | data | tail |
299 ---------------------------------
301 Puts data to the tail section.
304 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
308 assert((sb->end - sb->tail) >= len);
309 return memcpy(sb->tail, data, len);