aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-11-13 06:22:16 +0100
committerMattias Andrée <maandree@operamail.com>2014-11-13 06:22:16 +0100
commitc55cb366fe86b9b203ac7c7b9c3538b30f91a305 (patch)
tree35f9f35e39eaf1fff699b8aa2da0e7768cf00b79 /src
parent...and pave (diff)
downloadsha3sum-c55cb366fe86b9b203ac7c7b9c3538b30f91a305.tar.gz
sha3sum-c55cb366fe86b9b203ac7c7b9c3538b30f91a305.tar.bz2
sha3sum-c55cb366fe86b9b203ac7c7b9c3538b30f91a305.tar.xz
parse the command line
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r--src/common.c19
-rw-r--r--src/common.h3
-rw-r--r--src/keccaksum.c46
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 <stdlib.h>
+
+#include <argparser.h>
+
+
+#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;
}