// alder32.c, version 1.1.0 // OMKOV cryptutils alder32 // Copyright (C) 2020, Jakob Wakeling // MIT Licence #include "util/error.c" #include "util/optget.h" #include #include #include #define VERSION "1.1.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 alder32(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) { alder32(NULL); } else for (char **p = &av[opt.ind]; *p; ++p) { alder32(*p); } return warned; } static inline void alder32(const char *file) { uint8_t buf[BUFSIZ * 16]; FILE *fi; register uint32_t a = 1, b = 0; 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) { a = (a + buf[i]) % 65521; b = (b + a) % 65521; } } a = (b << 16) | a; if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; } if (!file || qflag) { printf("%08X\n", a); } else switch (form) { case 0: { printf("%08X *%s\n", a, file); break; } case 1: { printf("ALDER32 (%s) = %08X\n", file, a); break; } } if (fi != stdin) { fclose(fi); } return; } static void hlp(void) { puts("alder32 - compute the Alder-32 for a file\n"); puts("usage: alder32 [file...]\n"); puts("options:"); puts(" --bsd Use BSD style checksums"); puts(" --help Display help information"); puts(" --version Display version information"); } static void ver(void) { puts("OMKOV cryptutils alder32, version " VERSION); puts("Copyright (C) 2020, Jakob Wakeling"); puts("MIT Licence (https://opensource.org/licenses/MIT)"); }