Remove buf bits which moved to mLib. Fix email addresses.
[u/mdw/catacomb] / ghash.h
diff --git a/ghash.h b/ghash.h
index 85b3d7f..4b8e6a3 100644 (file)
--- a/ghash.h
+++ b/ghash.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: ghash.h,v 1.3 2000/07/02 18:27:42 mdw Exp $
+ * $Id$
  *
  * Generic hash function interface
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: ghash.h,v $
- * 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
 
@@ -55,6 +38,8 @@
 
 #include <stddef.h>
 
+#include <mLib/bits.h>
+
 /*----- Generic hash function interface -----------------------------------*/
 
 typedef struct ghash {
@@ -66,14 +51,102 @@ typedef struct ghash_ops {
   void (*hash)(ghash */*h*/, const void */*p*/, size_t /*sz*/); /* Hash */
   octet *(*done)(ghash */*h*/, void */*buf*/); /* Write result */
   void (*destroy)(ghash */*h*/);       /* Destroy hash block */
+  ghash *(*copy)(ghash */*h*/);                /* Make a copy of the hash context */
 } ghash_ops;
 
+#define GH_INIT(ch)            (ch)->init()
+#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_HASHSTR32(h, p) GH_HASHSTR_((h), (p), 64)
+#  define GH_HASHSTR32_L(h, p) GH_HASHSTR_((h), (p), 64_L)
+#  define GH_HASHSTR32_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 */
   ghash *(*init)(void);                        /* Create a new hash instance */
+  size_t bufsz;                                /* Buffer size, or zero */
 } gchash;
 
+/*----- Tables ------------------------------------------------------------*/
+
+extern const gchash *const ghashtab[];
+
+/* --- @ghash_byname@ --- *
+ *
+ * Arguments:  @const char *p@ = pointer to name string
+ *
+ * Returns:    The named cipher class, or null.
+ */
+
+extern const gchash *ghash_byname(const char */*p*/);
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus