chiark / gitweb /
Merge and close elliptic curve branch.
[catacomb] / buf.h
1 /* -*-c-*-
2  *
3  * $Id: buf.h,v 1.2 2003/11/10 22:18:30 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.2  2003/11/10 22:18:30  mdw
34  * Build fixes.
35  *
36  * Revision 1.1  2003/10/11 21:02:33  mdw
37  * Import buf stuff from tripe.
38  *
39  * Revision 1.1  2001/06/19 22:09:54  mdw
40  * Expose interface, for use in the proxy.
41  *
42  */
43
44 #ifndef CATACOMB_BUF_H
45 #define CATACOMB_BUF_H
46
47 #ifdef __cplusplus
48   extern "C" {
49 #endif
50
51 /*----- Header files ------------------------------------------------------*/
52
53 #include <stddef.h>
54
55 #include <mLib/bits.h>
56
57 #ifndef CATACOMB_MP_H
58 #  include "mp.h"
59 #endif
60
61 /*----- Data structures ---------------------------------------------------*/
62
63 /* --- Buffers --- *
64  *
65  * Buffers provide a simple stream-like interface for building and parsing
66  * packets.
67  */
68
69 typedef struct buf {
70   octet *base, *p, *limit;              /* Pointers to the buffer */
71   unsigned f;                           /* Various flags */
72 } buf;
73
74 #define BF_BROKEN 1u                    /* Buffer is broken */
75
76 /*----- Useful macros -----------------------------------------------------*/
77
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))
87
88 #define BENSURE(b, sz)                                                  \
89   (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
90
91 /*----- Functions provided ------------------------------------------------*/
92
93 /* --- @buf_init@ --- *
94  *
95  * Arguments:   @buf *b@ = pointer to a buffer block
96  *              @void *p@ = pointer to a buffer
97  *              @size_t sz@ = size of the buffer
98  *
99  * Returns:     ---
100  *
101  * Use:         Initializes the buffer block appropriately.
102  */
103
104 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
105
106 /* --- @buf_break@ --- *
107  *
108  * Arguments:   @buf *b@ = pointer to a buffer block
109  *
110  * Returns:     Some negative value.
111  *
112  * Use:         Marks a buffer as broken.
113  */
114
115 extern int buf_break(buf */*b*/);
116
117 /* --- @buf_flip@ --- *
118  *
119  * Arguments:   @buf *b@ = pointer to a buffer block
120  *
121  * Returns:     ---
122  *
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.
125  */
126
127 extern void buf_flip(buf */*b*/);
128
129 /* --- @buf_ensure@ --- *
130  *
131  * Arguments:   @buf *b@ = pointer to a buffer block
132  *              @size_t sz@ = size of data wanted
133  *
134  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
135  *
136  * Use:         Ensures that there are @sz@ bytes still in the buffer.
137  */
138
139 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
140
141 /* --- @buf_get@ --- *
142  *
143  * Arguments:   @buf *b@ = pointer to a buffer block
144  *              @size_t sz@ = size of the buffer
145  *
146  * Returns:     Pointer to the place in the buffer.
147  *
148  * Use:         Reserves a space in the buffer of the requested size, and
149  *              returns its start address.
150  */
151
152 extern void *buf_get(buf */*b*/, size_t /*sz*/);
153
154 /* --- @buf_put@ --- *
155  *
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
159  *
160  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
161  *
162  * Use:         Fetches data from some place and puts it in the buffer
163  */
164
165 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
166
167 /* --- @buf_getbyte@ --- *
168  *
169  * Arguments:   @buf *b@ = pointer to a buffer block
170  *
171  * Returns:     A byte, or less than zero if there wasn't a byte there.
172  *
173  * Use:         Gets a single byte from a buffer.
174  */
175
176 extern int buf_getbyte(buf */*b*/);
177
178 /* --- @buf_putbyte@ --- *
179  *
180  * Arguments:   @buf *b@ = pointer to a buffer block
181  *              @int ch@ = byte to write
182  *
183  * Returns:     Zero if OK, nonzero if there wasn't enough space.
184  *
185  * Use:         Puts a single byte in a buffer.
186  */
187
188 extern int buf_putbyte(buf */*b*/, int /*ch*/);
189
190 /* --- @buf_getu16@ --- *
191  *
192  * Arguments:   @buf *b@ = pointer to a buffer block
193  *              @uint16 *w@ = where to put the word
194  *
195  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
196  *
197  * Use:         Gets a 16-bit word from a buffer.
198  */
199
200 extern int buf_getu16(buf */*b*/, uint16 */*w*/);
201
202 /* --- @buf_putu16@ --- *
203  *
204  * Arguments:   @buf *b@ = pointer to a buffer block
205  *              @uint16 w@ = word to write
206  *
207  * Returns:     Zero if OK, nonzero if there wasn't enough space.
208  *
209  * Use:         Puts a 16-but word in a buffer.
210  */
211
212 extern int buf_putu16(buf */*b*/, uint16 /*w*/);
213
214 /* --- @buf_getu32@ --- *
215  *
216  * Arguments:   @buf *b@ = pointer to a buffer block
217  *              @uint32 *w@ = where to put the word
218  *
219  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
220  *
221  * Use:         Gets a 32-bit word from a buffer.
222  */
223
224 extern int buf_getu32(buf */*b*/, uint32 */*w*/);
225
226 /* --- @buf_putu32@ --- *
227  *
228  * Arguments:   @buf *b@ = pointer to a buffer block
229  *              @uint32 w@ = word to write
230  *
231  * Returns:     Zero if OK, nonzero if there wasn't enough space.
232  *
233  * Use:         Puts a 32-but word in a buffer.
234  */
235
236 extern int buf_putu32(buf */*b*/, uint32 /*w*/);
237
238 /* --- @buf_getmp@ --- *
239  *
240  * Arguments:   @buf *b@ = pointer to a buffer block
241  *
242  * Returns:     A multiprecision integer, or null if there wasn't one there.
243  *
244  * Use:         Gets a multiprecision integer from a buffer.
245  */
246
247 extern mp *buf_getmp(buf */*b*/);
248
249 /* --- @buf_putmp@ --- *
250  *
251  * Arguments:   @buf *b@ = pointer to a buffer block
252  *              @mp *m@ = a multiprecision integer
253  *
254  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
255  *
256  * Use:         Puts a multiprecision integer to a buffer.
257  */
258
259 extern int buf_putmp(buf */*b*/, mp */*m*/);
260
261 /*----- That's all, folks -------------------------------------------------*/
262
263 #ifdef __cplusplus
264   }
265 #endif
266
267 #endif