X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/cfc354fd52d515a886cd6556173a0c4d9dab915e..00e64b67d321bf53224d546dcba58c52834a7b79:/buf.c diff --git a/buf.c b/buf.c index f2d9f04d..dfa62051 100644 --- a/buf.c +++ b/buf.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: buf.c,v 1.2 2001/02/16 21:23:20 mdw Exp $ + * $Id: buf.c,v 1.4 2001/06/19 22:09:54 mdw Exp $ * * Buffer handling * @@ -29,6 +29,12 @@ /*----- Revision history --------------------------------------------------* * * $Log: buf.c,v $ + * Revision 1.4 2001/06/19 22:09:54 mdw + * Expose interface, for use in the proxy. + * + * Revision 1.3 2001/03/03 12:06:48 mdw + * Use 16-bit lengths on MPs, since there's a packet limit of 64K anyway. + * * Revision 1.2 2001/02/16 21:23:20 mdw * Various minor changes. Check that MPs are in canonical form when * loading. @@ -40,7 +46,11 @@ /*----- Header files ------------------------------------------------------*/ -#include "tripe.h" +#include + +#include + +#include "buf.h" /*----- Main code ---------------------------------------------------------*/ @@ -176,7 +186,45 @@ int buf_putbyte(buf *b, int ch) return (0); } -/* --- @buf_getword@ --- * +/* --- @buf_getu16@ --- * + * + * Arguments: @buf *b@ = pointer to a buffer block + * @uint16 *w@ = where to put the word + * + * Returns: Zero if OK, or nonzero if there wasn't a word there. + * + * Use: Gets a 16-bit word from a buffer. + */ + +int buf_getu16(buf *b, uint16 *w) +{ + if (BENSURE(b, 2)) + return (-1); + *w = LOAD16(b->p); + BSTEP(b, 2); + return (0); +} + +/* --- @buf_putu16@ --- * + * + * Arguments: @buf *b@ = pointer to a buffer block + * @uint16 w@ = word to write + * + * Returns: Zero if OK, nonzero if there wasn't enough space. + * + * Use: Puts a 16-but word in a buffer. + */ + +int buf_putu16(buf *b, uint16 w) +{ + if (BENSURE(b, 2)) + return (-1); + STORE16(b->p, w); + BSTEP(b, 2); + return (0); +} + +/* --- @buf_getu32@ --- * * * Arguments: @buf *b@ = pointer to a buffer block * @uint32 *w@ = where to put the word @@ -186,7 +234,7 @@ int buf_putbyte(buf *b, int ch) * Use: Gets a 32-bit word from a buffer. */ -int buf_getword(buf *b, uint32 *w) +int buf_getu32(buf *b, uint32 *w) { if (BENSURE(b, 4)) return (-1); @@ -195,7 +243,7 @@ int buf_getword(buf *b, uint32 *w) return (0); } -/* --- @buf_putword@ --- * +/* --- @buf_putu32@ --- * * * Arguments: @buf *b@ = pointer to a buffer block * @uint32 w@ = word to write @@ -205,7 +253,7 @@ int buf_getword(buf *b, uint32 *w) * Use: Puts a 32-but word in a buffer. */ -int buf_putword(buf *b, uint32 w) +int buf_putu32(buf *b, uint32 w) { if (BENSURE(b, 4)) return (-1); @@ -225,9 +273,9 @@ int buf_putword(buf *b, uint32 w) mp *buf_getmp(buf *b) { - uint32 sz; + uint16 sz; mp *m; - if (buf_getword(b, &sz) || buf_ensure(b, sz)) + if (buf_getu16(b, &sz) || buf_ensure(b, sz)) return (0); m = mp_loadb(MP_NEW, BCUR(b), sz); if (mp_octets(m) != sz) { @@ -251,7 +299,8 @@ mp *buf_getmp(buf *b) int buf_putmp(buf *b, mp *m) { size_t sz = mp_octets(m); - if (buf_putword(b, sz) || buf_ensure(b, sz)) + assert(sz < MASK16); + if (buf_putu16(b, sz) || buf_ensure(b, sz)) return (-1); mp_storeb(m, BCUR(b), sz); BSTEP(b, sz);