symm/...: Reformat encryption mode loops and related code.
[catacomb] / symm / hash.h
index dc0bbaa..420277d 100644 (file)
@@ -59,6 +59,8 @@
   pre##_ctx *_bctx = (ictx);                                           \
   size_t _bsz = (isz);                                                 \
   const octet *_bbuf = (octet *)(ibuf);                                        \
+  size_t _s;                                                           \
+  uint32 _l, _h;                                                       \
                                                                        \
   /* --- 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++;                                                     \
-  }                                                                    \
+  _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;                                                     \
                                                                        \
   /* --- Handle very small contributions --- */                                \
                                                                        \
-  if (_bctx->off + _bsz < PRE##_BUFSZ) {                               \
-    memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz);                      \
-    _bctx->off += _bsz;                                                        \
-  } else {                                                             \
+  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);                       \
+      _s = PRE##_BUFSZ - _bctx->off;                                   \
+      memcpy(_bctx->buf + _bctx->off, _bbuf, _s);                      \
       pre##_compress(_bctx, _bctx->buf);                               \
-      _bsz -= s; _bbuf += s;                                           \
+      _bsz -= _s; _bbuf += _s;                                         \
     }                                                                  \
                                                                        \
     /* --- Do whole buffers while we can --- */                                \
                                                                        \
     /* --- And wrap up at the end --- */                               \
                                                                        \
-    if (_bsz)                                                          \
-      memcpy(_bctx->buf, _bbuf, _bsz);                                 \
+    if (_bsz) memcpy(_bctx->buf, _bbuf, _bsz);                         \
     _bctx->off = _bsz;                                                 \
   }                                                                    \
 } while (0)
 
 #define HASH_VERIFYX(PRE, pre, name)                                   \
                                                                        \
-static int vrf_##pre(dstr *v)                                          \
+static int vrf_##pre(dstr *v, const test_type *msgty)                  \
 {                                                                      \
   pre##_ctx ctx;                                                       \
   int ok = 1;                                                          \
@@ -201,8 +196,9 @@ static int vrf_##pre(dstr *v)                                               \
     }                                                                  \
     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);                                            \
+      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);                                       \
@@ -215,6 +211,11 @@ static int vrf_##pre(dstr *v)                                              \
   return (ok);                                                         \
 }                                                                      \
                                                                        \
+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;                                                       \
@@ -255,7 +256,8 @@ static int vrf_##pre##_rep(dstr *v)                                 \
 #define HASH_TESTDEFS(PRE, pre) HASH_TESTDEFSX(PRE, pre, #pre)
 
 #define HASH_TESTDEFSX(PRE, pre, name)                                 \
-  { name, vrf_##pre, { &type_string, &type_hex, 0 } },                 \
+  { 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 } },