Author | Jamozed <[email protected]> |
Date | 2020-08-09 02:08:39 |
Commit | 03a082a8c818091ef7da1fa7a6d4acfabdf06ba1 |
Parent | a6d059a185f9f70a4dd6027bca43ce91d01b2cf5 |
alder32: Add alder32 utility
Diffstat
M | CMakeLists.txt | | | 3 | ++- |
M | README.md | | | 1 | + |
A | src/alder32.c | | | 106 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 109 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f4ab93..ace588c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,4 +16,5 @@ ADD_LIBRARY(lib STATIC ${LIBSRC}) LINK_LIBRARIES(lib) -ADD_EXECUTABLE(crc32 ${PROJECT_SOURCE_DIR}/src/crc32.c) +ADD_EXECUTABLE(alder32 ${PROJECT_SOURCE_DIR}/src/alder32.c) +ADD_EXECUTABLE(crc32 ${PROJECT_SOURCE_DIR}/src/crc32.c) diff --git a/README.md b/README.md index 977151c..433b85c 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ OMKOV cryptutils implements many cryptographic utilities. | Utility | Description | Standard | | ---------------- | ---------------------------------------- | -------- | +| alder32 | Compute the Alder-32 for a file | | | crc32 | Compute the CRC-32 for a file | | ## Build Instructions diff --git a/src/alder32.c b/src/alder32.c new file mode 100644 index 0000000..0f9e468 --- /dev/null +++ b/src/alder32.c @@ -0,0 +1,106 @@ +// alder32.c, version 1.0.0 +// OMKOV cryptutils alder32 +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "lib/error.c" +#include "optget.h" + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> + +#define VERSION "1.0.0" + +static inline int alder32(const char *file); + +static void hlp(void); +static void ver(void); + +int main(int ac, char *av[]) { A0 = av[0]; + lop_t lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } + }; + + opt_t opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; int o; + while((o = optget(&opt, av, 1)) != -1) switch (o) { + case 256: { hlp(); return 0; } + case 257: { ver(); return 0; } + 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; + } + + 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; + + if (!file || (file[0] == '-' && file[1] == 0)) { fi = stdin; } + else if (!(fi = fopen(file, "r"))) { return 1; } + + for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)); fl += c) { + for (register size_t i = 0; i != c; ++i) { + a = (a + buf[i]) % 65521; + b = (b + a) % 65521; + } + } + + printf("%X %zu", (b << 16) | a, fl); + if (file) { printf(" %s", file); } fputc('\n', stdout); + + if (fi != stdin) { fclose(fi); } return 0; +} + +static void hlp(void) { + puts("alder32 - compute the Alder-32 for a file\n"); + puts("usage: alder32 [file...]\n"); + puts("options:"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +static void ver(void) { + puts("OMKOV cryptutils alder32, version " VERSION); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}