da4063df425dce8453f2f3fd12d008dd9101607c
[u/mdw/catacomb] / ghash-def.h
1 /* -*-c-*-
2 *
3 * $Id: ghash-def.h,v 1.6 2004/04/04 19:42:59 mdw Exp $
4 *
5 * Definitions for generic hash interface
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: ghash-def.h,v $
33 * Revision 1.6 2004/04/04 19:42:59 mdw
34 * Add set -e.
35 *
36 * Revision 1.5 2001/01/25 21:39:58 mdw
37 * Burn the hash context when it's done with, for paranoia's sake.
38 *
39 * Revision 1.4 2000/07/15 10:00:58 mdw
40 * New generic hash operation for copying hash contexts.
41 *
42 * Revision 1.3 2000/07/02 18:27:42 mdw
43 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
44 * uses a buffer internal to the ghash object. The operation returns the
45 * address of the buffer it used. Clients of generic hashes no longer need
46 * to use dynamically allocated memory for hash results.
47 *
48 * Revision 1.2 2000/06/17 11:22:03 mdw
49 * Use secure arena for memory allocation. Minor changes in the generic
50 * hash interface.
51 *
52 * Revision 1.1 1999/12/10 23:21:37 mdw
53 * Generic interface.
54 *
55 */
56
57 #ifndef CATACOMB_GHASH_DEF_H
58 #define CATACOMB_GHASH_DEF_H
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64 /*----- Header files ------------------------------------------------------*/
65
66 #include <mLib/bits.h>
67 #include <mLib/sub.h>
68
69 #ifndef CATACOMB_ARENA_H
70 # include "arena.h"
71 #endif
72
73 #ifndef CATACOMB_GHASH_H
74 # include "ghash.h"
75 #endif
76
77 #ifndef CATACOMB_PARANOIA_H
78 # include "paranoia.h"
79 #endif
80
81 /*----- Generic hash function interface -----------------------------------*/
82
83 /* --- @GHASH_DEF@ --- *
84 *
85 * Arguments: @PRE, pre@ = prefixes for hash function
86 *
87 * Use: Defines the generic hash instance.
88 */
89
90 #define GHASH_DEF(PRE, pre) \
91 \
92 static const ghash_ops gops; \
93 \
94 typedef struct gctx { \
95 ghash h; \
96 pre##_ctx c; \
97 octet buf[PRE##_HASHSZ]; \
98 } gctx; \
99 \
100 static ghash *ghinit(void) \
101 { \
102 gctx *g = S_CREATE(gctx); \
103 g->h.ops = &gops; \
104 pre##_init(&g->c); \
105 return (&g->h); \
106 } \
107 \
108 static void ghhash(ghash *h, const void *p, size_t sz) \
109 { \
110 gctx *g = (gctx *)h; \
111 pre##_hash(&g->c, p, sz); \
112 } \
113 \
114 static octet *ghdone(ghash *h, void *buf) \
115 { \
116 gctx *g = (gctx *)h; \
117 if (!buf) \
118 buf = g->buf; \
119 pre##_done(&g->c, buf); \
120 return (buf); \
121 } \
122 \
123 static void ghdestroy(ghash *h) \
124 { \
125 gctx *g = (gctx *)h; \
126 BURN(*g); \
127 S_DESTROY(g); \
128 } \
129 \
130 static ghash *ghcopy(ghash *h) \
131 { \
132 gctx *g = (gctx *)h; \
133 gctx *gg = S_CREATE(gctx); \
134 memcpy(gg, g, sizeof(gctx)); \
135 return (&gg->h); \
136 } \
137 \
138 static const ghash_ops gops = \
139 { &pre, ghhash, ghdone, ghdestroy, ghcopy }; \
140 const gchash pre = { #pre, PRE##_HASHSZ, ghinit, PRE##_BUFSZ };
141
142 /*----- That's all, folks -------------------------------------------------*/
143
144 #ifdef __cplusplus
145 }
146 #endif
147
148 #endif