012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
|
// fnv1a64.c, version 1.0.0
// OMKOV cryptutils fnv1a64
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence
#include "util/error.c"
#include "util/optget.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#define VERSION "1.0.0"
static struct lop lops[] = {
{ "help", ARG_NUL, 256 },
{ "version", ARG_NUL, 257 },
{ "bsd", ARG_NUL, 258 },
{ NULL, 0, 0 }
};
static bool qflag;
static int form;
static inline void fnv1a64(const char *file);
static void hlp(void);
static void ver(void);
int main(int ac, char *av[]) { A0 = av[0];
struct opt opt = OPTGET_INIT; opt.str = "q"; opt.lops = lops;
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
case 'q': { qflag = true; break; }
case 256: { hlp(); return 0; }
case 257: { ver(); return 0; }
case 258: { form = 1; break; }
default: { return 1; }
}
if (opt.ind == ac) { fnv1a64(NULL); }
else for (char **p = &av[opt.ind]; *p; ++p) { fnv1a64(*p); }
return warned;
}
static inline void fnv1a64(const char *file) {
uint8_t buf[BUFSIZ * 16]; FILE *fi;
register uint64_t h = 0xCBF29CE484222325;
if (!file) { fi = stdin; } else { fi = fopen(file, "rb"); }
if (!fi) { warn("%s: %s", file, serr()); return; }
for (size_t c; (c = fread(buf, 1, sizeof (buf), fi));) {
for (register size_t i = 0; i != c; ++i) {
h ^= buf[i]; h *= 0x00000100000001B3;
}
}
if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; }
if (!file || qflag) { printf("%016lX\n", h); } else switch (form) {
case 0: { printf("%016lX *%s\n", h, file); break; }
case 1: { printf("FNV1A64 (%s) = %016lX\n", file, h); break; }
}
if (fi != stdin) { fclose(fi); } return;
}
/* Print help information */
static void hlp(void) {
puts("fnv1a64 - Compute the FNV1a-64 for a file\n");
puts("usage: fnv1a64 [-q] [--bsd] [file...]\n");
puts("options:");
puts(" -q Output only the hash value");
puts(" --bsd Use BSD style hashes");
puts(" --help Display help information");
puts(" --version Display version information");
}
/* Print version information */
static void ver(void) {
puts("OMKOV cryptutils fnv1a64, version " VERSION);
puts("Copyright (C) 2021, Jakob Wakeling");
puts("MIT Licence (https://opensource.org/licenses/MIT)");
}
|