From c55cb366fe86b9b203ac7c7b9c3538b30f91a305 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 13 Nov 2014 06:22:16 +0100 Subject: parse the command line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/common.c | 19 ++++++++++++++----- src/common.h | 3 +++ src/keccaksum.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/common.c b/src/common.c index a1afe28..f75657a 100644 --- a/src/common.c +++ b/src/common.c @@ -42,6 +42,11 @@ +static char* restrict hashsum = NULL; +static char* restrict hexsum = NULL; + + + /** * Print the checksum of a file * @@ -62,8 +67,6 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t* libkeccak_spec_t spec; libkeccak_state_t state; int r, fd; - static char* restrict hashsum = NULL; - static char* restrict hexsum = NULL; size_t length; if (r = libkeccak_degeneralise_spec(gspec, &spec), r) @@ -120,6 +123,9 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t* return USER_ERROR("unknown error in algorithm parameters"); } + if (squeezes <= 0) + return USER_ERROR("the squeeze count be be positive"); + if (verbose) { fprintf(stderr, "rate: %li\n", gspec->bitrate); @@ -175,9 +181,12 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t* } } - free(hashsum); - free(hexsum); - return 0; } + +void cleanup(void) +{ + free(hashsum), hashsum = NULL; + free(hexsum), hexsum = NULL; +} diff --git a/src/common.h b/src/common.h index 36582a8..76cd4da 100644 --- a/src/common.h +++ b/src/common.h @@ -59,5 +59,8 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t* const char* restrict execname); +void cleanup(void); + + #endif diff --git a/src/keccaksum.c b/src/keccaksum.c index 94013e0..f323852 100644 --- a/src/keccaksum.c +++ b/src/keccaksum.c @@ -18,12 +18,56 @@ */ #include "common.h" +#include + +#include + + +#define ADD(arg, desc, ...) \ + args_add_option(args_new_argumented(NULL, arg, 0, __VA_ARGS__, NULL), desc) + int main(int argc, char* argv[]) { libkeccak_generalised_spec_t spec; + long squeezes = 1; + int r, verbose = 1; + size_t i; libkeccak_generalised_spec_initialise(&spec); - return print_checksum(argv[1], &spec, 1, "", REPRESENTATION_UPPER_CASE, 1, argv[0]); + + args_init("Keccak checksum calculator", + "keccaksum [options...] [--] [files...]", NULL, + NULL, 1, 0, args_standard_abbreviations); + + 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"); + /* TODO more options */ + + args_parse(argc, argv); + + /* TODO stricter parsing */ + + if (args_opts_used("-r")) spec.bitrate = atol(*(args_opts_get("-r"))); + if (args_opts_used("-c")) spec.capacity = atol(*(args_opts_get("-c"))); + if (args_opts_used("-n")) spec.output = atol(*(args_opts_get("-n"))); + if (args_opts_used("-s")) spec.state_size = atol(*(args_opts_get("-s"))); + if (args_opts_used("-w")) spec.word_size = atol(*(args_opts_get("-w"))); + if (args_opts_used("-z")) squeezes = atol(*(args_opts_get("-z"))); + + if (args_files_count == 0) + r = print_checksum("-", &spec, squeezes, "", REPRESENTATION_UPPER_CASE, verbose, *argv); + else + for (i = 0; i < args_files_count; i++, verbose = 0) + if ((r = print_checksum(args_files[i], &spec, squeezes, "", REPRESENTATION_UPPER_CASE, verbose, *argv))) + break; + + args_dispose(); + cleanup(); + return r; } -- cgit v1.2.3-70-g09d2