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 2000/09/13 17:45:15 priikone
24 * Splitted SILC core library. Core library includes now only
25 * SILC protocol specific stuff. New utility library includes the
26 * old stuff from core library that is more generic purpose stuff.
28 * Revision 1.2 2000/07/05 06:06:35 priikone
29 * Global cosmetic change.
31 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
32 * Imported from internal CVS/Added Log headers.
37 #include "silcincludes.h"
38 #include "silcbuffer.h"
40 static unsigned char *silc_buffer_pull_i(SilcBuffer sb, unsigned int len)
42 return silc_buffer_pull(sb, len);
45 static unsigned char *silc_buffer_push_i(SilcBuffer sb, unsigned int len)
47 return silc_buffer_push(sb, len);
50 static unsigned char *silc_buffer_pull_tail_i(SilcBuffer sb, unsigned int len)
52 return silc_buffer_pull_tail(sb, len);
55 static unsigned char *silc_buffer_push_tail_i(SilcBuffer sb, unsigned int len)
57 return silc_buffer_push_tail(sb, len);
60 static unsigned char *silc_buffer_put_head_i(SilcBuffer sb,
64 return silc_buffer_put_head(sb, data, len);
67 static unsigned char *silc_buffer_put_i(SilcBuffer sb,
71 return silc_buffer_put(sb, data, len);
74 static unsigned char *silc_buffer_put_tail_i(SilcBuffer sb,
78 return silc_buffer_put_tail(sb, data, len);
81 /* Allocates a new SilcBuffer and returns a pointer to it. The data
82 area of the new buffer is set to the real beginning of the buffer.
84 Buffer after allocation:
85 ---------------------------------
87 ---------------------------------
88 ^ head, data, tail ^ end
92 SilcBuffer silc_buffer_alloc(unsigned int len)
97 /* Allocate new SilcBuffer */
98 sb = silc_calloc(1, sizeof(*sb));
100 /* Allocate the actual data area */
101 data = silc_calloc(len, sizeof(*data));
102 memset(data, 0, len);
104 /* Set pointers to the new buffer */
110 sb->end = data + sb->truelen;
112 /* Set the function pointers */
113 sb->pull = silc_buffer_pull_i;
114 sb->push = silc_buffer_push_i;
115 sb->pull_tail = silc_buffer_pull_tail_i;
116 sb->push_tail = silc_buffer_push_tail_i;
117 sb->put = silc_buffer_put_i;
118 sb->put_head = silc_buffer_put_head_i;
119 sb->put_tail = silc_buffer_put_tail_i;
124 /* Free's a SilcBuffer */
126 void silc_buffer_free(SilcBuffer sb)
129 memset(sb->head, 'F', sb->truelen);
135 #ifdef SILC_DEBUG /* If we are doing debugging we won't
136 have the optimized inline buffer functions
137 available as optimization is not set
138 to compiler. These normal routines are
139 used in debugging mode. */
141 /* XXX These are currenly obsolte as SILC is compiled always with -O
142 flag thus inline functions maybe used. So, fix these. */
144 /* Pulls current data area towards end. The length of the currently
145 valid data area is also decremented. Returns pointer to the data
149 ---------------------------------
150 | head | data | tail |
151 ---------------------------------
153 Pulls the start of the data area.
155 ---------------------------------
156 | head | data | tail |
157 ---------------------------------
161 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
163 unsigned char *old_data = sb->data;
165 assert(len <= (sb->tail - sb->data));
173 /* Pushes current data area towards beginning. Length of the currently
174 valid data area is also incremented. Returns a pointer to the
175 data area before pushing.
178 ---------------------------------
179 | head | data | tail |
180 ---------------------------------
182 Pushes the start of the data area.
184 ---------------------------------
185 | head | data | tail |
186 ---------------------------------
190 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
192 unsigned char *old_data = sb->data;
194 assert((sb->data - len) >= sb->head);
202 /* Pulls current tail section towards end. Length of the current valid
203 data area is also incremented. Returns a pointer to the data area
207 ---------------------------------
208 | head | data | tail |
209 ---------------------------------
211 Pulls the start of the tail section.
213 ---------------------------------
214 | head | data | tail |
215 ---------------------------------
219 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
221 unsigned char *old_tail = sb->tail;
223 assert((sb->end - sb->tail) >= len);
231 /* Pushes current tail section towards beginning. Length of the current
232 valid data area is also decremented. Returns a pointer to the
233 tail section before pushing.
236 ---------------------------------
237 | head | data | tail |
238 ---------------------------------
240 Pushes the start of the tail section.
242 ---------------------------------
243 | head | data | tail |
244 ---------------------------------
248 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
250 unsigned char *old_tail = sb->tail;
252 assert((sb->tail - len) >= sb->data);
260 /* Puts data at the head of the buffer. Returns pointer to the copied
264 ---------------------------------
265 | head | data | tail |
266 ---------------------------------
268 Puts data to the head section.
271 unsigned char *silc_buffer_put_head(SilcBuffer sb,
275 assert((sb->data - sb->head) >= len);
276 return memcpy(sb->head, data, len);
279 /* Puts data at the start of the valid data area. Returns a pointer
280 to the copied data area.
283 ---------------------------------
284 | head | data | tail |
285 ---------------------------------
287 Puts data to the data section.
290 unsigned char *silc_buffer_put(SilcBuffer sb,
294 assert((sb->tail - sb->data) >= len);
295 return memcpy(sb->data, data, len);
298 /* Puts data at the tail of the buffer. Returns pointer to the copied
302 ---------------------------------
303 | head | data | tail |
304 ---------------------------------
306 Puts data to the tail section.
309 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
313 assert((sb->end - sb->tail) >= len);
314 return memcpy(sb->tail, data, len);