chiark / gitweb /
Support expiry of other peers' public keys.
[tripe] / buf.c
1 /* -*-c-*-
2  *
3  * $Id: buf.c,v 1.4 2001/06/19 22:09:54 mdw Exp $
4  *
5  * Buffer handling
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE 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 General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: buf.c,v $
32  * Revision 1.4  2001/06/19 22:09:54  mdw
33  * Expose interface, for use in the proxy.
34  *
35  * Revision 1.3  2001/03/03 12:06:48  mdw
36  * Use 16-bit lengths on MPs, since there's a packet limit of 64K anyway.
37  *
38  * Revision 1.2  2001/02/16 21:23:20  mdw
39  * Various minor changes.  Check that MPs are in canonical form when
40  * loading.
41  *
42  * Revision 1.1  2001/02/03 20:26:37  mdw
43  * Initial checkin.
44  *
45  */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <string.h>
50
51 #include <catacomb/mp.h>
52
53 #include "buf.h"
54
55 /*----- Main code ---------------------------------------------------------*/
56
57 /* --- @buf_init@ --- *
58  *
59  * Arguments:   @buf *b@ = pointer to a buffer block
60  *              @void *p@ = pointer to a buffer
61  *              @size_t sz@ = size of the buffer
62  *
63  * Returns:     ---
64  *
65  * Use:         Initializes the buffer block appropriately.
66  */
67
68 void buf_init(buf *b, void *p, size_t sz)
69 {
70   b->base = b->p = p;
71   b->limit = b->p + sz;
72   b->f = 0;
73 }
74
75 /* --- @buf_break@ --- *
76  *
77  * Arguments:   @buf *b@ = pointer to a buffer block
78  *
79  * Returns:     Some negative value.
80  *
81  * Use:         Marks a buffer as broken.
82  */
83
84 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
85
86 /* --- @buf_flip@ --- *
87  *
88  * Arguments:   @buf *b@ = pointer to a buffer block
89  *
90  * Returns:     ---
91  *
92  * Use:         Flips a buffer so that if you've just been writing to it,
93  *              you can now read from the bit you've written.
94  */
95
96 void buf_flip(buf *b)
97 {
98   b->limit = b->p;
99   b->p = b->base;
100 }
101
102 /* --- @buf_ensure@ --- *
103  *
104  * Arguments:   @buf *b@ = pointer to a buffer block
105  *              @size_t sz@ = size of data wanted
106  *
107  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
108  *
109  * Use:         Ensures that there are @sz@ bytes still in the buffer.
110  */
111
112 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
113
114 /* --- @buf_get@ --- *
115  *
116  * Arguments:   @buf *b@ = pointer to a buffer block
117  *              @size_t sz@ = size of the buffer
118  *
119  * Returns:     Pointer to the place in the buffer.
120  *
121  * Use:         Reserves a space in the buffer of the requested size, and
122  *              returns its start address.
123  */
124
125 void *buf_get(buf *b, size_t sz)
126 {
127   void *p;
128   if (BENSURE(b, sz))
129     return (0);
130   p = BCUR(b);
131   BSTEP(b, sz);
132   return (p);
133 }
134
135 /* --- @buf_put@ --- *
136  *
137  * Arguments:   @buf *b@ = pointer to a buffer block
138  *              @const void *p@ = pointer to a buffer
139  *              @size_t sz@ = size of the buffer
140  *
141  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
142  *
143  * Use:         Fetches data from some place and puts it in the buffer
144  */
145
146 int buf_put(buf *b, const void *p, size_t sz)
147 {
148   if (BENSURE(b, sz))
149     return (-1);
150   memcpy(BCUR(b), p, sz);
151   BSTEP(b, sz);
152   return (0);
153 }
154
155 /* --- @buf_getbyte@ --- *
156  *
157  * Arguments:   @buf *b@ = pointer to a buffer block
158  *
159  * Returns:     A byte, or less than zero if there wasn't a byte there.
160  *
161  * Use:         Gets a single byte from a buffer.
162  */
163
164 int buf_getbyte(buf *b)
165 {
166   if (BENSURE(b, 1))
167     return (-1);
168   return (*b->p++);
169 }
170
171 /* --- @buf_putbyte@ --- *
172  *
173  * Arguments:   @buf *b@ = pointer to a buffer block
174  *              @int ch@ = byte to write
175  *
176  * Returns:     Zero if OK, nonzero if there wasn't enough space.
177  *
178  * Use:         Puts a single byte in a buffer.
179  */
180
181 int buf_putbyte(buf *b, int ch)
182 {
183   if (BENSURE(b, 1))
184     return (-1);
185   *b->p++ = ch;
186   return (0);
187 }
188
189 /* --- @buf_getu16@ --- *
190  *
191  * Arguments:   @buf *b@ = pointer to a buffer block
192  *              @uint16 *w@ = where to put the word
193  *
194  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
195  *
196  * Use:         Gets a 16-bit word from a buffer.
197  */
198
199 int buf_getu16(buf *b, uint16 *w)
200 {
201   if (BENSURE(b, 2))
202     return (-1);
203   *w = LOAD16(b->p);
204   BSTEP(b, 2);
205   return (0);
206 }
207
208 /* --- @buf_putu16@ --- *
209  *
210  * Arguments:   @buf *b@ = pointer to a buffer block
211  *              @uint16 w@ = word to write
212  *
213  * Returns:     Zero if OK, nonzero if there wasn't enough space.
214  *
215  * Use:         Puts a 16-but word in a buffer.
216  */
217
218 int buf_putu16(buf *b, uint16 w)
219 {
220   if (BENSURE(b, 2))
221     return (-1);
222   STORE16(b->p, w);
223   BSTEP(b, 2);
224   return (0);
225 }
226
227 /* --- @buf_getu32@ --- *
228  *
229  * Arguments:   @buf *b@ = pointer to a buffer block
230  *              @uint32 *w@ = where to put the word
231  *
232  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
233  *
234  * Use:         Gets a 32-bit word from a buffer.
235  */
236
237 int buf_getu32(buf *b, uint32 *w)
238 {
239   if (BENSURE(b, 4))
240     return (-1);
241   *w = LOAD32(b->p);
242   BSTEP(b, 4);
243   return (0);
244 }
245
246 /* --- @buf_putu32@ --- *
247  *
248  * Arguments:   @buf *b@ = pointer to a buffer block
249  *              @uint32 w@ = word to write
250  *
251  * Returns:     Zero if OK, nonzero if there wasn't enough space.
252  *
253  * Use:         Puts a 32-but word in a buffer.
254  */
255
256 int buf_putu32(buf *b, uint32 w)
257 {
258   if (BENSURE(b, 4))
259     return (-1);
260   STORE32(b->p, w);
261   BSTEP(b, 4);
262   return (0);
263 }
264
265 /* --- @buf_getmp@ --- *
266  *
267  * Arguments:   @buf *b@ = pointer to a buffer block
268  *
269  * Returns:     A multiprecision integer, or null if there wasn't one there.
270  *
271  * Use:         Gets a multiprecision integer from a buffer.
272  */
273
274 mp *buf_getmp(buf *b)
275 {
276   uint16 sz;
277   mp *m;
278   if (buf_getu16(b, &sz) || buf_ensure(b, sz))
279     return (0);
280   m = mp_loadb(MP_NEW, BCUR(b), sz);
281   if (mp_octets(m) != sz) {
282     mp_drop(m);
283     return (0);
284   }
285   BSTEP(b, sz);
286   return (m);
287 }
288
289 /* --- @buf_putmp@ --- *
290  *
291  * Arguments:   @buf *b@ = pointer to a buffer block
292  *              @mp *m@ = a multiprecision integer
293  *
294  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
295  *
296  * Use:         Puts a multiprecision integer to a buffer.
297  */
298
299 int buf_putmp(buf *b, mp *m)
300 {
301   size_t sz = mp_octets(m);
302   assert(sz < MASK16);
303   if (buf_putu16(b, sz) || buf_ensure(b, sz))
304     return (-1);
305   mp_storeb(m, BCUR(b), sz);
306   BSTEP(b, sz);
307   return (0);
308 }
309
310 /*----- That's all, folks -------------------------------------------------*/