c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
0
|
// alder32.c, version 1.1.0 |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
1
|
// OMKOV cryptutils alder32 |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
45f70e3 |
Jamozed |
2022-03-06 17:15:30 |
3
|
// MIT Licence |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
4
|
|
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
5
|
#include "util/error.c" |
c01a723 |
Jamozed |
2022-02-05 19:04:03 |
6
|
#include "util/optget.h" |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
7
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
8
|
#include <stdbool.h> |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
9
|
#include <stdint.h> |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
10
|
#include <stdio.h> |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
11
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
12
|
#define VERSION "1.1.0" |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
13
|
|
40437ba |
Jamozed |
2020-08-28 23:41:53 |
14
|
static struct lop lops[] = { |
40437ba |
Jamozed |
2020-08-28 23:41:53 |
15
|
{ "help", ARG_NUL, 256 }, |
40437ba |
Jamozed |
2020-08-28 23:41:53 |
16
|
{ "version", ARG_NUL, 257 }, |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
17
|
{ "bsd", ARG_NUL, 258 }, |
40437ba |
Jamozed |
2020-08-28 23:41:53 |
18
|
{ NULL, 0, 0 } |
40437ba |
Jamozed |
2020-08-28 23:41:53 |
19
|
}; |
40437ba |
Jamozed |
2020-08-28 23:41:53 |
20
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
21
|
static bool qflag; |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
22
|
static int form; |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
23
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
24
|
static inline void alder32(const char *file); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
25
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
26
|
static void hlp(void); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
27
|
static void ver(void); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
28
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
29
|
int main(int ac, char *av[]) { A0 = av[0]; |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
30
|
struct opt opt = OPTGET_INIT; opt.str = "q"; opt.lops = lops; |
40437ba |
Jamozed |
2020-08-28 23:41:53 |
31
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
32
|
case 'q': { qflag = true; break; } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
33
|
case 256: { hlp(); return 0; } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
34
|
case 257: { ver(); return 0; } |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
35
|
case 258: { form = 1; break; } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
36
|
default: { return 1; } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
37
|
} |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
38
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
39
|
if (opt.ind == ac) { alder32(NULL); } |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
40
|
else for (char **p = &av[opt.ind]; *p; ++p) { alder32(*p); } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
41
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
42
|
return warned; |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
43
|
} |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
44
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
45
|
static inline void alder32(const char *file) { |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
46
|
uint8_t buf[BUFSIZ * 16]; FILE *fi; register uint32_t a = 1, b = 0; |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
47
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
48
|
if (!file) { fi = stdin; } else { fi = fopen(file, "rb"); } |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
49
|
if (!fi) { warn("%s: %s", file, serr()); return; } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
50
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
51
|
for (size_t c; (c = fread(buf, 1, sizeof (buf), fi));) { |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
52
|
for (register size_t i = 0; i != c; ++i) { |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
53
|
a = (a + buf[i]) % 65521; b = (b + a) % 65521; |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
54
|
} |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
55
|
} a = (b << 16) | a; |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
56
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
57
|
if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; } |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
58
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
59
|
if (!file || qflag) { printf("%08X\n", a); } else switch (form) { |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
60
|
case 0: { printf("%08X *%s\n", a, file); break; } |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
61
|
case 1: { printf("ALDER32 (%s) = %08X\n", file, a); break; } |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
62
|
} |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
63
|
|
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
64
|
if (fi != stdin) { fclose(fi); } return; |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
65
|
} |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
66
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
67
|
static void hlp(void) { |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
68
|
puts("alder32 - compute the Alder-32 for a file\n"); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
69
|
puts("usage: alder32 [file...]\n"); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
70
|
puts("options:"); |
c21fdfd |
Jamozed |
2021-02-16 13:00:04 |
71
|
puts(" --bsd Use BSD style checksums"); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
72
|
puts(" --help Display help information"); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
73
|
puts(" --version Display version information"); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
74
|
} |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
75
|
|
03a082a |
Jamozed |
2020-08-09 14:08:39 |
76
|
static void ver(void) { |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
77
|
puts("OMKOV cryptutils alder32, version " VERSION); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
78
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
45f70e3 |
Jamozed |
2022-03-06 17:15:30 |
79
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
03a082a |
Jamozed |
2020-08-09 14:08:39 |
80
|
} |
|
|
|
81
|
|