3 * $Id: buf.h,v 1.4 2004/04/08 01:36:15 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 #ifndef CATACOMB_BUF_H
31 #define CATACOMB_BUF_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
51 /*----- Data structures ---------------------------------------------------*/
55 * Buffers provide a simple stream-like interface for building and parsing
60 octet
*base
, *p
, *limit
; /* Pointers to the buffer */
61 unsigned f
; /* Various flags */
64 #define BF_BROKEN 1u /* Buffer is broken */
66 /*----- Useful macros -----------------------------------------------------*/
68 #define BBASE(b) ((b)->base)
69 #define BLIM(b) ((b)->limit)
70 #define BCUR(b) ((b)->p)
71 #define BSZ(b) ((b)->limit - (b)->base)
72 #define BLEN(b) ((b)->p - (b)->base)
73 #define BLEFT(b) ((b)->limit - (b)->p)
74 #define BSTEP(b, sz) ((b)->p += (sz))
75 #define BBAD(b) ((b)->f & BF_BROKEN)
76 #define BOK(b) (!BBAD(b))
78 #define BENSURE(b, sz) \
79 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
81 /*----- Functions provided ------------------------------------------------*/
83 /* --- @buf_init@ --- *
85 * Arguments: @buf *b@ = pointer to a buffer block
86 * @void *p@ = pointer to a buffer
87 * @size_t sz@ = size of the buffer
91 * Use: Initializes the buffer block appropriately.
94 extern void buf_init(buf */
*b*/
, void */
*p*/
, size_t /*sz*/);
96 /* --- @buf_break@ --- *
98 * Arguments: @buf *b@ = pointer to a buffer block
100 * Returns: Some negative value.
102 * Use: Marks a buffer as broken.
105 extern int buf_break(buf */
*b*/
);
107 /* --- @buf_flip@ --- *
109 * Arguments: @buf *b@ = pointer to a buffer block
113 * Use: Flips a buffer so that if you've just been writing to it,
114 * you can now read from the bit you've written.
117 extern void buf_flip(buf */
*b*/
);
119 /* --- @buf_ensure@ --- *
121 * Arguments: @buf *b@ = pointer to a buffer block
122 * @size_t sz@ = size of data wanted
124 * Returns: Zero if it worked, nonzero if there wasn't enough space.
126 * Use: Ensures that there are @sz@ bytes still in the buffer.
129 extern int buf_ensure(buf */
*b*/
, size_t /*sz*/);
131 /* --- @buf_get@ --- *
133 * Arguments: @buf *b@ = pointer to a buffer block
134 * @size_t sz@ = size of the buffer
136 * Returns: Pointer to the place in the buffer.
138 * Use: Reserves a space in the buffer of the requested size, and
139 * returns its start address.
142 extern void *buf_get(buf */
*b*/
, size_t /*sz*/);
144 /* --- @buf_put@ --- *
146 * Arguments: @buf *b@ = pointer to a buffer block
147 * @const void *p@ = pointer to a buffer
148 * @size_t sz@ = size of the buffer
150 * Returns: Zero if it worked, nonzero if there wasn't enough space.
152 * Use: Fetches data from some place and puts it in the buffer
155 extern int buf_put(buf */
*b*/
, const void */
*p*/
, size_t /*sz*/);
157 /* --- @buf_getbyte@ --- *
159 * Arguments: @buf *b@ = pointer to a buffer block
161 * Returns: A byte, or less than zero if there wasn't a byte there.
163 * Use: Gets a single byte from a buffer.
166 extern int buf_getbyte(buf */
*b*/
);
168 /* --- @buf_putbyte@ --- *
170 * Arguments: @buf *b@ = pointer to a buffer block
171 * @int ch@ = byte to write
173 * Returns: Zero if OK, nonzero if there wasn't enough space.
175 * Use: Puts a single byte in a buffer.
178 extern int buf_putbyte(buf */
*b*/
, int /*ch*/);
180 /* --- @buf_getu16@ --- *
182 * Arguments: @buf *b@ = pointer to a buffer block
183 * @uint16 *w@ = where to put the word
185 * Returns: Zero if OK, or nonzero if there wasn't a word there.
187 * Use: Gets a 16-bit word from a buffer.
190 extern int buf_getu16(buf */
*b*/
, uint16 */
*w*/
);
192 /* --- @buf_putu16@ --- *
194 * Arguments: @buf *b@ = pointer to a buffer block
195 * @uint16 w@ = word to write
197 * Returns: Zero if OK, nonzero if there wasn't enough space.
199 * Use: Puts a 16-but word in a buffer.
202 extern int buf_putu16(buf */
*b*/
, uint16
/*w*/);
204 /* --- @buf_getu32@ --- *
206 * Arguments: @buf *b@ = pointer to a buffer block
207 * @uint32 *w@ = where to put the word
209 * Returns: Zero if OK, or nonzero if there wasn't a word there.
211 * Use: Gets a 32-bit word from a buffer.
214 extern int buf_getu32(buf */
*b*/
, uint32 */
*w*/
);
216 /* --- @buf_putu32@ --- *
218 * Arguments: @buf *b@ = pointer to a buffer block
219 * @uint32 w@ = word to write
221 * Returns: Zero if OK, nonzero if there wasn't enough space.
223 * Use: Puts a 32-but word in a buffer.
226 extern int buf_putu32(buf */
*b*/
, uint32
/*w*/);
228 /* --- @buf_getmp@ --- *
230 * Arguments: @buf *b@ = pointer to a buffer block
232 * Returns: A multiprecision integer, or null if there wasn't one there.
234 * Use: Gets a multiprecision integer from a buffer.
237 extern mp
*buf_getmp(buf */
*b*/
);
239 /* --- @buf_putmp@ --- *
241 * Arguments: @buf *b@ = pointer to a buffer block
242 * @mp *m@ = a multiprecision integer
244 * Returns: Zero if it worked, nonzero if there wasn't enough space.
246 * Use: Puts a multiprecision integer to a buffer.
249 extern int buf_putmp(buf */
*b*/
, mp */
*m*/
);
251 /* --- @buf_getec@ --- *
253 * Arguments: @buf *b@ = pointer to a buffer block
254 * @ec *p@ = where to put the point
256 * Returns: Zero if it worked, nonzero if it failed.
258 * Use: Gets a multiprecision integer from a buffer. The point must
262 extern int buf_getec(buf */
*b*/
, ec */
*p*/
);
264 /* --- @buf_putec@ --- *
266 * Arguments: @buf *b@ = pointer to a buffer block
267 * @ec *p@ = an elliptic curve point
269 * Returns: Zero if it worked, nonzero if there wasn't enough space.
271 * Use: Puts an elliptic curve point to a buffer.
274 extern int buf_putec(buf */
*b*/
, ec */
*p*/
);
276 /*----- That's all, folks -------------------------------------------------*/