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 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @buf_init@ --- *
42 * Arguments: @buf *b@ = pointer to a buffer block
43 * @void *p@ = pointer to a buffer
44 * @size_t sz@ = size of the buffer
48 * Use: Initializes the buffer block appropriately.
51 void buf_init(buf *b, void *p, size_t sz)
58 /* --- @buf_break@ --- *
60 * Arguments: @buf *b@ = pointer to a buffer block
62 * Returns: Some negative value.
64 * Use: Marks a buffer as broken.
67 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
69 /* --- @buf_flip@ --- *
71 * Arguments: @buf *b@ = pointer to a buffer block
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.
85 /* --- @buf_ensure@ --- *
87 * Arguments: @buf *b@ = pointer to a buffer block
88 * @size_t sz@ = size of data wanted
90 * Returns: Zero if it worked, nonzero if there wasn't enough space.
92 * Use: Ensures that there are @sz@ bytes still in the buffer.
95 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
97 /* --- @buf_get@ --- *
99 * Arguments: @buf *b@ = pointer to a buffer block
100 * @size_t sz@ = size of the buffer
102 * Returns: Pointer to the place in the buffer.
104 * Use: Reserves a space in the buffer of the requested size, and
105 * returns its start address.
108 void *buf_get(buf *b, size_t sz)
118 /* --- @buf_put@ --- *
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
124 * Returns: Zero if it worked, nonzero if there wasn't enough space.
126 * Use: Fetches data from some place and puts it in the buffer
129 int buf_put(buf *b, const void *p, size_t sz)
133 memcpy(BCUR(b), p, sz);
138 /* --- @buf_getbyte@ --- *
140 * Arguments: @buf *b@ = pointer to a buffer block
142 * Returns: A byte, or less than zero if there wasn't a byte there.
144 * Use: Gets a single byte from a buffer.
147 int buf_getbyte(buf *b)
154 /* --- @buf_putbyte@ --- *
156 * Arguments: @buf *b@ = pointer to a buffer block
157 * @int ch@ = byte to write
159 * Returns: Zero if OK, nonzero if there wasn't enough space.
161 * Use: Puts a single byte in a buffer.
164 int buf_putbyte(buf *b, int ch)
172 /* --- @buf_getu16@ --- *
174 * Arguments: @buf *b@ = pointer to a buffer block
175 * @uint16 *w@ = where to put the word
177 * Returns: Zero if OK, or nonzero if there wasn't a word there.
179 * Use: Gets a 16-bit word from a buffer.
182 int buf_getu16(buf *b, uint16 *w)
191 /* --- @buf_putu16@ --- *
193 * Arguments: @buf *b@ = pointer to a buffer block
194 * @uint16 w@ = word to write
196 * Returns: Zero if OK, nonzero if there wasn't enough space.
198 * Use: Puts a 16-but word in a buffer.
201 int buf_putu16(buf *b, uint16 w)
210 /* --- @buf_getu32@ --- *
212 * Arguments: @buf *b@ = pointer to a buffer block
213 * @uint32 *w@ = where to put the word
215 * Returns: Zero if OK, or nonzero if there wasn't a word there.
217 * Use: Gets a 32-bit word from a buffer.
220 int buf_getu32(buf *b, uint32 *w)
229 /* --- @buf_putu32@ --- *
231 * Arguments: @buf *b@ = pointer to a buffer block
232 * @uint32 w@ = word to write
234 * Returns: Zero if OK, nonzero if there wasn't enough space.
236 * Use: Puts a 32-but word in a buffer.
239 int buf_putu32(buf *b, uint32 w)
250 * Arguments: @buf *b@ = pointer to a buffer block
251 * @size_t *nn@ = where to put the length
253 * Returns: Zero if OK, nonzero if there wasn't a null byte to be found.
255 * Use: Finds a terminating null byte.
258 static int findz(buf *b, size_t *nn)
262 if ((p = memchr(BCUR(b), 0, BLEN(b))) == 0) {
270 #define DOSUFFIXES(DO) DO(8) DO(16) DO(32) DO(z)
272 /* --- @buf_getmem{8,16,32,z} --- *
274 * Arguments: @buf *b@ = pointer to a buffer block
275 * @size_t *nn@ = where to put the length
277 * Returns: Pointer to the buffer data, or null.
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.
283 void *buf_getmem8(buf *b, size_t *nn)
287 if ((n = buf_getbyte(b)) < 0) return (0);
289 return (buf_get(b, n));
292 void *buf_getmem16(buf *b, size_t *nn)
296 if (buf_getu16(b, &n)) return (0);
298 return (buf_get(b, n));
301 void *buf_getmem32(buf *b, size_t *nn)
305 if (buf_getu32(b, &n)) return (0);
307 return (buf_get(b, n));
310 void *buf_getmemz(buf *b, size_t *nn)
312 if (findz(b, nn)) return (0);
313 return (buf_get(b, *nn));
316 /* --- @buf_putmem{8,16,32,z} --- *
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
322 * Returns: Zero if OK, nonzero if there wasn't enough space.
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.
328 int buf_putmem8(buf *b, const void *p, size_t n)
331 if (buf_putbyte(b, n) || buf_put(b, p, n))
336 int buf_putmem16(buf *b, const void *p, size_t n)
338 assert(n <= 0xfffful);
339 if (buf_putu16(b, n) || buf_put(b, p, n))
344 int buf_putmem32(buf *b, const void *p, size_t n)
346 assert(n <= 0xfffffffful);
347 if (buf_putu32(b, n) || buf_put(b, p, n))
352 int buf_putmemz(buf *b, const void *p, size_t n)
356 assert(!memchr(p, 0, n));
357 if ((q = buf_get(b, n + 1)) == 0)
364 /* --- @buf_getbuf{8,16,32,z}@ --- *
366 * Arguments: @buf *b@ = pointer to a buffer block
367 * @buf *bb@ = where to put the result
369 * Returns: Zero if it worked, nonzero if there wasn't enough space.
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.
376 #define GETBUF(suff) int buf_getbuf##suff(buf *b, buf *bb) \
381 if ((p = buf_getmem##suff(b, &n)) == 0) \
383 buf_init(bb, p, n); \
388 /* --- @buf_putbuf{8,16,32,z}@ --- *
390 * Arguments: @buf *b@ = pointer to a buffer block
391 * @buf *bb@ = buffer to write
393 * Returns: Zero if it worked, nonzero if there wasn't enough space.
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-
400 #define PUTBUF(suff) int buf_putbuf##suff(buf *b, buf *bb) \
401 { return (buf_putmem##suff(b, BBASE(bb), BLEN(bb))); }
404 /* --- @buf_getstr{8,16,32,z}@ --- *
406 * Arguments: @buf *b@ = pointer to a buffer block
407 * @dstr *d@ = where to put the result
409 * Returns: Zero if it worked, nonzero if there wasn't enough space.
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.
416 #define GETSTR(suff) int buf_getstr##suff(buf *b, dstr *d) \
421 if ((p = buf_getmem##suff(b, &n)) == 0) \
428 /* --- @buf_putstr{8,16,32,z}@ --- *
430 * Arguments: @buf *b@ = pointer to a buffer block
431 * @dstr *d@ = string to write
433 * Returns: Zero if it worked, nonzero if there wasn't enough space.
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.
440 #define PUTSTR(suff) int buf_putstr##suff(buf *b, dstr *d) \
441 { return (buf_putmem##suff(b, d->buf, d->len)); }
444 /* --- @buf_getmp@ --- *
446 * Arguments: @buf *b@ = pointer to a buffer block
448 * Returns: A multiprecision integer, or null if there wasn't one there.
450 * Use: Gets a multiprecision integer from a buffer.
453 mp *buf_getmp(buf *b)
458 if (buf_getu16(b, &sz) || buf_ensure(b, sz))
460 m = mp_loadb(MP_NEW, BCUR(b), sz);
462 if (n != sz && n != 0 && sz != 1) {
470 /* --- @buf_putmp@ --- *
472 * Arguments: @buf *b@ = pointer to a buffer block
473 * @mp *m@ = a multiprecision integer
475 * Returns: Zero if it worked, nonzero if there wasn't enough space.
477 * Use: Puts a multiprecision integer to a buffer.
480 int buf_putmp(buf *b, mp *m)
482 size_t sz = mp_octets(m);
485 if (buf_putu16(b, sz) || buf_ensure(b, sz))
487 mp_storeb(m, BCUR(b), sz);
492 /* --- @buf_getec@ --- *
494 * Arguments: @buf *b@ = pointer to a buffer block
495 * @ec *p@ = where to put the point
497 * Returns: Zero if it worked, nonzero if it failed.
499 * Use: Gets a multiprecision integer from a buffer. The point must
503 int buf_getec(buf *b, ec *p)
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);
512 EC_DESTROY(p); p->x = x; p->y = y; p->z = 0;
516 /* --- @buf_putec@ --- *
518 * Arguments: @buf *b@ = pointer to a buffer block
519 * @ec *p@ = an elliptic curve point
521 * Returns: Zero if it worked, nonzero if there wasn't enough space.
523 * Use: Puts an elliptic curve point to a buffer.
526 int buf_putec(buf *b, ec *p)
528 if (EC_ATINF(p)) return (buf_putu16(b, 0));
529 if (buf_putmp(b, p->x) || buf_putmp(b, p->y)) return (-1);
533 /*----- That's all, folks -------------------------------------------------*/