1 #define _POSIX_C_SOURCE 200809L
18 int zpm_hash_mem(void *mem, size_t size, char *hash) {
19 struct blake2b_state__ blake;
21 unsigned char tmp[32];
23 blake2b_init(&blake, sizeof tmp);
24 blake2b_update(&blake, mem, size);
25 blake2b_final(&blake, tmp, sizeof tmp);
27 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
32 /* flags 0, close mmap, flags 1, return mmap fd */
33 int zpm_hash(char *path, char *hash, uint32_t flags) {
39 fd = open(path, O_RDONLY);
41 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
44 if (fstat(fd, &sbuf) == -1) {
45 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
48 /* not a regular file? */
49 if (!S_ISREG(sbuf.st_mode)) {
50 /* TODO this is ok, just stored differently */
51 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
55 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
58 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
62 zpm_hash_mem(content, sbuf.st_size, hash);
64 munmap(content, sbuf.st_size);
65 return flags ? fd : 1;