aboutsummaryrefslogtreecommitdiffstats
path: root/anysum.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-28 16:42:05 +0200
committerMattias Andrée <maandree@kth.se>2024-08-28 16:42:05 +0200
commita24071ae913b223487df78859c8d830f9e69f580 (patch)
treee2ec712cc29461c82cfdd477e8b1ba961b50018d /anysum.c
parentFirst commit (diff)
downloadanysum-a24071ae913b223487df78859c8d830f9e69f580.tar.gz
anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.bz2
anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.xz
Second commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'anysum.c')
-rw-r--r--anysum.c164
1 files changed, 103 insertions, 61 deletions
diff --git a/anysum.c b/anysum.c
index a478a7a..6853ca5 100644
--- a/anysum.c
+++ b/anysum.c
@@ -1,75 +1,117 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-#include <libsimple-arg.h>
-NUSAGE(2, "[file] ...");
-
-static int
-calculate_and_print(const char *file, struct barrier_group *group, struct global_data *global)
-{
- size_t i;
- if (calculate(file, group, global))
- return -1;
- for (i = 0; i < global->nalgorithms; i++)
- writeall(STDOUT_FILENO, global->algorithms[i].result, global->algorithms[i].result_length, "<stdout>");
- return 0;
-}
-
-
-static int
-calculate_each_and_print(char **files, struct algorithm *algorithms, size_t nalgorithms, enum format format)
-{
- size_t wanted_nalgorithms = nalgorithms;
- struct buffer buffer = {0};
- int r, ret = 0;
- struct barrier_group group;
- struct global_data global;
-
- global.format = format;
- global.buffer = &buffer;
- global.algorithms = algorithms;
- global.nalgorithms = nalgorithms;
-
- createbarriergroup(&group, nalgorithms, &global);
-
- if (!*files) {
- global.file = "-";
- ret = calculate_and_print("-", &group, &global) ? 2 : 0;
- }
- while (*files) {
- global.file = *files;
- r = calculate_and_print(*files++, &group, &global) ? 2 : 0;
- ret = MAX(r, ret);
- }
-
- if (nalgorithms != wanted_nalgorithms)
- ret = 2;
-
- killbarriergroup(&group, &global);
-
- free(buffer.buf);
- return ret;
-}
+static const char *default_algostrs[] = {
+#if defined(SUPPORT_MD2) && 0 /* skipping: compromised is theory */
+ "MD2",
+#endif
+#if defined(SUPPORT_MD4) && 0 /* skipping: compromised */
+ "MD4",
+#endif
+#if defined(SUPPORT_MD5) && 0 /* skipping: compromised */
+ "MD5",
+#endif
+#if defined(SUPPORT_RIPEMD_128) && 1
+ "RIPEMD-128",
+#endif
+#if defined(SUPPORT_RIPEMD_160) && 1
+ "RIPEMD-160",
+#endif
+#if defined(SUPPORT_RIPEMD_256) && 1
+ "RIPEMD-256",
+#endif
+#if defined(SUPPORT_RIPEMD_320) && 1
+ "RIPEMD-320",
+#endif
+#if defined(SUPPORT_SHA0) && 0 /* skipping: compromised */
+ "SHA0",
+#endif
+#if defined(SUPPORT_SHA1) && 0 /* skipping: compromised */
+ "SHA1",
+#endif
+#if defined(SUPPORT_SHA2) && 1
+ "SHA-224",
+ "SHA-256",
+ "SHA-384",
+ "SHA-512",
+ "SHA-512/224",
+ "SHA-512/256",
+#endif
+#if defined(SUPPORT_SHA3) && 1
+ "SHA3-224",
+ "SHA3-256",
+ "SHA3-384",
+ "SHA3-512",
+#endif
+#if defined(SUPPORT_KECCAK) && 1
+ "Keccak-224",
+ "Keccak-256",
+ "Keccak-384",
+ "Keccak-512",
+#endif
+#if defined(SUPPORT_RAWSHAKE) && 1
+ "RawSHAKE128",
+ "RawSHAKE256",
+ "RawSHAKE512",
+#endif
+#if defined(SUPPORT_SHAKE) && 1
+ "SHAKE128",
+ "SHAKE256",
+ "SHAKE512",
+#endif
+#if defined(SUPPORT_BLAKE224) && 1
+ "BLAKE224",
+#endif
+#if defined(SUPPORT_BLAKE256) && 1
+ "BLAKE256",
+#endif
+#if defined(SUPPORT_BLAKE384) && 1
+ "BLAKE384",
+#endif
+#if defined(SUPPORT_BLAKE512) && 1
+ "BLAKE512",
+#endif
+ NULL
+};
int
-main(int argc, char *argv[])
+main(int argc, char **argv)
{
- enum format format = LOWERCASE_HEX | WITH_ALGOSTR | WITH_FILENAME | WITH_LF;
- struct algorithm algorithms[] = {{"sha1"}, {"sha2-512/224"}, {"sha1"}};
- size_t i, nalgorithms = ELEMSOF(algorithms);
+ struct algorithm default_algorithms[ELEMSOF(default_algostrs)];
+ struct config config = {.format = LOWERCASE_HEX | WITH_FILENAME | WITH_LF};
+ char stdin_str[] = "-";
+ char *stdin_array[] = {stdin_str, NULL};
+ size_t i;
int ret;
libsimple_default_failure_exit = 2;
+ cmdline(&argc, &argv, &config);
+ if (!*argv)
+ argv = stdin_array;
+
+ if (config.verify) {
+ ret = verify_checksums(argv, config.algorithms, config.nalgorithms, config.format,
+ config.threads, config.warn_improper_format, config.hexinput);
+ } else {
+ if (!config.nalgorithms) {
+ memset(default_algorithms, 0, sizeof(default_algorithms));
+ for (i = 0; default_algostrs[i]; i++) {
+ default_algorithms[i].algostr = default_algostrs[i];
+ default_algorithms[i].result = NULL;
+ }
+ config.nalgorithms = i;
+ if (!config.nalgorithms) {
+ eprintf("not compiled to support any default hash functions");
+ }
+ }
+ ret = calculate_and_print_each(argv, config.algorithms, config.nalgorithms, config.threads,
+ config.format, config.hexinput, config.recursive);
+ }
- ARGBEGIN {
- default:
- usage();
- } ARGEND;
-
- ret = calculate_each_and_print(argv, algorithms, nalgorithms, format);
- for (i = 0; i < nalgorithms; i++)
- free(algorithms[i].result);
+ for (i = 0; i < config.nalgorithms; i++)
+ free(config.algorithms[i].result);
+ free(config.algorithms);
return ret;
}