chiark / gitweb /
Add cyclic group abstraction, with test code. Separate off exponentation
[catacomb] / buf.h
1 /* -*-c-*-
2  *
3  * $Id: buf.h,v 1.3 2004/04/01 12:50:09 mdw Exp $
4  *
5  * Reading and writing packet buffers
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: buf.h,v $
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.)
39  *
40  * Revision 1.2  2003/11/10 22:18:30  mdw
41  * Build fixes.
42  *
43  * Revision 1.1  2003/10/11 21:02:33  mdw
44  * Import buf stuff from tripe.
45  *
46  * Revision 1.1  2001/06/19 22:09:54  mdw
47  * Expose interface, for use in the proxy.
48  *
49  */
50
51 #ifndef CATACOMB_BUF_H
52 #define CATACOMB_BUF_H
53
54 #ifdef __cplusplus
55   extern "C" {
56 #endif
57
58 /*----- Header files ------------------------------------------------------*/
59
60 #include <stddef.h>
61
62 #include <mLib/bits.h>
63
64 #ifndef CATACOMB_MP_H
65 #  include "mp.h"
66 #endif
67
68 #ifndef CATACOMB_EC_H
69 #  include "ec.h"
70 #endif
71
72 /*----- Data structures ---------------------------------------------------*/
73
74 /* --- Buffers --- *
75  *
76  * Buffers provide a simple stream-like interface for building and parsing
77  * packets.
78  */
79
80 typedef struct buf {
81   octet *base, *p, *limit;              /* Pointers to the buffer */
82   unsigned f;                           /* Various flags */
83 } buf;
84
85 #define BF_BROKEN 1u                    /* Buffer is broken */
86
87 /*----- Useful macros -----------------------------------------------------*/
88
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))
98
99 #define BENSURE(b, sz)                                                  \
100   (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
101
102 /*----- Functions provided ------------------------------------------------*/
103
104 /* --- @buf_init@ --- *
105  *
106  * Arguments:   @buf *b@ = pointer to a buffer block
107  *              @void *p@ = pointer to a buffer
108  *              @size_t sz@ = size of the buffer
109  *
110  * Returns:     ---
111  *
112  * Use:         Initializes the buffer block appropriately.
113  */
114
115 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
116
117 /* --- @buf_break@ --- *
118  *
119  * Arguments:   @buf *b@ = pointer to a buffer block
120  *
121  * Returns:     Some negative value.
122  *
123  * Use:         Marks a buffer as broken.
124  */
125
126 extern int buf_break(buf */*b*/);
127
128 /* --- @buf_flip@ --- *
129  *
130  * Arguments:   @buf *b@ = pointer to a buffer block
131  *
132  * Returns:     ---
133  *
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.
136  */
137
138 extern void buf_flip(buf */*b*/);
139
140 /* --- @buf_ensure@ --- *
141  *
142  * Arguments:   @buf *b@ = pointer to a buffer block
143  *              @size_t sz@ = size of data wanted
144  *
145  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
146  *
147  * Use:         Ensures that there are @sz@ bytes still in the buffer.
148  */
149
150 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
151
152 /* --- @buf_get@ --- *
153  *
154  * Arguments:   @buf *b@ = pointer to a buffer block
155  *              @size_t sz@ = size of the buffer
156  *
157  * Returns:     Pointer to the place in the buffer.
158  *
159  * Use:         Reserves a space in the buffer of the requested size, and
160  *              returns its start address.
161  */
162
163 extern void *buf_get(buf */*b*/, size_t /*sz*/);
164
165 /* --- @buf_put@ --- *
166  *
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
170  *
171  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
172  *
173  * Use:         Fetches data from some place and puts it in the buffer
174  */
175
176 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
177
178 /* --- @buf_getbyte@ --- *
179  *
180  * Arguments:   @buf *b@ = pointer to a buffer block
181  *
182  * Returns:     A byte, or less than zero if there wasn't a byte there.
183  *
184  * Use:         Gets a single byte from a buffer.
185  */
186
187 extern int buf_getbyte(buf */*b*/);
188
189 /* --- @buf_putbyte@ --- *
190  *
191  * Arguments:   @buf *b@ = pointer to a buffer block
192  *              @int ch@ = byte to write
193  *
194  * Returns:     Zero if OK, nonzero if there wasn't enough space.
195  *
196  * Use:         Puts a single byte in a buffer.
197  */
198
199 extern int buf_putbyte(buf */*b*/, int /*ch*/);
200
201 /* --- @buf_getu16@ --- *
202  *
203  * Arguments:   @buf *b@ = pointer to a buffer block
204  *              @uint16 *w@ = where to put the word
205  *
206  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
207  *
208  * Use:         Gets a 16-bit word from a buffer.
209  */
210
211 extern int buf_getu16(buf */*b*/, uint16 */*w*/);
212
213 /* --- @buf_putu16@ --- *
214  *
215  * Arguments:   @buf *b@ = pointer to a buffer block
216  *              @uint16 w@ = word to write
217  *
218  * Returns:     Zero if OK, nonzero if there wasn't enough space.
219  *
220  * Use:         Puts a 16-but word in a buffer.
221  */
222
223 extern int buf_putu16(buf */*b*/, uint16 /*w*/);
224
225 /* --- @buf_getu32@ --- *
226  *
227  * Arguments:   @buf *b@ = pointer to a buffer block
228  *              @uint32 *w@ = where to put the word
229  *
230  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
231  *
232  * Use:         Gets a 32-bit word from a buffer.
233  */
234
235 extern int buf_getu32(buf */*b*/, uint32 */*w*/);
236
237 /* --- @buf_putu32@ --- *
238  *
239  * Arguments:   @buf *b@ = pointer to a buffer block
240  *              @uint32 w@ = word to write
241  *
242  * Returns:     Zero if OK, nonzero if there wasn't enough space.
243  *
244  * Use:         Puts a 32-but word in a buffer.
245  */
246
247 extern int buf_putu32(buf */*b*/, uint32 /*w*/);
248
249 /* --- @buf_getmp@ --- *
250  *
251  * Arguments:   @buf *b@ = pointer to a buffer block
252  *
253  * Returns:     A multiprecision integer, or null if there wasn't one there.
254  *
255  * Use:         Gets a multiprecision integer from a buffer.
256  */
257
258 extern mp *buf_getmp(buf */*b*/);
259
260 /* --- @buf_putmp@ --- *
261  *
262  * Arguments:   @buf *b@ = pointer to a buffer block
263  *              @mp *m@ = a multiprecision integer
264  *
265  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
266  *
267  * Use:         Puts a multiprecision integer to a buffer.
268  */
269
270 extern int buf_putmp(buf */*b*/, mp */*m*/);
271
272 /* --- @buf_getec@ --- *
273  *
274  * Arguments:   @buf *b@ = pointer to a buffer block
275  *              @ec *p@ = where to put the point
276  *
277  * Returns:     Zero if it worked, nonzero if it failed.
278  *
279  * Use:         Gets a multiprecision integer from a buffer.  The point must
280  *              be initialized.
281  */
282
283 extern int buf_getec(buf */*b*/, ec */*p*/);
284
285 /* --- @buf_putec@ --- *
286  *
287  * Arguments:   @buf *b@ = pointer to a buffer block
288  *              @ec *p@ = an elliptic curve point
289  *
290  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
291  *
292  * Use:         Puts an elliptic curve point to a buffer.
293  */
294
295 extern int buf_putec(buf */*b*/, ec */*p*/);
296
297 /*----- That's all, folks -------------------------------------------------*/
298
299 #ifdef __cplusplus
300   }
301 #endif
302
303 #endif