Author | Jamozed <[email protected]> |
Date | 2021-02-16 00:00:04 |
Commit | c21fdfde41203a66d1ee534b4090c0250369e6c4 |
Parent | 174660fc18f20c41d5f472d695cf714aa5ca78c4 |
alder32: Use BSD or GNU style checksums
Diffstat
M | man/alder32.1 | | | 11 | ++++++----- |
M | src/alder32.c | | | 47 | ++++++++++++++++++++++++++--------------------- |
2 files changed, 32 insertions, 26 deletions
diff --git a/man/alder32.1 b/man/alder32.1 index c266432..3410277 100644 --- a/man/alder32.1 +++ b/man/alder32.1 @@ -1,13 +1,14 @@ -.TH ALDER32 1 2020-08-14 "OMKOV cryptutils" "General Commands Manual" +.TH ALDER32 1 2021-02-16 "OMKOV cryptutils" "General Commands Manual" .SH NAME alder32 \(em compute the Alder-32 for a file .SH SYNOPSYS \fBalder32\fR [\fIfile\fR...] .SH DESCRIPTION -Compute and print an Alder-32 for each input file, as well as the file size in -bytes. +Compute and print an Alder-32 for each input file. .SH OPTIONS The following options are supported: +.IP "\fB--bsd\fR" 8 +Use BSD style checksums. .IP "\fB--help\fR" 8 Display help information. .IP "\fB--version\fR" 8 @@ -15,8 +16,8 @@ Display version information. .SH OPERANDS The following operand is supported: .IP "\fIfile\fR" 8 -A pathname of an input file. If no \fIfile\fR operands are specified, or -\fIfile\fR is a '\fB-\fR', \fIalder32\fR will read from standard input. +A pathname of an input file. If no \fIfile\fR operands are specified then +\fIalder32\fR will read from standard input. .SH EXIT STATUS The following exit values will be returned: .IP "\ 0" 8 diff --git a/src/alder32.c b/src/alder32.c index 3956c16..55ad113 100644 --- a/src/alder32.c +++ b/src/alder32.c @@ -1,4 +1,4 @@ -// alder32.c, version 1.0.0 +// alder32.c, version 1.1.0 // OMKOV cryptutils alder32 // Copyright (C) 2020, Jakob Wakeling // All rights reserved. @@ -37,61 +37,66 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include <stdint.h> #include <stdio.h> -#define VERSION "1.0.0" +#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 inline int alder32(const char *file); +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 = ""; opt.lops = lops; + 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; } } - bool warned = false; - if (opt.ind == ac) { alder32(NULL); } - else for (char **p = &av[opt.ind]; *p; ++p) if (alder32(*p)) { - warn("%s: %s", *p, serr()); warned = true; - } + else for (char **p = &av[opt.ind]; *p; ++p) { alder32(*p); } return warned; } -static inline int alder32(const char *file) { - uint8_t buf[BUFSIZ * 16]; FILE *fi; - register size_t fl = 0; register uint32_t a = 1, b = 0; +static inline void alder32(const char *file) { + uint8_t buf[BUFSIZ * 16]; FILE *fi; register uint32_t a = 1, b = 0; - if (!file || (file[0] == '-' && file[1] == 0)) { fi = stdin; } - else if (!(fi = fopen(file, "r"))) { return 1; } + 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)); fl += c) { + 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 = (a + buf[i]) % 65521; b = (b + a) % 65521; } - } + } a = (b << 16) | a; + + if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; } - printf("%X %zu", (b << 16) | a, fl); - if (file) { printf(" %s", file); } fputc('\n', stdout); + 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 0; + 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"); return;