chiark / gitweb /
Various minor changes. Check that MPs are in canonical form when
authormdw <mdw>
Fri, 16 Feb 2001 21:23:20 +0000 (21:23 +0000)
committermdw <mdw>
Fri, 16 Feb 2001 21:23:20 +0000 (21:23 +0000)
loading.

buf.c

diff --git a/buf.c b/buf.c
index 9f14cf62d767a3b51b92c54b113b4388484b1e71..f2d9f04d410965fd38599e7d258024795fdf663c 100644 (file)
--- 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
  *
 /*----- 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@ --- *