thash.c - dedup - data deduplication program HTML git clone git://bitreich.org/dedup/ git://hg6vgqziawt5s4dj.onion/dedup/ DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- thash.c (1587B) --- 1 #include <sys/types.h> 2 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <strings.h> 8 9 #include "blake2.h" 10 #include "dedup.h" 11 12 static struct hash_ops { 13 int (*init)(struct hash_ctx *ctx, size_t n); 14 int (*update)(struct hash_ctx *ctx, const void *buf, size_t n); 15 int (*final)(struct hash_ctx *ctx, void *buf, size_t n); 16 } hashes[NR_HASHES] = { 17 { 18 .init = blake2bi, 19 .update = blake2bu, 20 .final = blake2bf, 21 }, 22 { 23 .init = blake2bpi, 24 .update = blake2bpu, 25 .final = blake2bpf, 26 }, 27 { 28 .init = blake2si, 29 .update = blake2su, 30 .final = blake2sf, 31 }, 32 { 33 .init = blake2spi, 34 .update = blake2spu, 35 .final = blake2spf, 36 }, 37 }; 38 39 static char *algomap[NR_HASHES] = { 40 [HASH_BLAKE2B] = "blake2b", 41 [HASH_BLAKE2BP] = "blake2bp", 42 [HASH_BLAKE2S] = "blake2s", 43 [HASH_BLAKE2SP] = "blake2sp", 44 }; 45 46 int 47 hash_init(struct hash_ctx *ctx, int type, size_t n) 48 { 49 if (type < 0 || type >= NR_HASHES) 50 return -1; 51 52 ctx->ops = &hashes[type]; 53 return (*ctx->ops->init)(ctx, n); 54 } 55 56 int 57 hash_update(struct hash_ctx *ctx, const void *buf, size_t n) 58 { 59 return (*ctx->ops->update)(ctx, buf, n); 60 } 61 62 int 63 hash_final(struct hash_ctx *ctx, void *buf, size_t n) 64 { 65 return (*ctx->ops->final)(ctx, buf, n); 66 } 67 68 int 69 hash_name2type(char *name) 70 { 71 size_t i; 72 73 for (i = 0; i < NR_HASHES; i++) 74 if (strcasecmp(algomap[i], name) == 0) 75 return i; 76 return -1; 77 } 78 79 char * 80 hash_type2name(int type) 81 { 82 if (type < 0 || type >= NR_HASHES) 83 return NULL; 84 return algomap[type]; 85 } 86 87 void 88 hash_list(int fd) 89 { 90 size_t i; 91 92 for (i = 0; i < NR_HASHES; i++) 93 dprintf(fd, "%s\n", algomap[i]); 94 }