Merge branch '2.5.x'
[catacomb] / symm / hash.h
index eb3cd75..3d0c117 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 & ~(size_t)MASK32) >> 16) >> 16;                        \
-    _bctx->nh += _h;                                                   \
-    _bctx->nl += _l;                                                   \
-    if (_bctx->nl < _l || _bctx->nl & ~(uint32)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 --- */                     \
+  _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;                                                     \
                                                                        \
-    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;                       \
-    }                                                                  \
-                                                                       \
-    /* --- 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@ --- *