debian/changelog: Prepare for next minor version.
[catacomb] / symm / hash.h
index 31cf4a8..5b2e7fb 100644 (file)
 
 #include <mLib/bits.h>
 
+#ifndef CATACOMB_RSVR_H
+#  include "rsvr.h"
+#endif
+
 /*----- Macros ------------------------------------------------------------*/
 
 /* --- @HASH_BUFFER@ --- *
 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do {                    \
   pre##_ctx *_bctx = (ictx);                                           \
   size_t _bsz = (isz);                                                 \
-  const octet *_bbuf = (octet *)(ibuf);                                        \
+  const octet *_bbuf = (octet *)(ibuf), *_p;                           \
+  static const rsvr_policy _pol = { 0, PRE##_BUFSZ, PRE##_BUFSZ };     \
+  uint32 _l, _h;                                                       \
+  rsvr_state _st;                                                      \
                                                                        \
   /* --- Add on the size done so far --- *                             \
    *                                                                   \
    * how many bits you've actually got.                                        \
    */                                                                  \
                                                                        \
-  {                                                                    \
-    uint32 _l = U32(_bsz);                                             \
-    uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16;                                \
-    _bctx->nh += _h;                                                   \
-    _bctx->nl += _l;                                                   \
-    if (_bctx->nl < _l || _bctx->nl & ~MASK32)                         \
-      _bctx->nh++;                                                     \
-  }                                                                    \
-                                                                       \
-  /* --- Handle very small contributions --- */                                \
-                                                                       \
-  if (_bctx->off + _bsz < PRE##_BUFSZ) {                               \
-    memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz);                      \
-    _bctx->off += _bsz;                                                        \
-  } else {                                                             \
-                                                                       \
-    /* --- Handle an initial partial buffer --- */                     \
-                                                                       \
-    if (_bctx->off) {                                                  \
-      size_t s = PRE##_BUFSZ - _bctx->off;                             \
-      memcpy(_bctx->buf + _bctx->off, _bbuf, s);                       \
-      pre##_compress(_bctx, _bctx->buf);                               \
-      _bsz -= s; _bbuf += s;                                           \
-    }                                                                  \
-                                                                       \
-    /* --- Do whole buffers while we can --- */                                \
-                                                                       \
-    while (_bsz >= PRE##_BUFSZ) {                                      \
-      pre##_compress(_bctx, _bbuf);                                    \
-      _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ;                       \
-    }                                                                  \
+  _l = U32(_bsz); _h = ((_bsz & ~(size_t)MASK32) >> 16) >> 16;         \
+  _bctx->nl += _l; if (_bctx->nl < _l || _bctx->nl & ~(uint32)MASK32) _h++; \
+  _bctx->nh += _h;                                                     \
                                                                        \
-    /* --- And wrap up at the end --- */                               \
+  /* --- Accumulate the input data --- */                              \
                                                                        \
-    if (_bsz)                                                          \
-      memcpy(_bctx->buf, _bbuf, _bsz);                                 \
-    _bctx->off = _bsz;                                                 \
-  }                                                                    \
+  rsvr_setup(&_st, &_pol, _bctx->buf, &_bctx->off, _bbuf, _bsz);       \
+  RSVR_DO(&_st) while ((_p = RSVR_NEXT(&_st, PRE##_BUFSZ)) != 0)       \
+    pre##_compress(_bctx, _p);                                         \
 } while (0)
 
 /* --- @HASH_PAD@ --- *
 
 #ifdef TEST_RIG
 
+#include <mLib/macros.h>
 #include <mLib/quis.h>
 #include <mLib/testrig.h>
 
 #define HASH_BUFLEN 100000
 
-#define HASH_TEST(PRE, pre)                                            \
+#define HASH_VERIFY(PRE, pre) HASH_VERIFYX(PRE, pre, #pre)
+
+#define HASH_VERIFYX(PRE, pre, name)                                   \
                                                                        \
-static int verify(dstr *v)                                             \
+static int vrf_##pre(dstr *v, const test_type *msgty)                  \
 {                                                                      \
   pre##_ctx ctx;                                                       \
   int ok = 1;                                                          \
@@ -198,9 +179,10 @@ static int verify(dstr *v)                                         \
       sz -= i;                                                         \
     }                                                                  \
     pre##_done(&ctx, d.buf);                                           \
-    if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) {                  \
-      printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ",    \
-            *ip, v[0].buf);                                            \
+    if (MEMCMP(d.buf, !=, v[1].buf, PRE##_HASHSZ)) {                   \
+      printf("\nfail:\n\tstep = %i\n\tinput = ", *ip);                 \
+      msgty->dump(&v[0], stdout);                                      \
+      printf("\n\texpected = ");                                       \
       type_hex.dump(&v[1], stdout);                                    \
       fputs("\n\tcomputed = ", stdout);                                        \
       type_hex.dump(&d, stdout);                                       \
@@ -213,7 +195,12 @@ static int verify(dstr *v)                                         \
   return (ok);                                                         \
 }                                                                      \
                                                                        \
-static int verifyrep(dstr *v)                                          \
+static int vrf_##pre##_hex(dstr *v)                                    \
+  { return vrf_##pre(v, &type_hex); }                                  \
+static int vrf_##pre##_lit(dstr *v)                                    \
+  { return vrf_##pre(v, &type_string); }                               \
+                                                                       \
+static int vrf_##pre##_rep(dstr *v)                                    \
 {                                                                      \
   pre##_ctx ctx;                                                       \
   size_t len = v[0].len;                                               \
@@ -236,7 +223,7 @@ static int verifyrep(dstr *v)                                               \
   while (n--) pre##_hash(&ctx, p, len);                                        \
   pre##_done(&ctx, d.buf);                                             \
                                                                        \
-  if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) {                    \
+  if (MEMCMP(d.buf, !=, v[2].buf, PRE##_HASHSZ)) {                     \
     printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ",    \
             v[0].buf, *(int *)v[1].buf);                               \
     type_hex.dump(&v[2], stdout);                                      \
@@ -248,26 +235,38 @@ static int verifyrep(dstr *v)                                             \
   xfree(p);                                                            \
   dstr_destroy(&d);                                                    \
   return (ok);                                                         \
-}                                                                      \
+}
+
+#define HASH_TESTDEFS(PRE, pre) HASH_TESTDEFSX(PRE, pre, #pre)
+
+#define HASH_TESTDEFSX(PRE, pre, name)                                 \
+  { name, vrf_##pre##_lit, { &type_string, &type_hex, 0 } },           \
+  { name "-hex", vrf_##pre##_hex, { &type_hex, &type_hex, 0 } },       \
+  { name "-rep", vrf_##pre##_rep,                                      \
+    { &type_string, &type_int, &type_hex, 0 } },
+
+#define HASH_TESTX(PRE, pre, name, fname)                              \
+                                                                       \
+HASH_VERIFYX(PRE, pre, name)                                           \
                                                                        \
 static test_chunk defs[] = {                                           \
-  { #pre, verify, { &type_string, &type_hex, 0 } },                    \
-  { #pre "-rep", verifyrep,                                            \
-    { &type_string, &type_int, &type_hex, 0 } },                       \
+  HASH_TESTDEFSX(PRE, pre, name)                                       \
   { 0, 0, { 0 } }                                                      \
 };                                                                     \
                                                                        \
 int main(int argc, char *argv[])                                       \
 {                                                                      \
   ego(argv[0]);                                                                \
-  test_run(argc, argv, defs, SRCDIR"/t/" #pre);                                \
+  test_run(argc, argv, defs, SRCDIR"/t/" fname);                       \
   return (0);                                                          \
 }
 
 #else
-#  define HASH_TEST(PRE, pre)
+#  define HASH_TESTX(PRE, pre, name, fname)
 #endif
 
+#define HASH_TEST(PRE, pre) HASH_TESTX(PRE, pre, #pre, #pre)
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus