a4738812 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: ghash-def.h,v 1.7 2004/04/08 01:36:15 mdw Exp $ |
a4738812 |
4 | * |
5 | * Definitions for generic hash interface |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
a4738812 |
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. |
45c0fd36 |
18 | * |
a4738812 |
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. |
45c0fd36 |
23 | * |
a4738812 |
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 | |
a4738812 |
30 | #ifndef CATACOMB_GHASH_DEF_H |
31 | #define CATACOMB_GHASH_DEF_H |
32 | |
33 | #ifdef __cplusplus |
34 | extern "C" { |
35 | #endif |
36 | |
37 | /*----- Header files ------------------------------------------------------*/ |
38 | |
39 | #include <mLib/bits.h> |
40 | #include <mLib/sub.h> |
41 | |
28ef1f45 |
42 | #ifndef CATACOMB_ARENA_H |
43 | # include "arena.h" |
44 | #endif |
45 | |
a4738812 |
46 | #ifndef CATACOMB_GHASH_H |
47 | # include "ghash.h" |
48 | #endif |
49 | |
f80463ac |
50 | #ifndef CATACOMB_PARANOIA_H |
51 | # include "paranoia.h" |
52 | #endif |
53 | |
a4738812 |
54 | /*----- Generic hash function interface -----------------------------------*/ |
55 | |
56 | /* --- @GHASH_DEF@ --- * |
57 | * |
58 | * Arguments: @PRE, pre@ = prefixes for hash function |
59 | * |
60 | * Use: Defines the generic hash instance. |
61 | */ |
62 | |
63 | #define GHASH_DEF(PRE, pre) \ |
64 | \ |
65 | static const ghash_ops gops; \ |
66 | \ |
67 | typedef struct gctx { \ |
68 | ghash h; \ |
69 | pre##_ctx c; \ |
a351d052 |
70 | octet buf[PRE##_HASHSZ]; \ |
a4738812 |
71 | } gctx; \ |
72 | \ |
73 | static ghash *ghinit(void) \ |
74 | { \ |
28ef1f45 |
75 | gctx *g = S_CREATE(gctx); \ |
a4738812 |
76 | g->h.ops = &gops; \ |
77 | pre##_init(&g->c); \ |
78 | return (&g->h); \ |
79 | } \ |
80 | \ |
81 | static void ghhash(ghash *h, const void *p, size_t sz) \ |
82 | { \ |
83 | gctx *g = (gctx *)h; \ |
84 | pre##_hash(&g->c, p, sz); \ |
85 | } \ |
86 | \ |
a351d052 |
87 | static octet *ghdone(ghash *h, void *buf) \ |
a4738812 |
88 | { \ |
89 | gctx *g = (gctx *)h; \ |
a351d052 |
90 | if (!buf) \ |
91 | buf = g->buf; \ |
a4738812 |
92 | pre##_done(&g->c, buf); \ |
a351d052 |
93 | return (buf); \ |
a4738812 |
94 | } \ |
95 | \ |
96 | static void ghdestroy(ghash *h) \ |
97 | { \ |
98 | gctx *g = (gctx *)h; \ |
f80463ac |
99 | BURN(*g); \ |
28ef1f45 |
100 | S_DESTROY(g); \ |
a4738812 |
101 | } \ |
102 | \ |
d2fdbc2c |
103 | static ghash *ghcopy(ghash *h) \ |
104 | { \ |
105 | gctx *g = (gctx *)h; \ |
106 | gctx *gg = S_CREATE(gctx); \ |
107 | memcpy(gg, g, sizeof(gctx)); \ |
108 | return (&gg->h); \ |
109 | } \ |
110 | \ |
111 | static const ghash_ops gops = \ |
112 | { &pre, ghhash, ghdone, ghdestroy, ghcopy }; \ |
e9026a0a |
113 | const gchash pre = { #pre, PRE##_HASHSZ, ghinit, PRE##_BUFSZ }; |
a4738812 |
114 | |
115 | /*----- That's all, folks -------------------------------------------------*/ |
116 | |
117 | #ifdef __cplusplus |
118 | } |
119 | #endif |
120 | |
121 | #endif |