diff options
Diffstat (limited to '')
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | arg.h | 76 | ||||
-rw-r--r-- | common.c | 213 | ||||
-rw-r--r-- | common.h | 43 | ||||
-rw-r--r-- | config.mk | 2 | ||||
-rw-r--r-- | keccak-224sum.c | 4 | ||||
-rw-r--r-- | keccak-256sum.c | 4 | ||||
-rw-r--r-- | keccak-384sum.c | 4 | ||||
-rw-r--r-- | keccak-512sum.c | 4 | ||||
-rw-r--r-- | keccaksum.c | 2 | ||||
-rw-r--r-- | rawshake256sum.c | 2 | ||||
-rw-r--r-- | rawshake512sum.c | 2 | ||||
-rw-r--r-- | sha3-224sum.c | 2 | ||||
-rw-r--r-- | sha3-256sum.c | 2 | ||||
-rw-r--r-- | sha3-384sum.c | 2 | ||||
-rw-r--r-- | sha3-512sum.c | 2 | ||||
-rw-r--r-- | shake256sum.c | 2 | ||||
-rw-r--r-- | shake512sum.c | 2 |
18 files changed, 219 insertions, 151 deletions
@@ -20,7 +20,7 @@ BIN =\ shake512sum MAN1 = $(BIN:=.1) -HDR = common.h +HDR = arg.h common.h keccak-224sum = Keccak-224 keccak-256sum = Keccak-256 @@ -0,0 +1,76 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][0] == '-') {\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGALT(SYMBOL) }\ + } else if (argv[0][0] == SYMBOL) {\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } else {\ + break;\ + }\ + } + +#define ARGC() argc_ + +#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX)) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" +#include "arg.h" #include <sys/stat.h> #include <alloca.h> @@ -13,24 +14,8 @@ -#ifndef STDIN_PATH -# ifndef DEVDIR -# define DEVDIR "/dev" -# endif -# define STDIN_PATH DEVDIR"/stdin" -#endif - - - #define USER_ERROR(string)\ - (fprintf(stderr, "%s: %s.\n", execname, string), 1) - -#define ADD(arg, desc, ...)\ - (arg ? args_add_option(args_new_argumented(NULL, arg, 0, __VA_ARGS__, NULL), desc)\ - : args_add_option(args_new_argumentless(NULL, 0, __VA_ARGS__, NULL), desc)) - -#define LAST(arg)\ - (args_opts_get(arg)[args_opts_get_count(arg) - 1]) + (fprintf(stderr, "%s: %s\n", argv0, string), 1) @@ -57,11 +42,24 @@ static int bad_found = 0; /** * `argv[0]` from `main` */ -static char *execname; +char *argv0; /** + * Print usage information and exit + */ +static void +usage(void) +{ + fprintf(stderr, "usage: %s [-u | -l | -b | -c] [-R rate] [-C capacity] " + "[(-N | -O) output-size] [(-S | -B) state-size] [-W word-size] " + "[-Z squeeze-count] [-vx] [file ...]", argv0); + exit(1); +} + + +/** * Calculate a Keccak-family hashsum of a file, * the content of the file is assumed non-sensitive * @@ -187,17 +185,17 @@ hash(const char *restrict filename, const libkeccak_spec_t *restrict spec, length = (size_t)((spec->output + 7) / 8); if (!hashsum && (hashsum = malloc(length * sizeof(char)), !hashsum)) - return perror(execname), 2; + return perror(argv0), 2; if (!hexsum && (hexsum = malloc((length * 2 + 1) * sizeof(char)), !hexsum)) - return perror(execname), 2; + return perror(argv0), 2; - if (fd = open(strcmp(filename, "-") ? filename : STDIN_PATH, O_RDONLY), fd < 0) - return r = (errno != ENOENT), perror(execname), r + 1; + if (fd = open(strcmp(filename, "-") ? filename : "/dev/stdin", O_RDONLY), fd < 0) + return r = (errno != ENOENT), perror(argv0), r + 1; if ((hex == 0 ? libkeccak_generalised_sum_fd : generalised_sum_fd_hex) (fd, &state, spec, suffix, squeezes > 1 ? NULL : hashsum)) - return perror(execname), close(fd), libkeccak_state_fast_destroy(&state), 2; + return perror(argv0), close(fd), libkeccak_state_fast_destroy(&state), 2; close(fd); if (squeezes > 2) libkeccak_fast_squeeze(&state, squeezes - 2); @@ -248,17 +246,17 @@ check(const libkeccak_spec_t *restrict spec, long squeezes, const char *restrict /** * Check checksums from a file * - * @param filename The file to hash - * @param spec Hashing parameters - * @param squeezes The number of squeezes to perform - * @param suffix The message suffix - * @param representation (unused) - * @param hex Whether to use hexadecimal input rather than binary - * @return Zero on success, an appropriate exit value on error + * @param filename The file to hash + * @param spec Hashing parameters + * @param squeezes The number of squeezes to perform + * @param suffix The message suffix + * @param style (unused) + * @param hex Whether to use hexadecimal input rather than binary + * @return Zero on success, an appropriate exit value on error */ static int check_checksums(const char *restrict filename, const libkeccak_spec_t *restrict spec, - long squeezes, const char *restrict suffix, int representation, int hex) + long squeezes, const char *restrict suffix, enum representation style, int hex) { struct stat attr; size_t blksize = 4096; @@ -275,7 +273,7 @@ check_checksums(const char *restrict filename, const libkeccak_spec_t *restrict size_t hash_n; char c; - if (fd = open(strcmp(filename, "-") ? filename : STDIN_PATH, O_RDONLY), fd < 0) + if (fd = open(strcmp(filename, "-") ? filename : "/dev/stdin", O_RDONLY), fd < 0) goto pfail; if (fstat(fd, &attr) == 0) { @@ -370,32 +368,31 @@ check_checksums(const char *restrict filename, const libkeccak_spec_t *restrict return 0; pfail: - perror(execname); + perror(argv0); fail: free(buf); if (fd >= 0) close(fd); return rc; - (void) representation; + (void) style; } /** * Print the checksum of a file * - * @param filename The file to hash - * @param spec Hashing parameters - * @param squeezes The number of squeezes to perform - * @param suffix The message suffix - * @param representation Either of `REPRESENTATION_BINARY`, `REPRESENTATION_UPPER_CASE` - * and `REPRESENTATION_LOWER_CASE` - * @param hex Whether to use hexadecimal input rather than binary - * @return Zero on success, an appropriate exit value on error + * @param filename The file to hash + * @param spec Hashing parameters + * @param squeezes The number of squeezes to perform + * @param suffix The message suffix + * @param style How the hashes shall be represented + * @param hex Whether to use hexadecimal input rather than binary + * @return Zero on success, an appropriate exit value on error */ static int print_checksum(const char *restrict filename, const libkeccak_spec_t *restrict spec, - long squeezes, const char *restrict suffix, int representation, int hex) + long squeezes, const char *restrict suffix, enum representation style, int hex) { size_t length = (size_t)((spec->output + 7) / 8); int r; @@ -405,10 +402,10 @@ print_checksum(const char *restrict filename, const libkeccak_spec_t *restrict s if ((r = hash(filename, spec, squeezes, suffix, hex))) return r; - if (representation == REPRESENTATION_UPPER_CASE) { + if (style == REPRESENTATION_UPPER_CASE) { libkeccak_behex_upper(hexsum, hashsum, length); printf("%s %s\n", hexsum, filename); - } else if (representation == REPRESENTATION_LOWER_CASE) { + } else if (style == REPRESENTATION_LOWER_CASE) { libkeccak_behex_lower(hexsum, hashsum, length); printf("%s %s\n", hexsum, filename); } else { @@ -416,7 +413,7 @@ print_checksum(const char *restrict filename, const libkeccak_spec_t *restrict s while (length - ptr) { wrote = write(STDOUT_FILENO, hashsum, length - ptr); if (wrote <= 0) - return perror(execname), 2; + return perror(argv0), 2; ptr += (size_t)wrote; } } @@ -426,17 +423,6 @@ print_checksum(const char *restrict filename, const libkeccak_spec_t *restrict s /** - * Cleanup allocations - */ -static inline void -cleanup(void) -{ - free(hashsum), hashsum = NULL; - free(hexsum), hexsum = NULL; -} - - -/** * Parse the command line and calculate the hashes of the selected files * * @param argc The first argument from `main` @@ -448,48 +434,62 @@ cleanup(void) int run(int argc, char *argv[], libkeccak_generalised_spec_t *restrict gspec, const char *restrict suffix) { - int r, verbose = 0, presentation = REPRESENTATION_UPPER_CASE, hex = 0, check = 0; - long squeezes = 1; - size_t i; - libkeccak_spec_t spec; + enum representation style = REPRESENTATION_UPPER_CASE; + int verbose = 0; + int hex = 0; + int check = 0; + long int squeezes = 1; int (*fun)(const char *restrict filename, const libkeccak_spec_t *restrict spec, - long squeezes, const char *restrict suffix, int representation, int hex); - - execname = *argv; - - ADD(NULL, "Display option summary", "-h", "--help"); - ADD("RATE", "Select rate", "-R", "--bitrate", "--rate"); - ADD("CAPACITY", "Select capacity", "-C", "--capacity"); - ADD("SIZE", "Select output size", "-N", "-O", "--output-size", "--output"); - ADD("SIZE", "Select state size", "-S", "-B", "--state-size", "--state"); - ADD("SIZE", "Select word size", "-W", "--word-size", "--word"); - ADD("COUNT", "Select squeeze count", "-Z", "--squeezes"); - ADD(NULL, "Use upper-case output", "-u", "--upper", "--uppercase", "--upper-case"); - ADD(NULL, "Use lower-case output", "-l", "--lower", "--lowercase", "--lower-case"); - ADD(NULL, "Use binary output", "-b", "--binary"); - ADD(NULL, "Use hexadecimal input", "-x", "--hex", "--hex-input"); - ADD(NULL, "Check checksums", "-c", "--check"); - ADD(NULL, "Be verbose", "-v", "--verbose"); - /* --check has been added because the sha1sum, sha256sum &c have it, - * but I ignore the other crap, mostly because not all implemention - * have them and binary vs text mode is stupid. */ - - args_parse(argc, argv); - - if (args_opts_used("-h")) return args_help(0), args_dispose(), 0; - if (args_opts_used("-R")) gspec->bitrate = atol(LAST("-R")); - if (args_opts_used("-C")) gspec->capacity = atol(LAST("-C")); - if (args_opts_used("-N")) gspec->output = atol(LAST("-N")); - if (args_opts_used("-S")) gspec->state_size = atol(LAST("-S")); - if (args_opts_used("-W")) gspec->word_size = atol(LAST("-W")); - if (args_opts_used("-Z")) squeezes = atol(LAST("-Z")); - if (args_opts_used("-u")) presentation = REPRESENTATION_UPPER_CASE; - if (args_opts_used("-l")) presentation = REPRESENTATION_LOWER_CASE; - if (args_opts_used("-b")) presentation = REPRESENTATION_BINARY; - if (args_opts_used("-x")) hex = 1; - if (args_opts_used("-c")) check = 1; - if (args_opts_used("-v")) verbose = 1; - + long squeezes, const char *restrict suffix, enum representation style, int hex); + libkeccak_spec_t spec; + int r; + + ARGBEGIN { + case 'R': + gspec->bitrate = atol(EARGF(usage())); + break; + case 'C': + gspec->capacity = atol(EARGF(usage())); + break; + case 'N': + case 'O': + gspec->output = atol(EARGF(usage())); + break; + case 'S': + case 'B': + gspec->state_size = atol(EARGF(usage())); + break; + case 'W': + gspec->word_size = atol(EARGF(usage())); + break; + case 'Z': + squeezes = atol(EARGF(usage())); + break; + case 'u': + style = REPRESENTATION_UPPER_CASE; + break; + case 'l': + style = REPRESENTATION_LOWER_CASE; + break; + case 'b': + style = REPRESENTATION_BINARY; + break; + case 'c': + check = 1; + break; + case 'x': + hex = 1; + break; + case 'v': + verbose = 1; + break; + default: + usage(); + } ARGEND; + /* -c has been added because the sha1sum, sha256sum &c have + * it, but I ignore the other crap, mostly because not all + * implemention have them and binary vs text mode is stupid. */ + fun = check ? check_checksums : print_checksum; if ((r = make_spec(gspec, &spec))) @@ -510,17 +510,14 @@ run(int argc, char *argv[], libkeccak_generalised_spec_t *restrict gspec, const fprintf(stderr, "suffix: %s\n", suffix ? suffix : ""); } - if (args_files_count == 0) { - r = fun("-", &spec, squeezes, suffix, presentation, hex); - } else { - for (i = 0; i < (size_t)args_files_count; i++) - if ((r = fun(args_files[i], &spec, squeezes, suffix, presentation, hex))) - break; - } + if (!*argv) + r = fun("-", &spec, squeezes, suffix, style, hex); + for (; *argv; argv++) + if ((r = fun(*argv, &spec, squeezes, suffix, style, hex))) + break; done: - args_dispose(); - cleanup(); + free(hashsum); + free(hexsum); return r ? r : bad_found; } - @@ -1,39 +1,34 @@ /* See LICENSE file for copyright and license details. */ #include <libkeccak.h> -#include <argparser.h> - -#define libkeccak_spec_keccak libkeccak_spec_sha3 -#define LIBKECCAK_KECCAK_SUFFIX "" - /** * Wrapper for `run` that also initialises the command line parser * - * @param algo The name of the hashing algorithm, must be a string literal - * @param prog The name of program, must be a string literal * @param suffix The message suffix */ -#define RUN(algo, prog, suffix)\ - (args_init(algo " checksum calculator",\ - prog " [options...] [--] [files...]", NULL,\ - NULL, 1, 0, args_standard_abbreviations),\ - run(argc, argv, &spec, suffix)) - - -/** - * Print the checksum in binary - */ -#define REPRESENTATION_BINARY 0 +#define RUN(suffix)\ + (run(argc, argv, &spec, suffix)) -/** - * Print the checksum in upper case hexadecimal - */ -#define REPRESENTATION_UPPER_CASE 1 /** - * Print the checksum in lower case hexadecimal + * Message digest representation formats */ -#define REPRESENTATION_LOWER_CASE 2 +enum representation { + /** + * Print the checksum in binary + */ + REPRESENTATION_BINARY, + + /** + * Print the checksum in upper case hexadecimal + */ + REPRESENTATION_UPPER_CASE, + + /** + * Print the checksum in lower case hexadecimal + */ + REPRESENTATION_LOWER_CASE +}; /** @@ -12,4 +12,4 @@ WARN = -pedantic -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dir CFLAGS = -std=c99 -Wall -Wextra $(WARN) -O3 CPPFLAGS = -LDFLAGS = -s -lkeccak -largparser +LDFLAGS = -s -lkeccak diff --git a/keccak-224sum.c b/keccak-224sum.c index f7d3095..c9c2294 100644 --- a/keccak-224sum.c +++ b/keccak-224sum.c @@ -6,6 +6,6 @@ main(int argc, char *argv[]) { libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); - libkeccak_spec_keccak((libkeccak_spec_t *)&spec, 224); - return RUN("Keccak", "keccak-224sum", LIBKECCAK_KECCAK_SUFFIX); + libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 224); + return RUN(""); } diff --git a/keccak-256sum.c b/keccak-256sum.c index f4f78ac..8e780d8 100644 --- a/keccak-256sum.c +++ b/keccak-256sum.c @@ -6,6 +6,6 @@ main(int argc, char *argv[]) { libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); - libkeccak_spec_keccak((libkeccak_spec_t *)&spec, 256); - return RUN("Keccak", "keccak-256sum", LIBKECCAK_KECCAK_SUFFIX); + libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 256); + return RUN(""); } diff --git a/keccak-384sum.c b/keccak-384sum.c index 3a6e3a0..39e74f8 100644 --- a/keccak-384sum.c +++ b/keccak-384sum.c @@ -6,6 +6,6 @@ main(int argc, char *argv[]) { libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); - libkeccak_spec_keccak((libkeccak_spec_t *)&spec, 384); - return RUN("Keccak", "keccak-384sum", LIBKECCAK_KECCAK_SUFFIX); + libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 384); + return RUN(""); } diff --git a/keccak-512sum.c b/keccak-512sum.c index 6ba1041..cd1a302 100644 --- a/keccak-512sum.c +++ b/keccak-512sum.c @@ -6,6 +6,6 @@ main(int argc, char *argv[]) { libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); - libkeccak_spec_keccak((libkeccak_spec_t *)&spec, 512); - return RUN("Keccak", "keccak-512sum", LIBKECCAK_KECCAK_SUFFIX); + libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 512); + return RUN(""); } diff --git a/keccaksum.c b/keccaksum.c index 8fc10a8..54a9cee 100644 --- a/keccaksum.c +++ b/keccaksum.c @@ -6,5 +6,5 @@ main(int argc, char *argv[]) { libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); - return RUN("Keccak", "keccaksum", LIBKECCAK_KECCAK_SUFFIX); + return RUN(""); } diff --git a/rawshake256sum.c b/rawshake256sum.c index 6d6e19a..207d075 100644 --- a/rawshake256sum.c +++ b/rawshake256sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_rawshake((libkeccak_spec_t *)&spec, 256, 256); - return RUN("RawSHAKE", "rawshake256sum", LIBKECCAK_RAWSHAKE_SUFFIX); + return RUN(LIBKECCAK_RAWSHAKE_SUFFIX); } diff --git a/rawshake512sum.c b/rawshake512sum.c index be1d8ac..fce679f 100644 --- a/rawshake512sum.c +++ b/rawshake512sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_rawshake((libkeccak_spec_t *)&spec, 512, 512); - return RUN("RawSHAKE", "rawshake512sum", LIBKECCAK_RAWSHAKE_SUFFIX); + return RUN(LIBKECCAK_RAWSHAKE_SUFFIX); } diff --git a/sha3-224sum.c b/sha3-224sum.c index 15fac82..1a09b85 100644 --- a/sha3-224sum.c +++ b/sha3-224sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 224); - return RUN("SHA-3", "sha3-224sum", LIBKECCAK_SHA3_SUFFIX); + return RUN(LIBKECCAK_SHA3_SUFFIX); } diff --git a/sha3-256sum.c b/sha3-256sum.c index 2273785..6e976d1 100644 --- a/sha3-256sum.c +++ b/sha3-256sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 256); - return RUN("SHA-3", "sha3-256sum", LIBKECCAK_SHA3_SUFFIX); + return RUN(LIBKECCAK_SHA3_SUFFIX); } diff --git a/sha3-384sum.c b/sha3-384sum.c index 2c18df7..200f564 100644 --- a/sha3-384sum.c +++ b/sha3-384sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 384); - return RUN("SHA-3", "sha3-384sum", LIBKECCAK_SHA3_SUFFIX); + return RUN(LIBKECCAK_SHA3_SUFFIX); } diff --git a/sha3-512sum.c b/sha3-512sum.c index a5b40e3..3ae6c54 100644 --- a/sha3-512sum.c +++ b/sha3-512sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_sha3((libkeccak_spec_t *)&spec, 512); - return RUN("SHA-3", "sha3-512sum", LIBKECCAK_SHA3_SUFFIX); + return RUN(LIBKECCAK_SHA3_SUFFIX); } diff --git a/shake256sum.c b/shake256sum.c index 7ab6cc6..ff3af9f 100644 --- a/shake256sum.c +++ b/shake256sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_shake((libkeccak_spec_t *)&spec, 256, 256); - return RUN("SHAKE", "shake256sum", LIBKECCAK_SHAKE_SUFFIX); + return RUN(LIBKECCAK_SHAKE_SUFFIX); } diff --git a/shake512sum.c b/shake512sum.c index 3e24ddb..ad39ab2 100644 --- a/shake512sum.c +++ b/shake512sum.c @@ -7,5 +7,5 @@ main(int argc, char *argv[]) libkeccak_generalised_spec_t spec; libkeccak_generalised_spec_initialise(&spec); libkeccak_spec_shake((libkeccak_spec_t *)&spec, 512, 512); - return RUN("SHAKE", "shake512sum", LIBKECCAK_SHAKE_SUFFIX); + return RUN(LIBKECCAK_SHAKE_SUFFIX); } |