| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Generic hash wrapper for CRC32 |
| 4 | * |
| 5 | * (c) 2001 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <mLib/crc32.h> |
| 31 | #include <mLib/sub.h> |
| 32 | |
| 33 | #include "arena.h" |
| 34 | #include "crc32.h" |
| 35 | #include "ghash.h" |
| 36 | #include "paranoia.h" |
| 37 | |
| 38 | /*----- Main code ---------------------------------------------------------*/ |
| 39 | |
| 40 | typedef struct gctx { |
| 41 | ghash h; |
| 42 | uint32 c; |
| 43 | octet buf[4]; |
| 44 | } gctx; |
| 45 | |
| 46 | static const ghash_ops gops; |
| 47 | |
| 48 | static ghash *ghinit(void) |
| 49 | { |
| 50 | gctx *g = S_CREATE(gctx); |
| 51 | g->h.ops = &gops; |
| 52 | g->c = 0; |
| 53 | return (&g->h); |
| 54 | } |
| 55 | |
| 56 | static void ghhash(ghash *h, const void *p, size_t sz) |
| 57 | { |
| 58 | gctx *g = (gctx *)h; |
| 59 | CRC32(g->c, g->c, p, sz); |
| 60 | } |
| 61 | |
| 62 | static octet *ghdone(ghash *h, void *buf) |
| 63 | { |
| 64 | gctx *g = (gctx *)h; |
| 65 | if (!buf) |
| 66 | buf = g->buf; |
| 67 | STORE32(buf, g->c); |
| 68 | return (buf); |
| 69 | } |
| 70 | |
| 71 | static void ghdestroy(ghash *h) |
| 72 | { |
| 73 | gctx *g = (gctx *)h; |
| 74 | BURN(*g); |
| 75 | S_DESTROY(g); |
| 76 | } |
| 77 | |
| 78 | static ghash *ghcopy(ghash *h) |
| 79 | { |
| 80 | gctx *g = (gctx *)h; |
| 81 | gctx *gg = S_CREATE(gctx); |
| 82 | memcpy(gg, g, sizeof(gctx)); |
| 83 | return (&gg->h); |
| 84 | } |
| 85 | |
| 86 | static const ghash_ops gops = { &gcrc32, ghhash, ghdone, ghdestroy, ghcopy }; |
| 87 | const gchash gcrc32 = { "crc32", 4, ghinit }; |
| 88 | |
| 89 | /*----- That's all, folks -------------------------------------------------*/ |