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.3 2000/11/01 21:43:44 priikone
24 * removed memset not needed
26 * Revision 1.2 2000/10/31 19:48:32 priikone
27 * A LOT updates. Cannot separate. :)
29 * Revision 1.1 2000/09/13 17:45:15 priikone
30 * Splitted SILC core library. Core library includes now only
31 * SILC protocol specific stuff. New utility library includes the
32 * old stuff from core library that is more generic purpose stuff.
34 * Revision 1.2 2000/07/05 06:06:35 priikone
35 * Global cosmetic change.
37 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
38 * Imported from internal CVS/Added Log headers.
43 #include "silcincludes.h"
44 #include "silcbuffer.h"
46 #ifdef SILC_DEBUG /* If we are doing debugging we won't
47 have the optimized inline buffer functions
48 available as optimization is not set
49 to compiler. These normal routines are
50 used in debugging mode. */
52 /* XXX These are currenly obsolete as SILC is compiled always with -O
53 flag thus inline functions maybe used always. So, fix these. */
55 /* Allocates a new SilcBuffer and returns a pointer to it. The data
56 area of the new buffer is set to the real beginning of the buffer.
58 Buffer after allocation:
59 ---------------------------------
61 ---------------------------------
62 ^ head, data, tail ^ end
66 SilcBuffer silc_buffer_alloc(unsigned int len)
71 /* Allocate new SilcBuffer */
72 sb = silc_calloc(1, sizeof(*sb));
74 /* Allocate the actual data area */
75 data = silc_calloc(len, sizeof(*data));
77 /* Set pointers to the new buffer */
83 sb->end = data + sb->truelen;
88 /* Free's a SilcBuffer */
90 void silc_buffer_free(SilcBuffer sb)
93 memset(sb->head, 'F', sb->truelen);
99 /* Pulls current data area towards end. The length of the currently
100 valid data area is also decremented. Returns pointer to the data
104 ---------------------------------
105 | head | data | tail |
106 ---------------------------------
108 Pulls the start of the data area.
110 ---------------------------------
111 | head | data | tail |
112 ---------------------------------
116 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
118 unsigned char *old_data = sb->data;
120 assert(len <= (sb->tail - sb->data));
128 /* Pushes current data area towards beginning. Length of the currently
129 valid data area is also incremented. Returns a pointer to the
130 data area before pushing.
133 ---------------------------------
134 | head | data | tail |
135 ---------------------------------
137 Pushes the start of the data area.
139 ---------------------------------
140 | head | data | tail |
141 ---------------------------------
145 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
147 unsigned char *old_data = sb->data;
149 assert((sb->data - len) >= sb->head);
157 /* Pulls current tail section towards end. Length of the current valid
158 data area is also incremented. Returns a pointer to the data area
162 ---------------------------------
163 | head | data | tail |
164 ---------------------------------
166 Pulls the start of the tail section.
168 ---------------------------------
169 | head | data | tail |
170 ---------------------------------
174 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
176 unsigned char *old_tail = sb->tail;
178 assert((sb->end - sb->tail) >= len);
186 /* Pushes current tail section towards beginning. Length of the current
187 valid data area is also decremented. Returns a pointer to the
188 tail section before pushing.
191 ---------------------------------
192 | head | data | tail |
193 ---------------------------------
195 Pushes the start of the tail section.
197 ---------------------------------
198 | head | data | tail |
199 ---------------------------------
203 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
205 unsigned char *old_tail = sb->tail;
207 assert((sb->tail - len) >= sb->data);
215 /* Puts data at the head of the buffer. Returns pointer to the copied
219 ---------------------------------
220 | head | data | tail |
221 ---------------------------------
223 Puts data to the head section.
226 unsigned char *silc_buffer_put_head(SilcBuffer sb,
230 assert((sb->data - sb->head) >= len);
231 return memcpy(sb->head, data, len);
234 /* Puts data at the start of the valid data area. Returns a pointer
235 to the copied data area.
238 ---------------------------------
239 | head | data | tail |
240 ---------------------------------
242 Puts data to the data section.
245 unsigned char *silc_buffer_put(SilcBuffer sb,
249 assert((sb->tail - sb->data) >= len);
250 return memcpy(sb->data, data, len);
253 /* Puts data at the tail of the buffer. Returns pointer to the copied
257 ---------------------------------
258 | head | data | tail |
259 ---------------------------------
261 Puts data to the tail section.
264 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
268 assert((sb->end - sb->tail) >= len);
269 return memcpy(sb->tail, data, len);