a4738812 |
1 | /* -*-c-*- |
a4738812 |
2 | * |
3 | * Definitions for generic hash interface |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
a4738812 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
a4738812 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
a4738812 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
a4738812 |
28 | #ifndef CATACOMB_GHASH_DEF_H |
29 | #define CATACOMB_GHASH_DEF_H |
30 | |
31 | #ifdef __cplusplus |
32 | extern "C" { |
33 | #endif |
34 | |
35 | /*----- Header files ------------------------------------------------------*/ |
36 | |
37 | #include <mLib/bits.h> |
38 | #include <mLib/sub.h> |
39 | |
28ef1f45 |
40 | #ifndef CATACOMB_ARENA_H |
41 | # include "arena.h" |
42 | #endif |
43 | |
a4738812 |
44 | #ifndef CATACOMB_GHASH_H |
45 | # include "ghash.h" |
46 | #endif |
47 | |
f80463ac |
48 | #ifndef CATACOMB_PARANOIA_H |
49 | # include "paranoia.h" |
50 | #endif |
51 | |
a4738812 |
52 | /*----- Generic hash function interface -----------------------------------*/ |
53 | |
54 | /* --- @GHASH_DEF@ --- * |
55 | * |
56 | * Arguments: @PRE, pre@ = prefixes for hash function |
57 | * |
58 | * Use: Defines the generic hash instance. |
59 | */ |
60 | |
61 | #define GHASH_DEF(PRE, pre) \ |
62 | \ |
63 | static const ghash_ops gops; \ |
64 | \ |
65 | typedef struct gctx { \ |
66 | ghash h; \ |
67 | pre##_ctx c; \ |
a351d052 |
68 | octet buf[PRE##_HASHSZ]; \ |
a4738812 |
69 | } gctx; \ |
70 | \ |
71 | static ghash *ghinit(void) \ |
72 | { \ |
28ef1f45 |
73 | gctx *g = S_CREATE(gctx); \ |
a4738812 |
74 | g->h.ops = &gops; \ |
75 | pre##_init(&g->c); \ |
76 | return (&g->h); \ |
77 | } \ |
78 | \ |
79 | static void ghhash(ghash *h, const void *p, size_t sz) \ |
80 | { \ |
81 | gctx *g = (gctx *)h; \ |
82 | pre##_hash(&g->c, p, sz); \ |
83 | } \ |
84 | \ |
a351d052 |
85 | static octet *ghdone(ghash *h, void *buf) \ |
a4738812 |
86 | { \ |
87 | gctx *g = (gctx *)h; \ |
a351d052 |
88 | if (!buf) \ |
89 | buf = g->buf; \ |
a4738812 |
90 | pre##_done(&g->c, buf); \ |
a351d052 |
91 | return (buf); \ |
a4738812 |
92 | } \ |
93 | \ |
94 | static void ghdestroy(ghash *h) \ |
95 | { \ |
96 | gctx *g = (gctx *)h; \ |
f80463ac |
97 | BURN(*g); \ |
28ef1f45 |
98 | S_DESTROY(g); \ |
a4738812 |
99 | } \ |
100 | \ |
d2fdbc2c |
101 | static ghash *ghcopy(ghash *h) \ |
102 | { \ |
103 | gctx *g = (gctx *)h; \ |
104 | gctx *gg = S_CREATE(gctx); \ |
105 | memcpy(gg, g, sizeof(gctx)); \ |
106 | return (&gg->h); \ |
107 | } \ |
108 | \ |
109 | static const ghash_ops gops = \ |
110 | { &pre, ghhash, ghdone, ghdestroy, ghcopy }; \ |
e9026a0a |
111 | const gchash pre = { #pre, PRE##_HASHSZ, ghinit, PRE##_BUFSZ }; |
a4738812 |
112 | |
113 | /*----- That's all, folks -------------------------------------------------*/ |
114 | |
115 | #ifdef __cplusplus |
116 | } |
117 | #endif |
118 | |
119 | #endif |