aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-11-13 06:49:00 +0100
committerMattias Andrée <maandree@operamail.com>2014-11-13 06:49:00 +0100
commitae04fb053eebfe53924b095b774dfc5b9cd88e4c (patch)
treef91094406f1729d44ede60171a0bda5067055b59
parentparse the command line (diff)
downloadsha3sum-ae04fb053eebfe53924b095b774dfc5b9cd88e4c.tar.gz
sha3sum-ae04fb053eebfe53924b095b774dfc5b9cd88e4c.tar.bz2
sha3sum-ae04fb053eebfe53924b095b774dfc5b9cd88e4c.tar.xz
misc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/common.c75
-rw-r--r--src/common.h27
-rw-r--r--src/keccaksum.c45
3 files changed, 100 insertions, 47 deletions
diff --git a/src/common.c b/src/common.c
index f75657a..425db10 100644
--- a/src/common.c
+++ b/src/common.c
@@ -41,8 +41,20 @@
(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))
+
+
+/**
+ * Storage for binary hash
+ */
static char* restrict hashsum = NULL;
+
+/**
+ * Storage for hexadecimal hash
+ */
static char* restrict hexsum = NULL;
@@ -172,7 +184,7 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t*
size_t ptr = 0;
ssize_t wrote;
fflush(stdout);
- for (;;)
+ while (length - ptr)
{
wrote = write(STDOUT_FILENO, hashsum, length - ptr);
if (wrote <= 0)
@@ -185,8 +197,67 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t*
}
-void cleanup(void)
+/**
+ * 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`
+ * @param argv The second argument from `main`
+ * @param spec The default algorithm parameters
+ * @param suffix Message suffix
+ * @return An appropriate exit value
+ */
+int run(int argc, char* argv[], libkeccak_generalised_spec_t* restrict spec, const char* restrict suffix)
+{
+ int r, verbose = 0, presentation = REPRESENTATION_UPPER_CASE;
+ long squeezes = 1;
+ size_t i;
+
+ 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, "Be verbose", "-V", "--verbose");
+
+ args_parse(argc, argv);
+
+ /* TODO stricter parsing */
+ if (args_opts_used("-h")) return args_help(0), 0;
+ if (args_opts_used("-r")) spec->bitrate = atol(args_opts_get("-r")[0]);
+ if (args_opts_used("-c")) spec->capacity = atol(args_opts_get("-c")[0]);
+ if (args_opts_used("-n")) spec->output = atol(args_opts_get("-n")[0]);
+ if (args_opts_used("-s")) spec->state_size = atol(args_opts_get("-s")[0]);
+ if (args_opts_used("-w")) spec->word_size = atol(args_opts_get("-w")[0]);
+ if (args_opts_used("-z")) squeezes = atol(args_opts_get("-z")[0]);
+ 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("-V")) verbose = 1;
+
+ if (args_files_count == 0)
+ r = print_checksum("-", spec, squeezes, suffix, presentation, verbose, *argv);
+ else
+ for (i = 0; i < args_files_count; i++, verbose = 0)
+ if ((r = print_checksum(args_files[i], spec, squeezes, suffix, presentation, verbose, *argv)))
+ break;
+
+ args_dispose();
+ cleanup();
+ return r;
+}
+
diff --git a/src/common.h b/src/common.h
index 76cd4da..7c35e3c 100644
--- a/src/common.h
+++ b/src/common.h
@@ -21,6 +21,22 @@
#include <libkeccak.h>
+#include <argparser.h>
+
+
+
+/**
+ * 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))
@@ -59,7 +75,16 @@ int print_checksum(const char* restrict filename, libkeccak_generalised_spec_t*
const char* restrict execname);
-void cleanup(void);
+/**
+ * Parse the command line and calculate the hashes of the selected files
+ *
+ * @param argc The first argument from `main`
+ * @param argv The second argument from `main`
+ * @param spec The default algorithm parameters
+ * @param suffix Message suffix
+ * @return An appropriate exit value
+ */
+int run(int argc, char* argv[], libkeccak_generalised_spec_t* restrict spec, const char* restrict suffix);
#endif
diff --git a/src/keccaksum.c b/src/keccaksum.c
index f323852..594fe77 100644
--- a/src/keccaksum.c
+++ b/src/keccaksum.c
@@ -18,56 +18,13 @@
*/
#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);
- 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;
+ return RUN("Keccak", "keccaksum", "");
}