feat: support multiple -s strings (up to 64)
This commit is contained in:
parent
be9b6e79df
commit
46ec3184cd
8 changed files with 32 additions and 22 deletions
|
|
@ -5,11 +5,14 @@
|
||||||
#include <libft_ssl.h>
|
#include <libft_ssl.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define SSL_MAX_STRINGS 64
|
||||||
|
|
||||||
struct ssl_config
|
struct ssl_config
|
||||||
{
|
{
|
||||||
const struct digest_algo *algo;
|
const struct digest_algo *algo;
|
||||||
uint8_t flags;
|
uint8_t flags;
|
||||||
const char *string;
|
const char *strings[SSL_MAX_STRINGS];
|
||||||
|
int nb_strings;
|
||||||
char **files;
|
char **files;
|
||||||
int nb_files;
|
int nb_files;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_help, \
|
cli_handle_help, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Display this help and exit" \
|
"Display this help and exit", \
|
||||||
|
0 \
|
||||||
) \
|
) \
|
||||||
X( \
|
X( \
|
||||||
'V', \
|
'V', \
|
||||||
|
|
@ -18,7 +19,8 @@
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_version, \
|
cli_handle_version, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Display version information and exit" \
|
"Display version information and exit", \
|
||||||
|
0 \
|
||||||
) \
|
) \
|
||||||
X( \
|
X( \
|
||||||
'p', \
|
'p', \
|
||||||
|
|
@ -26,7 +28,8 @@
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_p, \
|
cli_handle_p, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Echo stdin to stdout and append checksum to output" \
|
"Echo stdin to stdout and append checksum to output", \
|
||||||
|
0 \
|
||||||
) \
|
) \
|
||||||
X( \
|
X( \
|
||||||
'q', \
|
'q', \
|
||||||
|
|
@ -34,7 +37,8 @@
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_q, \
|
cli_handle_q, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Quiet mode: only print the digest" \
|
"Quiet mode: only print the digest", \
|
||||||
|
0 \
|
||||||
) \
|
) \
|
||||||
X( \
|
X( \
|
||||||
'r', \
|
'r', \
|
||||||
|
|
@ -42,7 +46,8 @@
|
||||||
no_argument, \
|
no_argument, \
|
||||||
cli_handle_r, \
|
cli_handle_r, \
|
||||||
OPT_ARG_NONE, \
|
OPT_ARG_NONE, \
|
||||||
"Reverse the output format (digest then filename)" \
|
"Reverse the output format (digest then filename)", \
|
||||||
|
0 \
|
||||||
) \
|
) \
|
||||||
X( \
|
X( \
|
||||||
's', \
|
's', \
|
||||||
|
|
@ -50,15 +55,16 @@
|
||||||
required_argument, \
|
required_argument, \
|
||||||
cli_handle_s, \
|
cli_handle_s, \
|
||||||
OPT_ARG_STRING, \
|
OPT_ARG_STRING, \
|
||||||
"Hash a string" \
|
"Hash a string", \
|
||||||
|
CLI_OPT_F_REPEATABLE \
|
||||||
)
|
)
|
||||||
|
|
||||||
#undef X
|
#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 ) };
|
enum { FT_SSL_OPT_LEN = (0 FT_SSL_OPTION_LIST ) };
|
||||||
|
|
||||||
#undef X
|
#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) \
|
+ (1 * (has_arg == no_argument) \
|
||||||
+ (2 * (has_arg == required_argument) \
|
+ (2 * (has_arg == required_argument) \
|
||||||
+ (3 * (has_arg == optional_argument))))
|
+ (3 * (has_arg == optional_argument))))
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
#define MAX_DIGEST_SIZE 64
|
#define MAX_DIGEST_SIZE 64
|
||||||
#define READ_BUFSIZE 4096
|
#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_file(const struct ssl_config *config, const char *path);
|
||||||
int run_stdin(const struct ssl_config *config);
|
int run_stdin(const struct ssl_config *config);
|
||||||
int digest_stream(const struct ssl_config *config, int fd,
|
int digest_stream(const struct ssl_config *config, int fd,
|
||||||
|
|
|
||||||
2
libcli
2
libcli
|
|
@ -1 +1 @@
|
||||||
Subproject commit 17ba957ed647364f8f21e43ebf2445410dc5c494
|
Subproject commit a1b7a3620487d5c7b8a2178794cb8c5cb30d07f2
|
||||||
|
|
@ -6,6 +6,8 @@ cli_handle_s(const char *arg, void *config_void)
|
||||||
{
|
{
|
||||||
struct ssl_config *config = (struct ssl_config *)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;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@
|
||||||
#include "internal/cli/option.h"
|
#include "internal/cli/option.h"
|
||||||
|
|
||||||
#undef X
|
#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) \
|
||||||
{ short_opt, long_opt, has_arg, handler, arg_type, desc },
|
{ short_opt, long_opt, has_arg, handler, arg_type, desc, flags },
|
||||||
|
|
||||||
const struct option_descriptor g_options[] = {
|
const struct option_descriptor g_options[] = {
|
||||||
FT_SSL_OPTION_LIST
|
FT_SSL_OPTION_LIST
|
||||||
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL }
|
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef X
|
#undef X
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,16 @@
|
||||||
#include "internal/ssl/ssl.h"
|
#include "internal/ssl/ssl.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
run_string(const struct ssl_config *config)
|
run_string(const struct ssl_config *config, const char *s)
|
||||||
{
|
{
|
||||||
union digest_ctx ctx;
|
union digest_ctx ctx;
|
||||||
uint8_t digest[MAX_DIGEST_SIZE];
|
uint8_t digest[MAX_DIGEST_SIZE];
|
||||||
char label[4096];
|
char label[4096];
|
||||||
|
|
||||||
config->algo->init(&ctx);
|
config->algo->init(&ctx);
|
||||||
config->algo->update(&ctx, (const uint8_t *)config->string,
|
config->algo->update(&ctx, (const uint8_t *)s, strlen(s));
|
||||||
strlen(config->string));
|
|
||||||
config->algo->final(&ctx, digest);
|
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);
|
print_digest(config, digest, label, 1);
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,11 @@ ssl_run(const struct ssl_config *config)
|
||||||
if (CLI_SUCCESS != check_algo(config))
|
if (CLI_SUCCESS != check_algo(config))
|
||||||
return CLI_ERROR;
|
return CLI_ERROR;
|
||||||
ret = CLI_SUCCESS;
|
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)
|
if (HAS_FLAG(config->flags, FLAG_P) || !has_input)
|
||||||
update_ret(&ret, run_stdin(config));
|
update_ret(&ret, run_stdin(config));
|
||||||
if (NULL != config->string)
|
for (i = 0; i < config->nb_strings; i++)
|
||||||
update_ret(&ret, run_string(config));
|
update_ret(&ret, run_string(config, config->strings[i]));
|
||||||
for (i = 0; i < config->nb_files; i++)
|
for (i = 0; i < config->nb_files; i++)
|
||||||
update_ret(&ret, run_file(config, config->files[i]));
|
update_ret(&ret, run_file(config, config->files[i]));
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue