Browse Source

alder32: Add alder32 utility

master
Jake Wakeling 1 month ago
parent
commit
03a082a8c8
3 changed files with 109 additions and 1 deletions
  1. +2
    -1
      CMakeLists.txt
  2. +1
    -0
      README.md
  3. +106
    -0
      src/alder32.c

+ 2
- 1
CMakeLists.txt View File

@@ -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)

+ 1
- 0
README.md View File

@@ -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


+ 106
- 0
src/alder32.c View File

@@ -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;
}

Loading…
Cancel
Save