3 * $Id: buf.h,v 1.3 2004/04/01 12:50:09 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.3 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.2 2003/11/10 22:18:30 mdw
43 * Revision 1.1 2003/10/11 21:02:33 mdw
44 * Import buf stuff from tripe.
46 * Revision 1.1 2001/06/19 22:09:54 mdw
47 * Expose interface, for use in the proxy.
51 #ifndef CATACOMB_BUF_H
52 #define CATACOMB_BUF_H
58 /*----- Header files ------------------------------------------------------*/
62 #include <mLib/bits.h>
72 /*----- Data structures ---------------------------------------------------*/
76 * Buffers provide a simple stream-like interface for building and parsing
81 octet
*base
, *p
, *limit
; /* Pointers to the buffer */
82 unsigned f
; /* Various flags */
85 #define BF_BROKEN 1u /* Buffer is broken */
87 /*----- Useful macros -----------------------------------------------------*/
89 #define BBASE(b) ((b)->base)
90 #define BLIM(b) ((b)->limit)
91 #define BCUR(b) ((b)->p)
92 #define BSZ(b) ((b)->limit - (b)->base)
93 #define BLEN(b) ((b)->p - (b)->base)
94 #define BLEFT(b) ((b)->limit - (b)->p)
95 #define BSTEP(b, sz) ((b)->p += (sz))
96 #define BBAD(b) ((b)->f & BF_BROKEN)
97 #define BOK(b) (!BBAD(b))
99 #define BENSURE(b, sz) \
100 (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
102 /*----- Functions provided ------------------------------------------------*/
104 /* --- @buf_init@ --- *
106 * Arguments: @buf *b@ = pointer to a buffer block
107 * @void *p@ = pointer to a buffer
108 * @size_t sz@ = size of the buffer
112 * Use: Initializes the buffer block appropriately.
115 extern void buf_init(buf */
*b*/
, void */
*p*/
, size_t /*sz*/);
117 /* --- @buf_break@ --- *
119 * Arguments: @buf *b@ = pointer to a buffer block
121 * Returns: Some negative value.
123 * Use: Marks a buffer as broken.
126 extern int buf_break(buf */
*b*/
);
128 /* --- @buf_flip@ --- *
130 * Arguments: @buf *b@ = pointer to a buffer block
134 * Use: Flips a buffer so that if you've just been writing to it,
135 * you can now read from the bit you've written.
138 extern void buf_flip(buf */
*b*/
);
140 /* --- @buf_ensure@ --- *
142 * Arguments: @buf *b@ = pointer to a buffer block
143 * @size_t sz@ = size of data wanted
145 * Returns: Zero if it worked, nonzero if there wasn't enough space.
147 * Use: Ensures that there are @sz@ bytes still in the buffer.
150 extern int buf_ensure(buf */
*b*/
, size_t /*sz*/);
152 /* --- @buf_get@ --- *
154 * Arguments: @buf *b@ = pointer to a buffer block
155 * @size_t sz@ = size of the buffer
157 * Returns: Pointer to the place in the buffer.
159 * Use: Reserves a space in the buffer of the requested size, and
160 * returns its start address.
163 extern void *buf_get(buf */
*b*/
, size_t /*sz*/);
165 /* --- @buf_put@ --- *
167 * Arguments: @buf *b@ = pointer to a buffer block
168 * @const void *p@ = pointer to a buffer
169 * @size_t sz@ = size of the buffer
171 * Returns: Zero if it worked, nonzero if there wasn't enough space.
173 * Use: Fetches data from some place and puts it in the buffer
176 extern int buf_put(buf */
*b*/
, const void */
*p*/
, size_t /*sz*/);
178 /* --- @buf_getbyte@ --- *
180 * Arguments: @buf *b@ = pointer to a buffer block
182 * Returns: A byte, or less than zero if there wasn't a byte there.
184 * Use: Gets a single byte from a buffer.
187 extern int buf_getbyte(buf */
*b*/
);
189 /* --- @buf_putbyte@ --- *
191 * Arguments: @buf *b@ = pointer to a buffer block
192 * @int ch@ = byte to write
194 * Returns: Zero if OK, nonzero if there wasn't enough space.
196 * Use: Puts a single byte in a buffer.
199 extern int buf_putbyte(buf */
*b*/
, int /*ch*/);
201 /* --- @buf_getu16@ --- *
203 * Arguments: @buf *b@ = pointer to a buffer block
204 * @uint16 *w@ = where to put the word
206 * Returns: Zero if OK, or nonzero if there wasn't a word there.
208 * Use: Gets a 16-bit word from a buffer.
211 extern int buf_getu16(buf */
*b*/
, uint16 */
*w*/
);
213 /* --- @buf_putu16@ --- *
215 * Arguments: @buf *b@ = pointer to a buffer block
216 * @uint16 w@ = word to write
218 * Returns: Zero if OK, nonzero if there wasn't enough space.
220 * Use: Puts a 16-but word in a buffer.
223 extern int buf_putu16(buf */
*b*/
, uint16
/*w*/);
225 /* --- @buf_getu32@ --- *
227 * Arguments: @buf *b@ = pointer to a buffer block
228 * @uint32 *w@ = where to put the word
230 * Returns: Zero if OK, or nonzero if there wasn't a word there.
232 * Use: Gets a 32-bit word from a buffer.
235 extern int buf_getu32(buf */
*b*/
, uint32 */
*w*/
);
237 /* --- @buf_putu32@ --- *
239 * Arguments: @buf *b@ = pointer to a buffer block
240 * @uint32 w@ = word to write
242 * Returns: Zero if OK, nonzero if there wasn't enough space.
244 * Use: Puts a 32-but word in a buffer.
247 extern int buf_putu32(buf */
*b*/
, uint32
/*w*/);
249 /* --- @buf_getmp@ --- *
251 * Arguments: @buf *b@ = pointer to a buffer block
253 * Returns: A multiprecision integer, or null if there wasn't one there.
255 * Use: Gets a multiprecision integer from a buffer.
258 extern mp
*buf_getmp(buf */
*b*/
);
260 /* --- @buf_putmp@ --- *
262 * Arguments: @buf *b@ = pointer to a buffer block
263 * @mp *m@ = a multiprecision integer
265 * Returns: Zero if it worked, nonzero if there wasn't enough space.
267 * Use: Puts a multiprecision integer to a buffer.
270 extern int buf_putmp(buf */
*b*/
, mp */
*m*/
);
272 /* --- @buf_getec@ --- *
274 * Arguments: @buf *b@ = pointer to a buffer block
275 * @ec *p@ = where to put the point
277 * Returns: Zero if it worked, nonzero if it failed.
279 * Use: Gets a multiprecision integer from a buffer. The point must
283 extern int buf_getec(buf */
*b*/
, ec */
*p*/
);
285 /* --- @buf_putec@ --- *
287 * Arguments: @buf *b@ = pointer to a buffer block
288 * @ec *p@ = an elliptic curve point
290 * Returns: Zero if it worked, nonzero if there wasn't enough space.
292 * Use: Puts an elliptic curve point to a buffer.
295 extern int buf_putec(buf */
*b*/
, ec */
*p*/
);
297 /*----- That's all, folks -------------------------------------------------*/