feat: support multiple -s strings (up to 64)

This commit is contained in:
lohhiiccc 2026-06-10 02:08:54 +02:00
parent be9b6e79df
commit 46ec3184cd
8 changed files with 32 additions and 22 deletions

View file

@ -5,11 +5,14 @@
#include <libft_ssl.h>
#include <stdint.h>
#define SSL_MAX_STRINGS 64
struct ssl_config
{
const struct digest_algo *algo;
uint8_t flags;
const char *string;
const char *strings[SSL_MAX_STRINGS];
int nb_strings;
char **files;
int nb_files;
};

View file

@ -10,7 +10,8 @@
no_argument, \
cli_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit" \
"Display this help and exit", \
0 \
) \
X( \
'V', \
@ -18,7 +19,8 @@
no_argument, \
cli_handle_version, \
OPT_ARG_NONE, \
"Display version information and exit" \
"Display version information and exit", \
0 \
) \
X( \
'p', \
@ -26,7 +28,8 @@
no_argument, \
cli_handle_p, \
OPT_ARG_NONE, \
"Echo stdin to stdout and append checksum to output" \
"Echo stdin to stdout and append checksum to output", \
0 \
) \
X( \
'q', \
@ -34,7 +37,8 @@
no_argument, \
cli_handle_q, \
OPT_ARG_NONE, \
"Quiet mode: only print the digest" \
"Quiet mode: only print the digest", \
0 \
) \
X( \
'r', \
@ -42,7 +46,8 @@
no_argument, \
cli_handle_r, \
OPT_ARG_NONE, \
"Reverse the output format (digest then filename)" \
"Reverse the output format (digest then filename)", \
0 \
) \
X( \
's', \
@ -50,15 +55,16 @@
required_argument, \
cli_handle_s, \
OPT_ARG_STRING, \
"Hash a string" \
"Hash a string", \
CLI_OPT_F_REPEATABLE \
)
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) + 1
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
enum { FT_SSL_OPT_LEN = (0 FT_SSL_OPTION_LIST ) };
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
+ (1 * (has_arg == no_argument) \
+ (2 * (has_arg == required_argument) \
+ (3 * (has_arg == optional_argument))))

View file

@ -8,7 +8,7 @@
#define MAX_DIGEST_SIZE 64
#define READ_BUFSIZE 4096
int run_string(const struct ssl_config *config);
int run_string(const struct ssl_config *config, const char *s);
int run_file(const struct ssl_config *config, const char *path);
int run_stdin(const struct ssl_config *config);
int digest_stream(const struct ssl_config *config, int fd,

2
libcli

@ -1 +1 @@
Subproject commit 17ba957ed647364f8f21e43ebf2445410dc5c494
Subproject commit a1b7a3620487d5c7b8a2178794cb8c5cb30d07f2

View file

@ -6,6 +6,8 @@ cli_handle_s(const char *arg, void *config_void)
{
struct ssl_config *config = (struct ssl_config *)config_void;
config->string = arg;
if (config->nb_strings >= SSL_MAX_STRINGS)
return CLI_ERROR;
config->strings[config->nb_strings++] = arg;
return CLI_SUCCESS;
}

View file

@ -2,12 +2,12 @@
#include "internal/cli/option.h"
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \
{ short_opt, long_opt, has_arg, handler, arg_type, desc },
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
{ short_opt, long_opt, has_arg, handler, arg_type, desc, flags },
const struct option_descriptor g_options[] = {
FT_SSL_OPTION_LIST
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL }
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL, 0 }
};
#undef X

View file

@ -8,17 +8,16 @@
#include "internal/ssl/ssl.h"
int
run_string(const struct ssl_config *config)
run_string(const struct ssl_config *config, const char *s)
{
union digest_ctx ctx;
uint8_t digest[MAX_DIGEST_SIZE];
char label[4096];
config->algo->init(&ctx);
config->algo->update(&ctx, (const uint8_t *)config->string,
strlen(config->string));
config->algo->update(&ctx, (const uint8_t *)s, strlen(s));
config->algo->final(&ctx, digest);
(void)snprintf(label, sizeof(label), "\"%s\"", config->string);
(void)snprintf(label, sizeof(label), "\"%s\"", s);
print_digest(config, digest, label, 1);
return CLI_SUCCESS;
}

View file

@ -24,11 +24,11 @@ ssl_run(const struct ssl_config *config)
if (CLI_SUCCESS != check_algo(config))
return CLI_ERROR;
ret = CLI_SUCCESS;
has_input = (NULL != config->string || 0 < config->nb_files);
has_input = (0 < config->nb_strings || 0 < config->nb_files);
if (HAS_FLAG(config->flags, FLAG_P) || !has_input)
update_ret(&ret, run_stdin(config));
if (NULL != config->string)
update_ret(&ret, run_string(config));
for (i = 0; i < config->nb_strings; i++)
update_ret(&ret, run_string(config, config->strings[i]));
for (i = 0; i < config->nb_files; i++)
update_ret(&ret, run_file(config, config->files[i]));
return ret;