3 * $Id: buf.h,v 1.2 2003/11/10 22:18:30 mdw Exp $
5 * Reading and writing packet buffers
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2003/11/10 22:18:30 mdw
36 * Revision 1.1 2003/10/11 21:02:33 mdw
37 * Import buf stuff from tripe.
39 * Revision 1.1 2001/06/19 22:09:54 mdw
40 * Expose interface, for use in the proxy.
44 #ifndef CATACOMB_BUF_H
45 #define CATACOMB_BUF_H
51 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/bits.h>
61 /*----- Data structures ---------------------------------------------------*/
65 * Buffers provide a simple stream-like interface for building and parsing
70 octet
*base
, *p
, *limit
; /* Pointers to the buffer */
71 unsigned f
; /* Various flags */
74 #define BF_BROKEN 1u /* Buffer is broken */
76 /*----- Useful macros -----------------------------------------------------*/
78 #define BBASE(b) ((b)->base)
79 #define BLIM(b) ((b)->limit)
80 #define BCUR(b) ((b)->p)
81 #define BSZ(b) ((b)->limit - (b)->base)
82 #define BLEN(b) ((b)->p - (b)->base)
83 #define BLEFT(b) ((b)->limit - (b)->p)
84 #define BSTEP(b, sz) ((b)->p += (sz))
85 #define BBAD(b) ((b)->f & BF_BROKEN)
86 #define BOK(b) (!BBAD(b))
88 #define BENSURE(b, sz) \
89 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
91 /*----- Functions provided ------------------------------------------------*/
93 /* --- @buf_init@ --- *
95 * Arguments: @buf *b@ = pointer to a buffer block
96 * @void *p@ = pointer to a buffer
97 * @size_t sz@ = size of the buffer
101 * Use: Initializes the buffer block appropriately.
104 extern void buf_init(buf */
*b*/
, void */
*p*/
, size_t /*sz*/);
106 /* --- @buf_break@ --- *
108 * Arguments: @buf *b@ = pointer to a buffer block
110 * Returns: Some negative value.
112 * Use: Marks a buffer as broken.
115 extern int buf_break(buf */
*b*/
);
117 /* --- @buf_flip@ --- *
119 * Arguments: @buf *b@ = pointer to a buffer block
123 * Use: Flips a buffer so that if you've just been writing to it,
124 * you can now read from the bit you've written.
127 extern void buf_flip(buf */
*b*/
);
129 /* --- @buf_ensure@ --- *
131 * Arguments: @buf *b@ = pointer to a buffer block
132 * @size_t sz@ = size of data wanted
134 * Returns: Zero if it worked, nonzero if there wasn't enough space.
136 * Use: Ensures that there are @sz@ bytes still in the buffer.
139 extern int buf_ensure(buf */
*b*/
, size_t /*sz*/);
141 /* --- @buf_get@ --- *
143 * Arguments: @buf *b@ = pointer to a buffer block
144 * @size_t sz@ = size of the buffer
146 * Returns: Pointer to the place in the buffer.
148 * Use: Reserves a space in the buffer of the requested size, and
149 * returns its start address.
152 extern void *buf_get(buf */
*b*/
, size_t /*sz*/);
154 /* --- @buf_put@ --- *
156 * Arguments: @buf *b@ = pointer to a buffer block
157 * @const void *p@ = pointer to a buffer
158 * @size_t sz@ = size of the buffer
160 * Returns: Zero if it worked, nonzero if there wasn't enough space.
162 * Use: Fetches data from some place and puts it in the buffer
165 extern int buf_put(buf */
*b*/
, const void */
*p*/
, size_t /*sz*/);
167 /* --- @buf_getbyte@ --- *
169 * Arguments: @buf *b@ = pointer to a buffer block
171 * Returns: A byte, or less than zero if there wasn't a byte there.
173 * Use: Gets a single byte from a buffer.
176 extern int buf_getbyte(buf */
*b*/
);
178 /* --- @buf_putbyte@ --- *
180 * Arguments: @buf *b@ = pointer to a buffer block
181 * @int ch@ = byte to write
183 * Returns: Zero if OK, nonzero if there wasn't enough space.
185 * Use: Puts a single byte in a buffer.
188 extern int buf_putbyte(buf */
*b*/
, int /*ch*/);
190 /* --- @buf_getu16@ --- *
192 * Arguments: @buf *b@ = pointer to a buffer block
193 * @uint16 *w@ = where to put the word
195 * Returns: Zero if OK, or nonzero if there wasn't a word there.
197 * Use: Gets a 16-bit word from a buffer.
200 extern int buf_getu16(buf */
*b*/
, uint16 */
*w*/
);
202 /* --- @buf_putu16@ --- *
204 * Arguments: @buf *b@ = pointer to a buffer block
205 * @uint16 w@ = word to write
207 * Returns: Zero if OK, nonzero if there wasn't enough space.
209 * Use: Puts a 16-but word in a buffer.
212 extern int buf_putu16(buf */
*b*/
, uint16
/*w*/);
214 /* --- @buf_getu32@ --- *
216 * Arguments: @buf *b@ = pointer to a buffer block
217 * @uint32 *w@ = where to put the word
219 * Returns: Zero if OK, or nonzero if there wasn't a word there.
221 * Use: Gets a 32-bit word from a buffer.
224 extern int buf_getu32(buf */
*b*/
, uint32 */
*w*/
);
226 /* --- @buf_putu32@ --- *
228 * Arguments: @buf *b@ = pointer to a buffer block
229 * @uint32 w@ = word to write
231 * Returns: Zero if OK, nonzero if there wasn't enough space.
233 * Use: Puts a 32-but word in a buffer.
236 extern int buf_putu32(buf */
*b*/
, uint32
/*w*/);
238 /* --- @buf_getmp@ --- *
240 * Arguments: @buf *b@ = pointer to a buffer block
242 * Returns: A multiprecision integer, or null if there wasn't one there.
244 * Use: Gets a multiprecision integer from a buffer.
247 extern mp
*buf_getmp(buf */
*b*/
);
249 /* --- @buf_putmp@ --- *
251 * Arguments: @buf *b@ = pointer to a buffer block
252 * @mp *m@ = a multiprecision integer
254 * Returns: Zero if it worked, nonzero if there wasn't enough space.
256 * Use: Puts a multiprecision integer to a buffer.
259 extern int buf_putmp(buf */
*b*/
, mp */
*m*/
);
261 /*----- That's all, folks -------------------------------------------------*/