3 * $Id: buf.h,v 1.1 2003/10/11 21:02:33 mdw Exp $
5 * [Reading and writing packet buffers *
6 * (c) 2001 Straylight/Edgeware
9 /*----- Licensing notice --------------------------------------------------*
11 * This file is part of Catacomb.
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.
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.
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,
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2003/10/11 21:02:33 mdw
33 * Import buf stuff from tripe.
35 * Revision 1.1 2001/06/19 22:09:54 mdw
36 * Expose interface, for use in the proxy.
47 /*----- Header files ------------------------------------------------------*/
51 #include <mLib/bits.h>
53 #include <catacomb/mp.h>
55 /*----- Data structures ---------------------------------------------------*/
59 * Buffers provide a simple stream-like interface for building and parsing
64 octet
*base
, *p
, *limit
; /* Pointers to the buffer */
65 unsigned f
; /* Various flags */
68 #define BF_BROKEN 1u /* Buffer is broken */
70 /*----- Useful macros -----------------------------------------------------*/
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))
82 #define BENSURE(b, sz) \
83 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
85 /*----- Functions provided ------------------------------------------------*/
87 /* --- @buf_init@ --- *
89 * Arguments: @buf *b@ = pointer to a buffer block
90 * @void *p@ = pointer to a buffer
91 * @size_t sz@ = size of the buffer
95 * Use: Initializes the buffer block appropriately.
98 extern void buf_init(buf */
*b*/
, void */
*p*/
, size_t /*sz*/);
100 /* --- @buf_break@ --- *
102 * Arguments: @buf *b@ = pointer to a buffer block
104 * Returns: Some negative value.
106 * Use: Marks a buffer as broken.
109 extern int buf_break(buf */
*b*/
);
111 /* --- @buf_flip@ --- *
113 * Arguments: @buf *b@ = pointer to a buffer block
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.
121 extern void buf_flip(buf */
*b*/
);
123 /* --- @buf_ensure@ --- *
125 * Arguments: @buf *b@ = pointer to a buffer block
126 * @size_t sz@ = size of data wanted
128 * Returns: Zero if it worked, nonzero if there wasn't enough space.
130 * Use: Ensures that there are @sz@ bytes still in the buffer.
133 extern int buf_ensure(buf */
*b*/
, size_t /*sz*/);
135 /* --- @buf_get@ --- *
137 * Arguments: @buf *b@ = pointer to a buffer block
138 * @size_t sz@ = size of the buffer
140 * Returns: Pointer to the place in the buffer.
142 * Use: Reserves a space in the buffer of the requested size, and
143 * returns its start address.
146 extern void *buf_get(buf */
*b*/
, size_t /*sz*/);
148 /* --- @buf_put@ --- *
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
154 * Returns: Zero if it worked, nonzero if there wasn't enough space.
156 * Use: Fetches data from some place and puts it in the buffer
159 extern int buf_put(buf */
*b*/
, const void */
*p*/
, size_t /*sz*/);
161 /* --- @buf_getbyte@ --- *
163 * Arguments: @buf *b@ = pointer to a buffer block
165 * Returns: A byte, or less than zero if there wasn't a byte there.
167 * Use: Gets a single byte from a buffer.
170 extern int buf_getbyte(buf */
*b*/
);
172 /* --- @buf_putbyte@ --- *
174 * Arguments: @buf *b@ = pointer to a buffer block
175 * @int ch@ = byte to write
177 * Returns: Zero if OK, nonzero if there wasn't enough space.
179 * Use: Puts a single byte in a buffer.
182 extern int buf_putbyte(buf */
*b*/
, int /*ch*/);
184 /* --- @buf_getu16@ --- *
186 * Arguments: @buf *b@ = pointer to a buffer block
187 * @uint16 *w@ = where to put the word
189 * Returns: Zero if OK, or nonzero if there wasn't a word there.
191 * Use: Gets a 16-bit word from a buffer.
194 extern int buf_getu16(buf */
*b*/
, uint16 */
*w*/
);
196 /* --- @buf_putu16@ --- *
198 * Arguments: @buf *b@ = pointer to a buffer block
199 * @uint16 w@ = word to write
201 * Returns: Zero if OK, nonzero if there wasn't enough space.
203 * Use: Puts a 16-but word in a buffer.
206 extern int buf_putu16(buf */
*b*/
, uint16
/*w*/);
208 /* --- @buf_getu32@ --- *
210 * Arguments: @buf *b@ = pointer to a buffer block
211 * @uint32 *w@ = where to put the word
213 * Returns: Zero if OK, or nonzero if there wasn't a word there.
215 * Use: Gets a 32-bit word from a buffer.
218 extern int buf_getu32(buf */
*b*/
, uint32 */
*w*/
);
220 /* --- @buf_putu32@ --- *
222 * Arguments: @buf *b@ = pointer to a buffer block
223 * @uint32 w@ = word to write
225 * Returns: Zero if OK, nonzero if there wasn't enough space.
227 * Use: Puts a 32-but word in a buffer.
230 extern int buf_putu32(buf */
*b*/
, uint32
/*w*/);
232 /* --- @buf_getmp@ --- *
234 * Arguments: @buf *b@ = pointer to a buffer block
236 * Returns: A multiprecision integer, or null if there wasn't one there.
238 * Use: Gets a multiprecision integer from a buffer.
241 extern mp
*buf_getmp(buf */
*b*/
);
243 /* --- @buf_putmp@ --- *
245 * Arguments: @buf *b@ = pointer to a buffer block
246 * @mp *m@ = a multiprecision integer
248 * Returns: Zero if it worked, nonzero if there wasn't enough space.
250 * Use: Puts a multiprecision integer to a buffer.
253 extern int buf_putmp(buf */
*b*/
, mp */
*m*/
);
255 /*----- That's all, folks -------------------------------------------------*/