Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / ghash.h
diff --git a/ghash.h b/ghash.h
index 9d63419..d20aff4 100644 (file)
--- a/ghash.h
+++ b/ghash.h
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: ghash.h,v 1.6 2004/04/04 19:42:30 mdw Exp $
+ * $Id$
  *
  * Generic hash function interface
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: ghash.h,v $
- * Revision 1.6  2004/04/04 19:42:30  mdw
- * Make tables of standard encryption schemes etc.
- *
- * Revision 1.5  2000/07/15 10:00:58  mdw
- * New generic hash operation for copying hash contexts.
- *
- * Revision 1.4  2000/07/03 18:08:24  mdw
- * Include `bits.h'.
- *
- * Revision 1.3  2000/07/02 18:27:42  mdw
- * (ghash->ops->done): Interface change.  Passing in a null buffer pointer
- * uses a buffer internal to the ghash object.  The operation returns the
- * address of the buffer it used.  Clients of generic hashes no longer need
- * to use dynamically allocated memory for hash results.
- *
- * Revision 1.2  2000/06/17 11:22:17  mdw
- * Minor changes in the generic hash interface.
- *
- * Revision 1.1  1999/12/10 23:16:01  mdw
- * Generic interface.
- *
- */
-
 #ifndef CATACOMB_GHASH_H
 #define CATACOMB_GHASH_H
 
@@ -81,12 +55,78 @@ typedef struct ghash_ops {
 } ghash_ops;
 
 #define GH_INIT(ch)            (ch)->init()
-#define GH_CLASS(H)            (h)->ops->c
+#define GH_CLASS(h)            (h)->ops->c
 #define GH_HASH(h, p, sz)      (h)->ops->hash((h), (p), (sz))
 #define GH_DONE(h, buf)                (h)->ops->done((h), (buf))
 #define GH_DESTROY(h)          (h)->ops->destroy((h))
 #define GH_COPY(h)             (h)->ops->copy((h))
 
+#define GH_HASHU_(h, n, W)  do {                                       \
+  TY_U##W n_ = (n); octet b_[SZ_##W];                                  \
+  STORE##W(b_, n_); GH_HASH((h), b_, SZ_##W);                          \
+} while (0)
+#define GH_HASHU8(h, n)         GH_HASHU_((h), (n), 8)
+#define GH_HASHU16(h, n) GH_HASHU_((h), (n), 16)
+#define GH_HASHU16_B(h, n) GH_HASHU_((h), (n), 16_B)
+#define GH_HASHU16_L(h, n) GH_HASHU_((h), (n), 16_L)
+#define GH_HASHU24(h, n) GH_HASHU_((h), (n), 24)
+#define GH_HASHU24_B(h, n) GH_HASHU_((h), (n), 24_B)
+#define GH_HASHU24_L(h, n) GH_HASHU_((h), (n), 24_L)
+#define GH_HASHU32(h, n) GH_HASHU_((h), (n), 32)
+#define GH_HASHU32_B(h, n) GH_HASHU_((h), (n), 32_B)
+#define GH_HASHU32_L(h, n) GH_HASHU_((h), (n), 32_L)
+#ifdef HAVE_UINT64
+#  define GH_HASHU64(h, n) GH_HASHU_((h), (n), 64)
+#  define GH_HASHU64_B(h, n) GH_HASHU_((h), (n), 64_B)
+#  define GH_HASHU64_L(h, n) GH_HASHU_((h), (n), 64_L)
+#endif
+
+#define GH_HASHBUF_(h, p, sz, W) do {                                  \
+  size_t sz_ = (sz); assert(sz_ <= MASK##W);                           \
+  GH_HASHU_(h, sz_, W); GH_HASH(h, (p), sz_);                          \
+} while (0)
+#define GH_HASHBUF8(h, p, sz) GH_HASHBUF_((h), (p), (sz), 8)
+#define GH_HASHBUF16(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16)
+#define GH_HASHBUF16_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_L)
+#define GH_HASHBUF16_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_B)
+#define GH_HASHBUF24(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24)
+#define GH_HASHBUF24_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_L)
+#define GH_HASHBUF24_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_B)
+#define GH_HASHBUF32(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32)
+#define GH_HASHBUF32_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_L)
+#define GH_HASHBUF32_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_B)
+#ifdef HAVE_UINT64
+#  define GH_HASHBUF64(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64)
+#  define GH_HASHBUF64_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_L)
+#  define GH_HASHBUF64_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_B)
+#endif
+
+#define GH_HASHSTR_(h, p, W) do {                                      \
+  const char *p_ = (p); GH_HASHBUF_((h), p_, strlen(p_), W);           \
+} while (0)
+#define GH_HASHSTR8(h, p) GH_HASHSTR_((h), (p), 8)
+#define GH_HASHSTR16(h, p) GH_HASHSTR_((h), (p), 16)
+#define GH_HASHSTR16_L(h, p) GH_HASHSTR_((h), (p), 16_L)
+#define GH_HASHSTR16_B(h, p) GH_HASHSTR_((h), (p), 16_B)
+#define GH_HASHSTR24(h, p) GH_HASHSTR_((h), (p), 24)
+#define GH_HASHSTR24_L(h, p) GH_HASHSTR_((h), (p), 24_L)
+#define GH_HASHSTR24_B(h, p) GH_HASHSTR_((h), (p), 24_B)
+#define GH_HASHSTR32(h, p) GH_HASHSTR_((h), (p), 32)
+#define GH_HASHSTR32_L(h, p) GH_HASHSTR_((h), (p), 32_L)
+#define GH_HASHSTR32_B(h, p) GH_HASHSTR_((h), (p), 32_B)
+#ifdef HAVE_UINT64
+#  define GH_HASHSTR64(h, p) GH_HASHSTR_((h), (p), 64)
+#  define GH_HASHSTR64_L(h, p) GH_HASHSTR_((h), (p), 64_L)
+#  define GH_HASHSTR64_B(h, p) GH_HASHSTR_((h), (p), 64_B)
+#endif
+
+#define GH_HASHSTRZ(h, p) do {                                         \
+  const char *p_ = (p); GH_HASH((h), p_, strlen(p_) + 1);              \
+} while (0)
+#define GH_HASHSTR(h, p) do {                                          \
+  const char *p_ = (p); GH_HASH((h), p_, strlen(p_));                  \
+} while (0)
+
 typedef struct gchash {
   const char *name;                    /* Name of the hash function */
   size_t hashsz;                       /* Size of output hash */