chiark / gitweb /
Import buf stuff from tripe.
[catacomb] / buf.h
1 /* -*-c-*-
2  *
3  * $Id: buf.h,v 1.1 2003/10/11 21:02:33 mdw Exp $
4  *
5  * [Reading and writing packet buffers *
6  * (c) 2001 Straylight/Edgeware
7  */
8
9 /*----- Licensing notice --------------------------------------------------* 
10  *
11  * This file is part of Catacomb.
12  *
13  * Catacomb is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Library General Public License as
15  * published by the Free Software Foundation; either version 2 of the
16  * License, or (at your option) any later version.
17  * 
18  * Catacomb is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU Library General Public License for more details.
22  * 
23  * You should have received a copy of the GNU Library General Public
24  * License along with Catacomb; if not, write to the Free
25  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26  * MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: buf.h,v $
32  * Revision 1.1  2003/10/11 21:02:33  mdw
33  * Import buf stuff from tripe.
34  *
35  * Revision 1.1  2001/06/19 22:09:54  mdw
36  * Expose interface, for use in the proxy.
37  *
38  */
39
40 #ifndef BUF_H
41 #define BUF_H
42
43 #ifdef __cplusplus
44   extern "C" {
45 #endif
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <stddef.h>
50
51 #include <mLib/bits.h>
52
53 #include <catacomb/mp.h>
54
55 /*----- Data structures ---------------------------------------------------*/
56
57 /* --- Buffers --- *
58  *
59  * Buffers provide a simple stream-like interface for building and parsing
60  * packets.
61  */
62
63 typedef struct buf {
64   octet *base, *p, *limit;              /* Pointers to the buffer */
65   unsigned f;                           /* Various flags */
66 } buf;
67
68 #define BF_BROKEN 1u                    /* Buffer is broken */
69
70 /*----- Useful macros -----------------------------------------------------*/
71
72 #define BBASE(b) ((b)->base)
73 #define BLIM(b) ((b)->limit)
74 #define BCUR(b) ((b)->p)
75 #define BSZ(b) ((b)->limit - (b)->base)
76 #define BLEN(b) ((b)->p - (b)->base)
77 #define BLEFT(b) ((b)->limit - (b)->p)
78 #define BSTEP(b, sz) ((b)->p += (sz))
79 #define BBAD(b) ((b)->f & BF_BROKEN)
80 #define BOK(b) (!BBAD(b))
81
82 #define BENSURE(b, sz)                                                  \
83   (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
84
85 /*----- Functions provided ------------------------------------------------*/
86
87 /* --- @buf_init@ --- *
88  *
89  * Arguments:   @buf *b@ = pointer to a buffer block
90  *              @void *p@ = pointer to a buffer
91  *              @size_t sz@ = size of the buffer
92  *
93  * Returns:     ---
94  *
95  * Use:         Initializes the buffer block appropriately.
96  */
97
98 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
99
100 /* --- @buf_break@ --- *
101  *
102  * Arguments:   @buf *b@ = pointer to a buffer block
103  *
104  * Returns:     Some negative value.
105  *
106  * Use:         Marks a buffer as broken.
107  */
108
109 extern int buf_break(buf */*b*/);
110
111 /* --- @buf_flip@ --- *
112  *
113  * Arguments:   @buf *b@ = pointer to a buffer block
114  *
115  * Returns:     ---
116  *
117  * Use:         Flips a buffer so that if you've just been writing to it,
118  *              you can now read from the bit you've written.
119  */
120
121 extern void buf_flip(buf */*b*/);
122
123 /* --- @buf_ensure@ --- *
124  *
125  * Arguments:   @buf *b@ = pointer to a buffer block
126  *              @size_t sz@ = size of data wanted
127  *
128  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
129  *
130  * Use:         Ensures that there are @sz@ bytes still in the buffer.
131  */
132
133 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
134
135 /* --- @buf_get@ --- *
136  *
137  * Arguments:   @buf *b@ = pointer to a buffer block
138  *              @size_t sz@ = size of the buffer
139  *
140  * Returns:     Pointer to the place in the buffer.
141  *
142  * Use:         Reserves a space in the buffer of the requested size, and
143  *              returns its start address.
144  */
145
146 extern void *buf_get(buf */*b*/, size_t /*sz*/);
147
148 /* --- @buf_put@ --- *
149  *
150  * Arguments:   @buf *b@ = pointer to a buffer block
151  *              @const void *p@ = pointer to a buffer
152  *              @size_t sz@ = size of the buffer
153  *
154  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
155  *
156  * Use:         Fetches data from some place and puts it in the buffer
157  */
158
159 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
160
161 /* --- @buf_getbyte@ --- *
162  *
163  * Arguments:   @buf *b@ = pointer to a buffer block
164  *
165  * Returns:     A byte, or less than zero if there wasn't a byte there.
166  *
167  * Use:         Gets a single byte from a buffer.
168  */
169
170 extern int buf_getbyte(buf */*b*/);
171
172 /* --- @buf_putbyte@ --- *
173  *
174  * Arguments:   @buf *b@ = pointer to a buffer block
175  *              @int ch@ = byte to write
176  *
177  * Returns:     Zero if OK, nonzero if there wasn't enough space.
178  *
179  * Use:         Puts a single byte in a buffer.
180  */
181
182 extern int buf_putbyte(buf */*b*/, int /*ch*/);
183
184 /* --- @buf_getu16@ --- *
185  *
186  * Arguments:   @buf *b@ = pointer to a buffer block
187  *              @uint16 *w@ = where to put the word
188  *
189  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
190  *
191  * Use:         Gets a 16-bit word from a buffer.
192  */
193
194 extern int buf_getu16(buf */*b*/, uint16 */*w*/);
195
196 /* --- @buf_putu16@ --- *
197  *
198  * Arguments:   @buf *b@ = pointer to a buffer block
199  *              @uint16 w@ = word to write
200  *
201  * Returns:     Zero if OK, nonzero if there wasn't enough space.
202  *
203  * Use:         Puts a 16-but word in a buffer.
204  */
205
206 extern int buf_putu16(buf */*b*/, uint16 /*w*/);
207
208 /* --- @buf_getu32@ --- *
209  *
210  * Arguments:   @buf *b@ = pointer to a buffer block
211  *              @uint32 *w@ = where to put the word
212  *
213  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
214  *
215  * Use:         Gets a 32-bit word from a buffer.
216  */
217
218 extern int buf_getu32(buf */*b*/, uint32 */*w*/);
219
220 /* --- @buf_putu32@ --- *
221  *
222  * Arguments:   @buf *b@ = pointer to a buffer block
223  *              @uint32 w@ = word to write
224  *
225  * Returns:     Zero if OK, nonzero if there wasn't enough space.
226  *
227  * Use:         Puts a 32-but word in a buffer.
228  */
229
230 extern int buf_putu32(buf */*b*/, uint32 /*w*/);
231
232 /* --- @buf_getmp@ --- *
233  *
234  * Arguments:   @buf *b@ = pointer to a buffer block
235  *
236  * Returns:     A multiprecision integer, or null if there wasn't one there.
237  *
238  * Use:         Gets a multiprecision integer from a buffer.
239  */
240
241 extern mp *buf_getmp(buf */*b*/);
242
243 /* --- @buf_putmp@ --- *
244  *
245  * Arguments:   @buf *b@ = pointer to a buffer block
246  *              @mp *m@ = a multiprecision integer
247  *
248  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
249  *
250  * Use:         Puts a multiprecision integer to a buffer.
251  */
252
253 extern int buf_putmp(buf */*b*/, mp */*m*/);
254
255 /*----- That's all, folks -------------------------------------------------*/
256
257 #ifdef __cplusplus
258   }
259 #endif
260
261 #endif