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.1.1.1 2000/06/27 11:36:55 priikone
24 * Importet from internal CVS/Added Log headers.
29 #include "silcincludes.h"
30 #include "silcbuffer.h"
32 static unsigned char *silc_buffer_pull_i(SilcBuffer sb, unsigned int len)
34 return silc_buffer_pull(sb, len);
37 static unsigned char *silc_buffer_push_i(SilcBuffer sb, unsigned int len)
39 return silc_buffer_push(sb, len);
42 static unsigned char *silc_buffer_pull_tail_i(SilcBuffer sb, unsigned int len)
44 return silc_buffer_pull_tail(sb, len);
47 static unsigned char *silc_buffer_push_tail_i(SilcBuffer sb, unsigned int len)
49 return silc_buffer_push_tail(sb, len);
52 static unsigned char *silc_buffer_put_head_i(SilcBuffer sb,
56 return silc_buffer_put_head(sb, data, len);
59 static unsigned char *silc_buffer_put_i(SilcBuffer sb,
63 return silc_buffer_put(sb, data, len);
66 static unsigned char *silc_buffer_put_tail_i(SilcBuffer sb,
70 return silc_buffer_put_tail(sb, data, len);
73 /* Allocates a new SilcBuffer and returns a pointer to it. The data
74 area of the new buffer is set to the real beginning of the buffer.
76 Buffer after allocation:
77 ---------------------------------
79 ---------------------------------
80 ^ head, data, tail ^ end
84 SilcBuffer silc_buffer_alloc(unsigned int len)
89 /* Allocate new SilcBuffer */
90 sb = silc_calloc(1, sizeof(*sb));
94 /* Allocate the actual data area */
95 data = silc_malloc(len);
100 /* Set pointers to the new buffer */
106 sb->end = data + sb->truelen;
108 /* Set the function pointers */
109 sb->pull = silc_buffer_pull_i;
110 sb->push = silc_buffer_push_i;
111 sb->pull_tail = silc_buffer_pull_tail_i;
112 sb->push_tail = silc_buffer_push_tail_i;
113 sb->put = silc_buffer_put_i;
114 sb->put_head = silc_buffer_put_head_i;
115 sb->put_tail = silc_buffer_put_tail_i;
120 /* Free's a SilcBuffer */
122 void silc_buffer_free(SilcBuffer sb)
125 memset(sb->head, 'F', sb->truelen);
131 #ifdef SILC_DEBUG /* If we are doing debugging we won't
132 have the optimized inline buffer functions
133 available as optimization is not set
134 to compiler. These normal routines are
135 used in debugging mode. */
137 /* XXX These are currenly obsolte as SILC is compiled always with -O
138 flag thus inline functions maybe used. So, fix these. */
140 /* Pulls current data area towards end. The length of the currently
141 valid data area is also decremented. Returns pointer to the data
145 ---------------------------------
146 | head | data | tail |
147 ---------------------------------
149 Pulls the start of the data area.
151 ---------------------------------
152 | head | data | tail |
153 ---------------------------------
157 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
159 unsigned char *old_data = sb->data;
161 assert(len <= (sb->tail - sb->data));
169 /* Pushes current data area towards beginning. Length of the currently
170 valid data area is also incremented. Returns a pointer to the
171 data area before pushing.
174 ---------------------------------
175 | head | data | tail |
176 ---------------------------------
178 Pushes the start of the data area.
180 ---------------------------------
181 | head | data | tail |
182 ---------------------------------
186 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
188 unsigned char *old_data = sb->data;
190 assert((sb->data - len) >= sb->head);
198 /* Pulls current tail section towards end. Length of the current valid
199 data area is also incremented. Returns a pointer to the data area
203 ---------------------------------
204 | head | data | tail |
205 ---------------------------------
207 Pulls the start of the tail section.
209 ---------------------------------
210 | head | data | tail |
211 ---------------------------------
215 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
217 unsigned char *old_tail = sb->tail;
219 assert((sb->end - sb->tail) >= len);
227 /* Pushes current tail section towards beginning. Length of the current
228 valid data area is also decremented. Returns a pointer to the
229 tail section before pushing.
232 ---------------------------------
233 | head | data | tail |
234 ---------------------------------
236 Pushes the start of the tail section.
238 ---------------------------------
239 | head | data | tail |
240 ---------------------------------
244 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
246 unsigned char *old_tail = sb->tail;
248 assert((sb->tail - len) >= sb->data);
256 /* Puts data at the head of the buffer. Returns pointer to the copied
260 ---------------------------------
261 | head | data | tail |
262 ---------------------------------
264 Puts data to the head section.
267 unsigned char *silc_buffer_put_head(SilcBuffer sb,
271 assert((sb->data - sb->head) >= len);
272 return memcpy(sb->head, data, len);
275 /* Puts data at the start of the valid data area. Returns a pointer
276 to the copied data area.
279 ---------------------------------
280 | head | data | tail |
281 ---------------------------------
283 Puts data to the data section.
286 unsigned char *silc_buffer_put(SilcBuffer sb,
290 assert((sb->tail - sb->data) >= len);
291 return memcpy(sb->data, data, len);
294 /* Puts data at the tail of the buffer. Returns pointer to the copied
298 ---------------------------------
299 | head | data | tail |
300 ---------------------------------
302 Puts data to the tail section.
305 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
309 assert((sb->end - sb->tail) >= len);
310 return memcpy(sb->tail, data, len);