chiark / gitweb /
e34d8765edeb22aa84edfe816aea37131ec84f15
[mLib] / buf.c
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * Buffer handling
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 /*----- Header files ------------------------------------------------------*/
31
32 #include <string.h>
33
34 #include "mp.h"
35 #include "ec.h"
36 #include "buf.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @buf_init@ --- *
41  *
42  * Arguments:   @buf *b@ = pointer to a buffer block
43  *              @void *p@ = pointer to a buffer
44  *              @size_t sz@ = size of the buffer
45  *
46  * Returns:     ---
47  *
48  * Use:         Initializes the buffer block appropriately.
49  */
50
51 void buf_init(buf *b, void *p, size_t sz)
52 {
53   b->base = b->p = p;
54   b->limit = b->p + sz;
55   b->f = 0;
56 }
57
58 /* --- @buf_break@ --- *
59  *
60  * Arguments:   @buf *b@ = pointer to a buffer block
61  *
62  * Returns:     Some negative value.
63  *
64  * Use:         Marks a buffer as broken.
65  */
66
67 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
68
69 /* --- @buf_flip@ --- *
70  *
71  * Arguments:   @buf *b@ = pointer to a buffer block
72  *
73  * Returns:     ---
74  *
75  * Use:         Flips a buffer so that if you've just been writing to it,
76  *              you can now read from the bit you've written.
77  */
78
79 void buf_flip(buf *b)
80 {
81   b->limit = b->p;
82   b->p = b->base;
83 }
84
85 /* --- @buf_ensure@ --- *
86  *
87  * Arguments:   @buf *b@ = pointer to a buffer block
88  *              @size_t sz@ = size of data wanted
89  *
90  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
91  *
92  * Use:         Ensures that there are @sz@ bytes still in the buffer.
93  */
94
95 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
96
97 /* --- @buf_get@ --- *
98  *
99  * Arguments:   @buf *b@ = pointer to a buffer block
100  *              @size_t sz@ = size of the buffer
101  *
102  * Returns:     Pointer to the place in the buffer.
103  *
104  * Use:         Reserves a space in the buffer of the requested size, and
105  *              returns its start address.
106  */
107
108 void *buf_get(buf *b, size_t sz)
109 {
110   void *p;
111   if (BENSURE(b, sz))
112     return (0);
113   p = BCUR(b);
114   BSTEP(b, sz);
115   return (p);
116 }
117
118 /* --- @buf_put@ --- *
119  *
120  * Arguments:   @buf *b@ = pointer to a buffer block
121  *              @const void *p@ = pointer to a buffer
122  *              @size_t sz@ = size of the buffer
123  *
124  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
125  *
126  * Use:         Fetches data from some place and puts it in the buffer
127  */
128
129 int buf_put(buf *b, const void *p, size_t sz)
130 {
131   if (BENSURE(b, sz))
132     return (-1);
133   memcpy(BCUR(b), p, sz);
134   BSTEP(b, sz);
135   return (0);
136 }
137
138 /* --- @buf_getbyte@ --- *
139  *
140  * Arguments:   @buf *b@ = pointer to a buffer block
141  *
142  * Returns:     A byte, or less than zero if there wasn't a byte there.
143  *
144  * Use:         Gets a single byte from a buffer.
145  */
146
147 int buf_getbyte(buf *b)
148 {
149   if (BENSURE(b, 1))
150     return (-1);
151   return (*b->p++);
152 }
153
154 /* --- @buf_putbyte@ --- *
155  *
156  * Arguments:   @buf *b@ = pointer to a buffer block
157  *              @int ch@ = byte to write
158  *
159  * Returns:     Zero if OK, nonzero if there wasn't enough space.
160  *
161  * Use:         Puts a single byte in a buffer.
162  */
163
164 int buf_putbyte(buf *b, int ch)
165 {
166   if (BENSURE(b, 1))
167     return (-1);
168   *b->p++ = ch;
169   return (0);
170 }
171
172 /* --- @buf_getu16@ --- *
173  *
174  * Arguments:   @buf *b@ = pointer to a buffer block
175  *              @uint16 *w@ = where to put the word
176  *
177  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
178  *
179  * Use:         Gets a 16-bit word from a buffer.
180  */
181
182 int buf_getu16(buf *b, uint16 *w)
183 {
184   if (BENSURE(b, 2))
185     return (-1);
186   *w = LOAD16(b->p);
187   BSTEP(b, 2);
188   return (0);
189 }
190
191 /* --- @buf_putu16@ --- *
192  *
193  * Arguments:   @buf *b@ = pointer to a buffer block
194  *              @uint16 w@ = word to write
195  *
196  * Returns:     Zero if OK, nonzero if there wasn't enough space.
197  *
198  * Use:         Puts a 16-but word in a buffer.
199  */
200
201 int buf_putu16(buf *b, uint16 w)
202 {
203   if (BENSURE(b, 2))
204     return (-1);
205   STORE16(b->p, w);
206   BSTEP(b, 2);
207   return (0);
208 }
209
210 /* --- @buf_getu32@ --- *
211  *
212  * Arguments:   @buf *b@ = pointer to a buffer block
213  *              @uint32 *w@ = where to put the word
214  *
215  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
216  *
217  * Use:         Gets a 32-bit word from a buffer.
218  */
219
220 int buf_getu32(buf *b, uint32 *w)
221 {
222   if (BENSURE(b, 4))
223     return (-1);
224   *w = LOAD32(b->p);
225   BSTEP(b, 4);
226   return (0);
227 }
228
229 /* --- @buf_putu32@ --- *
230  *
231  * Arguments:   @buf *b@ = pointer to a buffer block
232  *              @uint32 w@ = word to write
233  *
234  * Returns:     Zero if OK, nonzero if there wasn't enough space.
235  *
236  * Use:         Puts a 32-but word in a buffer.
237  */
238
239 int buf_putu32(buf *b, uint32 w)
240 {
241   if (BENSURE(b, 4))
242     return (-1);
243   STORE32(b->p, w);
244   BSTEP(b, 4);
245   return (0);
246 }
247
248 /* --- @findz@ --- *
249  *
250  * Arguments:   @buf *b@ = pointer to a buffer block
251  *              @size_t *nn@ = where to put the length
252  *
253  * Returns:     Zero if OK, nonzero if there wasn't a null byte to be found.
254  *
255  * Use:         Finds a terminating null byte.
256  */
257
258 static int findz(buf *b, size_t *nn)
259 {
260   octet *p;
261
262   if ((p = memchr(BCUR(b), 0, BLEN(b))) == 0) {
263     buf_break(b);
264     return (-1);
265   }
266   *nn = p - BCUR(b);
267   return (0);
268 }
269
270 #define DOSUFFIXES(DO) DO(8) DO(16) DO(32) DO(z)
271
272 /* --- @buf_getmem{8,16,32,z} --- *
273  *
274  * Arguments:   @buf *b@ = pointer to a buffer block
275  *              @size_t *nn@ = where to put the length
276  *
277  * Returns:     Pointer to the buffer data, or null.
278  *
279  * Use:         Gets a chunk of memory from a buffer.  The number, @16@ or
280  *              @32@, is the width of the length; @z@ means null-terminated.
281  */
282
283 void *buf_getmem8(buf *b, size_t *nn)
284 {
285   int n;
286
287   if ((n = buf_getbyte(b)) < 0) return (0);
288   *nn = n;
289   return (buf_get(b, n));
290 }
291
292 void *buf_getmem16(buf *b, size_t *nn)
293 {
294   uint16 n;
295
296   if (buf_getu16(b, &n)) return (0);
297   *nn = n;
298   return (buf_get(b, n));
299 }
300
301 void *buf_getmem32(buf *b, size_t *nn)
302 {
303   uint32 n;
304
305   if (buf_getu32(b, &n)) return (0);
306   *nn = n;
307   return (buf_get(b, n));
308 }
309   
310 void *buf_getmemz(buf *b, size_t *nn)
311 {
312   if (findz(b, nn)) return (0);
313   return (buf_get(b, *nn));
314 }
315
316 /* --- @buf_putmem{8,16,32,z} --- *
317  *
318  * Arguments:   @buf *b@ = pointer to a buffer block
319  *              @const void *p@ = pointer to data to write
320  *              @size_t n@ = length to write
321  *
322  * Returns:     Zero if OK, nonzero if there wasn't enough space.
323  *
324  * Use:         Writes a chunk of data to a buffer.  The number, @16@ or
325  *              @32@, is the width of the length; @z@ means null-terminated.
326  */
327
328 int buf_putmem8(buf *b, const void *p, size_t n)
329 {
330   assert(n <= 0xfful);
331   if (buf_putbyte(b, n) || buf_put(b, p, n))
332     return (-1);
333   return (0);
334 }
335
336 int buf_putmem16(buf *b, const void *p, size_t n)
337 {
338   assert(n <= 0xfffful);
339   if (buf_putu16(b, n) || buf_put(b, p, n))
340     return (-1);
341   return (0);
342 }
343
344 int buf_putmem32(buf *b, const void *p, size_t n)
345 {
346   assert(n <= 0xfffffffful);
347   if (buf_putu32(b, n) || buf_put(b, p, n))
348     return (-1);
349   return (0);
350 }
351
352 int buf_putmemz(buf *b, const void *p, size_t n)
353 {
354   octet *q;
355
356   assert(!memchr(p, 0, n));
357   if ((q = buf_get(b, n + 1)) == 0)
358     return (-1);
359   memcpy(q, p, n);
360   q[n] = 0;
361   return (0);
362 }
363
364 /* --- @buf_getbuf{8,16,32,z}@ --- *
365  *
366  * Arguments:   @buf *b@ = pointer to a buffer block
367  *              @buf *bb@ = where to put the result
368  *
369  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
370  *
371  * Use:         Gets a block of data from a buffer, and writes its bounds to
372  *              another buffer.  The number, @16@ or @32@, is the width of
373  *              the length; @z@ means null-terminated.
374  */
375
376 #define GETBUF(suff) int buf_getbuf##suff(buf *b, buf *bb)              \
377 {                                                                       \
378   void *p;                                                              \
379   size_t n;                                                             \
380                                                                         \
381   if ((p = buf_getmem##suff(b, &n)) == 0)                               \
382     return (-1);                                                        \
383   buf_init(bb, p, n);                                                   \
384   return (0);                                                           \
385 }
386 DOSUFFIXES(GETBUF)
387
388 /* --- @buf_putbuf{8,16,32,z}@ --- *
389  *
390  * Arguments:   @buf *b@ = pointer to a buffer block
391  *              @buf *bb@ = buffer to write
392  *
393  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
394  *
395  * Use:         Puts the contents of a buffer to a buffer.  The number, @16@
396  *              or @32@, is the width of the length; @z@ means null-
397  *              terminated.
398  */
399
400 #define PUTBUF(suff) int buf_putbuf##suff(buf *b, buf *bb)              \
401   { return (buf_putmem##suff(b, BBASE(bb), BLEN(bb))); }
402 DOSUFFIXES(PUTBUF)  
403
404 /* --- @buf_getstr{8,16,32,z}@ --- *
405  *
406  * Arguments:   @buf *b@ = pointer to a buffer block
407  *              @dstr *d@ = where to put the result
408  *
409  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
410  *
411  * Use:         Gets a block of data from a buffer, and writes its contents
412  *              to a string.  The number, @16@ or @32@, is the width of the
413  *              length; @z@ means null-terminated.
414  */
415
416 #define GETSTR(suff) int buf_getstr##suff(buf *b, dstr *d)              \
417 {                                                                       \
418   void *p;                                                              \
419   size_t n;                                                             \
420                                                                         \
421   if ((p = buf_getmem##suff(b, &n)) == 0)                               \
422     return (-1);                                                        \
423   DPUTM(d, p, n);                                                       \
424   return (0);                                                           \
425 }
426 DOSUFFIXES(GETSTR)
427
428 /* --- @buf_putstr{8,16,32,z}@ --- *
429  *
430  * Arguments:   @buf *b@ = pointer to a buffer block
431  *              @dstr *d@ = string to write
432  *
433  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
434  *
435  * Use:         Puts a string to a buffer, and writes its bounds to
436  *              another buffer.  The number, @16@ or @32@, is the width of
437  *              the length; @z@ means null-terminated.
438  */
439
440 #define PUTSTR(suff) int buf_putstr##suff(buf *b, dstr *d)              \
441   { return (buf_putmem##suff(b, d->buf, d->len)); }
442 DOSUFFIXES(PUTSTR)
443
444 /* --- @buf_getmp@ --- *
445  *
446  * Arguments:   @buf *b@ = pointer to a buffer block
447  *
448  * Returns:     A multiprecision integer, or null if there wasn't one there.
449  *
450  * Use:         Gets a multiprecision integer from a buffer.
451  */
452
453 mp *buf_getmp(buf *b)
454 {
455   uint16 sz;
456   size_t n;
457   mp *m;
458   if (buf_getu16(b, &sz) || buf_ensure(b, sz))
459     return (0);
460   m = mp_loadb(MP_NEW, BCUR(b), sz);
461   n = mp_octets(m);
462   if (n != sz && n != 0 && sz != 1) {
463     mp_drop(m);
464     return (0);
465   }
466   BSTEP(b, sz);
467   return (m);
468 }
469
470 /* --- @buf_putmp@ --- *
471  *
472  * Arguments:   @buf *b@ = pointer to a buffer block
473  *              @mp *m@ = a multiprecision integer
474  *
475  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
476  *
477  * Use:         Puts a multiprecision integer to a buffer.
478  */
479
480 int buf_putmp(buf *b, mp *m)
481 {
482   size_t sz = mp_octets(m);
483   assert(sz < MASK16);
484   if (!sz) sz = 1;
485   if (buf_putu16(b, sz) || buf_ensure(b, sz))
486     return (-1);
487   mp_storeb(m, BCUR(b), sz);
488   BSTEP(b, sz);
489   return (0);
490 }
491
492 /* --- @buf_getec@ --- *
493  *
494  * Arguments:   @buf *b@ = pointer to a buffer block
495  *              @ec *p@ = where to put the point
496  *
497  * Returns:     Zero if it worked, nonzero if it failed.
498  *
499  * Use:         Gets a multiprecision integer from a buffer.  The point must
500  *              be initialized.
501  */
502
503 int buf_getec(buf *b, ec *p)
504 {
505   mp *x = 0, *y = 0;
506   uint16 n;
507   if (buf_ensure(b, 2)) return (-1);
508   n = LOAD16(BCUR(b)); if (!n) { BSTEP(b, 2); EC_SETINF(p); return (0); }
509   if ((x = buf_getmp(b)) == 0 || (y = buf_getmp(b)) == 0) {
510     mp_drop(x); mp_drop(y); return (-1);
511   }
512   EC_DESTROY(p); p->x = x; p->y = y; p->z = 0;
513   return (0);
514 }
515
516 /* --- @buf_putec@ --- *
517  *
518  * Arguments:   @buf *b@ = pointer to a buffer block
519  *              @ec *p@ = an elliptic curve point
520  *
521  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
522  *
523  * Use:         Puts an elliptic curve point to a buffer.
524  */
525
526 int buf_putec(buf *b, ec *p)
527 {
528   if (EC_ATINF(p)) return (buf_putu16(b, 0));
529   if (buf_putmp(b, p->x) || buf_putmp(b, p->y)) return (-1);
530   return (0);
531 }
532
533 /*----- That's all, folks -------------------------------------------------*/