From: mdw Date: Fri, 16 Feb 2001 21:23:20 +0000 (+0000) Subject: Various minor changes. Check that MPs are in canonical form when X-Git-Tag: 1.0.0pre1~28 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/commitdiff_plain/cfc354fd52d515a886cd6556173a0c4d9dab915e Various minor changes. Check that MPs are in canonical form when loading. --- diff --git a/buf.c b/buf.c index 9f14cf62..f2d9f04d 100644 --- a/buf.c +++ b/buf.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: buf.c,v 1.1 2001/02/03 20:26:37 mdw Exp $ + * $Id: buf.c,v 1.2 2001/02/16 21:23:20 mdw Exp $ * * Buffer handling * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: buf.c,v $ + * Revision 1.2 2001/02/16 21:23:20 mdw + * Various minor changes. Check that MPs are in canonical form when + * loading. + * * Revision 1.1 2001/02/03 20:26:37 mdw * Initial checkin. * @@ -69,6 +73,22 @@ void buf_init(buf *b, void *p, size_t sz) int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); } +/* --- @buf_flip@ --- * + * + * Arguments: @buf *b@ = pointer to a buffer block + * + * Returns: --- + * + * Use: Flips a buffer so that if you've just been writing to it, + * you can now read from the bit you've written. + */ + +void buf_flip(buf *b) +{ + b->limit = b->p; + b->p = b->base; +} + /* --- @buf_ensure@ --- * * * Arguments: @buf *b@ = pointer to a buffer block @@ -84,21 +104,22 @@ int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); } /* --- @buf_get@ --- * * * Arguments: @buf *b@ = pointer to a buffer block - * @void *p@ = pointer to a buffer * @size_t sz@ = size of the buffer * - * Returns: Zero if it worked, nonzero if there wasn't enough data. + * Returns: Pointer to the place in the buffer. * - * Use: Fetches data from the buffer into some other place. + * Use: Reserves a space in the buffer of the requested size, and + * returns its start address. */ -int buf_get(buf *b, void *p, size_t sz) +void *buf_get(buf *b, size_t sz) { + void *p; if (BENSURE(b, sz)) - return (-1); - memcpy(p, BCUR(b), sz); + return (0); + p = BCUR(b); BSTEP(b, sz); - return (0); + return (p); } /* --- @buf_put@ --- * @@ -202,14 +223,19 @@ int buf_putword(buf *b, uint32 w) * Use: Gets a multiprecision integer from a buffer. */ -mp *buf_getmp(buf *b, mp *d) +mp *buf_getmp(buf *b) { uint32 sz; + mp *m; if (buf_getword(b, &sz) || buf_ensure(b, sz)) return (0); - d = mp_loadb(d, BCUR(b), sz); + m = mp_loadb(MP_NEW, BCUR(b), sz); + if (mp_octets(m) != sz) { + mp_drop(m); + return (0); + } BSTEP(b, sz); - return (d); + return (m); } /* --- @buf_putmp@ --- *