Global cosmetic change.
[silc.git] / lib / silccore / silcbuffer.c
1 /*
2
3   silcbuffer.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1998 - 2000 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /*
21  * $Id$
22  * $Log$
23  * Revision 1.2  2000/07/05 06:06:35  priikone
24  *      Global cosmetic change.
25  *
26  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
27  *      Imported from internal CVS/Added Log headers.
28  *
29  *
30  */
31
32 #include "silcincludes.h"
33 #include "silcbuffer.h"
34
35 static unsigned char *silc_buffer_pull_i(SilcBuffer sb, unsigned int len)
36 {
37   return silc_buffer_pull(sb, len);
38 }
39
40 static unsigned char *silc_buffer_push_i(SilcBuffer sb, unsigned int len)
41 {
42   return silc_buffer_push(sb, len);
43 }
44
45 static unsigned char *silc_buffer_pull_tail_i(SilcBuffer sb, unsigned int len)
46 {
47   return silc_buffer_pull_tail(sb, len);
48 }
49
50 static unsigned char *silc_buffer_push_tail_i(SilcBuffer sb, unsigned int len)
51 {
52   return silc_buffer_push_tail(sb, len);
53 }
54
55 static unsigned char *silc_buffer_put_head_i(SilcBuffer sb, 
56                                              unsigned char *data,
57                                              unsigned int len)
58 {
59   return silc_buffer_put_head(sb, data, len);
60 }
61
62 static unsigned char *silc_buffer_put_i(SilcBuffer sb, 
63                                         unsigned char *data,
64                                         unsigned int len)
65 {
66   return silc_buffer_put(sb, data, len);
67 }
68
69 static unsigned char *silc_buffer_put_tail_i(SilcBuffer sb, 
70                                              unsigned char *data,
71                                              unsigned int len)
72 {
73   return silc_buffer_put_tail(sb, data, len);
74 }
75
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. 
78
79    Buffer after allocation:
80    ---------------------------------
81    |                               |
82    ---------------------------------
83    ^ head, data, tail              ^ end
84
85 */
86
87 SilcBuffer silc_buffer_alloc(unsigned int len)
88 {
89   SilcBuffer sb;
90   unsigned char *data;
91
92   /* Allocate new SilcBuffer */
93   sb = silc_calloc(1, sizeof(*sb));
94
95   /* Allocate the actual data area */
96   data = silc_calloc(len, sizeof(*data));
97   memset(data, 0, len);
98
99   /* Set pointers to the new buffer */
100   sb->truelen = len;
101   sb->len = 0;
102   sb->head = data;
103   sb->data = data;
104   sb->tail = data;
105   sb->end = data + sb->truelen;
106
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;
115
116   return sb;
117 }
118
119 /* Free's a SilcBuffer */
120
121 void silc_buffer_free(SilcBuffer sb)
122 {
123   if (sb) {
124     memset(sb->head, 'F', sb->truelen);
125     silc_free(sb->head);
126     silc_free(sb);
127   }
128 }
129
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. */
135
136 /* XXX These are currenly obsolte as SILC is compiled always with -O
137    flag thus inline functions maybe used. So, fix these. */
138
139 /* Pulls current data area towards end. The length of the currently
140    valid data area is also decremented. Returns pointer to the data
141    area before pulling. 
142
143    Example:
144    ---------------------------------
145    | head  | data       | tail     |
146    ---------------------------------
147            ^
148            Pulls the start of the data area.
149
150    ---------------------------------
151    | head     | data    | tail     |
152    ---------------------------------
153            ^
154 */
155
156 unsigned char *silc_buffer_pull(SilcBuffer sb, unsigned int len)
157 {
158   unsigned char *old_data = sb->data;
159
160   assert(len <= (sb->tail - sb->data));
161
162   sb->data += len;
163   sb->len -= len;
164
165   return old_data;
166 }
167
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. 
171
172    Example:
173    ---------------------------------
174    | head     | data    | tail     |
175    ---------------------------------
176               ^
177               Pushes the start of the data area.
178
179    ---------------------------------
180    | head  | data       | tail     |
181    ---------------------------------
182               ^
183 */
184
185 unsigned char *silc_buffer_push(SilcBuffer sb, unsigned int len)
186 {
187   unsigned char *old_data = sb->data;
188
189   assert((sb->data - len) >= sb->head);
190
191   sb->data -= len;
192   sb->len += len;
193
194   return old_data;
195 }
196
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 
199    before pulling.
200
201    Example:
202    ---------------------------------
203    | head  | data       | tail     |
204    ---------------------------------
205                         ^
206                         Pulls the start of the tail section.
207
208    ---------------------------------
209    | head  | data           | tail |
210    ---------------------------------
211                         ^
212 */
213
214 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, unsigned int len)
215 {
216   unsigned char *old_tail = sb->tail;
217
218   assert((sb->end - sb->tail) >= len);
219
220   sb->tail += len;
221   sb->len += len;
222
223   return old_tail;
224 }
225
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. 
229
230    Example:
231    ---------------------------------
232    | head  | data           | tail |
233    ---------------------------------
234                             ^
235                             Pushes the start of the tail section.
236
237    ---------------------------------
238    | head  | data       | tail     |
239    ---------------------------------
240                             ^
241 */
242
243 unsigned char *silc_buffer_push_tail(SilcBuffer sb, unsigned int len)
244 {
245   unsigned char *old_tail = sb->tail;
246
247   assert((sb->tail - len) >= sb->data);
248
249   sb->tail -= len;
250   sb->len -= len;
251
252   return old_tail;
253 }
254
255 /* Puts data at the head of the buffer. Returns pointer to the copied
256    data area. 
257    
258    Example:
259    ---------------------------------
260    | head  | data       | tail     |
261    ---------------------------------
262    ^
263    Puts data to the head section. 
264 */
265
266 unsigned char *silc_buffer_put_head(SilcBuffer sb, 
267                                     unsigned char *data,
268                                     unsigned int len)
269 {
270   assert((sb->data - sb->head) >= len);
271   return memcpy(sb->head, data, len);
272 }
273
274 /* Puts data at the start of the valid data area. Returns a pointer 
275    to the copied data area. 
276
277    Example:
278    ---------------------------------
279    | head  | data       | tail     |
280    ---------------------------------
281            ^
282            Puts data to the data section.
283 */
284
285 unsigned char *silc_buffer_put(SilcBuffer sb, 
286                                unsigned char *data,
287                                unsigned int len)
288 {
289   assert((sb->tail - sb->data) >= len);
290   return memcpy(sb->data, data, len);
291 }
292
293 /* Puts data at the tail of the buffer. Returns pointer to the copied
294    data area. 
295
296    Example:
297    ---------------------------------
298    | head  | data           | tail |
299    ---------------------------------
300                             ^
301                             Puts data to the tail section.
302 */
303
304 unsigned char *silc_buffer_put_tail(SilcBuffer sb, 
305                                     unsigned char *data,
306                                     unsigned int len)
307 {
308   assert((sb->end - sb->tail) >= len);
309   return memcpy(sb->tail, data, len);
310 }
311
312 #endif