Compare commits

..

6 commits

23 changed files with 312 additions and 136 deletions

View file

@ -5,19 +5,17 @@
#include <libft_ssl.h> #include <libft_ssl.h>
#include <stdint.h> #include <stdint.h>
#define SSL_MAX_STRINGS 64 #define DIGEST_MAX_STRINGS 64
struct ssl_config struct digest_config
{ {
const struct digest_algo *algo; const struct digest_algo *algo;
const char *cmd_name;
uint8_t flags; uint8_t flags;
const char *strings[SSL_MAX_STRINGS]; const char *strings[DIGEST_MAX_STRINGS];
int nb_strings; int nb_strings;
char **files; char **files;
int nb_files; int nb_files;
}; };
enum cli_code cli_parse_arguments(int argc, char **argv, struct ssl_config *config);
int ssl_run(const struct ssl_config *config);
#endif #endif

View file

@ -3,8 +3,6 @@
#include <cli.h> #include <cli.h>
extern const struct option_descriptor g_options[];
int cli_handle_help(const char *arg, void *config); int cli_handle_help(const char *arg, void *config);
int cli_handle_version(const char *arg, void *config); int cli_handle_version(const char *arg, void *config);
int cli_handle_p(const char *arg, void *config); int cli_handle_p(const char *arg, void *config);

View file

@ -1,8 +0,0 @@
#ifndef FT_SSL_INTERACTIVE_H
#define FT_SSL_INTERACTIVE_H
#include <cli.h>
enum cli_code run_interactive(void);
#endif

View file

@ -0,0 +1,26 @@
#ifndef FT_SSL_CMD_COMMAND_H
#define FT_SSL_CMD_COMMAND_H
#include <stddef.h>
#include <stdio.h>
#include <cli.h>
enum command_category {
CMD_DIGEST,
CMD_CIPHER
};
struct command_descriptor {
const char *name;
enum command_category category;
const struct option_descriptor *opts;
size_t nb_opts;
const void *impl;
int (*run)(int argc, char **argv, const struct command_descriptor *self);
};
const struct command_descriptor *find_command(const char *name);
void print_commands(FILE *out);
#endif

View file

@ -0,0 +1,84 @@
#ifndef FT_SSL_CMD_DIGEST_H
#define FT_SSL_CMD_DIGEST_H
#include <getopt.h>
#include <cli.h>
#include "internal/cli/cli_handlers.h"
#include "internal/cmd/command.h"
#define DIGEST_OPTION_LIST \
X( \
'h', \
"help", \
no_argument, \
cli_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit", \
0 \
) \
X( \
'V', \
"version", \
no_argument, \
cli_handle_version, \
OPT_ARG_NONE, \
"Display version information and exit", \
0 \
) \
X( \
'p', \
"stdin", \
no_argument, \
cli_handle_p, \
OPT_ARG_NONE, \
"Echo stdin to stdout and append checksum to output", \
0 \
) \
X( \
'q', \
"quiet", \
no_argument, \
cli_handle_q, \
OPT_ARG_NONE, \
"Quiet mode: only print the digest", \
0 \
) \
X( \
'r', \
"reverse", \
no_argument, \
cli_handle_r, \
OPT_ARG_NONE, \
"Reverse the output format (digest then filename)", \
0 \
) \
X( \
's', \
"string", \
required_argument, \
cli_handle_s, \
OPT_ARG_STRING, \
"Hash a string", \
CLI_OPT_F_REPEATABLE \
)
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) + 1
enum { DIGEST_OPT_LEN = (0 DIGEST_OPTION_LIST) };
#undef X
#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))))
enum { DIGEST_OPTSTR_LEN = (2 DIGEST_OPTION_LIST) };
/* +2 for ':' to disable getopt error messages and \0 */
#undef X
extern const struct option_descriptor g_digest_opts[];
int digest_run(int argc, char **argv,
const struct command_descriptor *self);
#endif

View file

@ -8,12 +8,12 @@
#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, const char *s); int run_string(const struct digest_config *config, const char *s);
int run_file(const struct ssl_config *config, const char *path); int run_file(const struct digest_config *config, const char *path);
int run_stdin(const struct ssl_config *config); int run_stdin(const struct digest_config *config);
int digest_stream(const struct ssl_config *config, int fd, int digest_stream(const struct digest_config *config, int fd,
const char *label, int show_algo, int echo); const char *label, int show_algo, int echo);
void print_digest(const struct ssl_config *config, const uint8_t *digest, void print_digest(const struct digest_config *config, const uint8_t *digest,
const char *label, int show_algo); const char *label, int show_algo);
#endif #endif

@ -1 +1 @@
Subproject commit 27d2c8d21a122aa0c0cb623668657188f9d95444 Subproject commit 7e3c4bd5068ae159d71b5dd8fed9e141dcef60f8

View file

@ -41,15 +41,14 @@ FORCE:
ft_ssl_SOURCES = \ ft_ssl_SOURCES = \
main.c \ main.c \
ssl_run.c \ cmd/command_table.c \
cmd/digest_run.c \
ssl/run_stdin.c \ ssl/run_stdin.c \
ssl/run_string.c \ ssl/run_string.c \
ssl/run_file.c \ ssl/run_file.c \
ssl/digest.c \ ssl/digest.c \
ssl/output.c \ ssl/output.c \
cli/parse.c \
cli/version.c \ cli/version.c \
cli/interactive.c \
cli/handlers/option_map.c \ cli/handlers/option_map.c \
cli/handlers/handle_help.c \ cli/handlers/handle_help.c \
cli/handlers/handle_version.c \ cli/handlers/handle_version.c \

View file

@ -1,15 +1,17 @@
#include <cli.h> #include <cli.h>
#include <stdio.h> #include <stdio.h>
#include "compiler.h" #include "compiler.h"
#include "ft_ssl.h"
#include "internal/cmd/digest_cmd.h"
#include "version_gen.h" #include "version_gen.h"
#include "internal/cli/option.h"
#include "internal/cli/cli_handlers.h"
int int
cli_handle_help(__unused const char *arg, __unused void *config_void) cli_handle_help(__unused const char *arg, void *config_void)
{ {
print_usage(g_prog_name); struct digest_config *config = (struct digest_config *)config_void;
printf("\n");
cli_print_options(g_options, FT_SSL_OPT_LEN, cli_arg_type_to_str); printf("Usage: %s %s [OPTIONS] [FILE...]\n\n", g_prog_name, config->cmd_name);
cli_print_options(g_digest_opts, DIGEST_OPT_LEN, cli_arg_type_to_str);
return CLI_EXIT_SUCCESS; return CLI_EXIT_SUCCESS;
} }

View file

@ -6,7 +6,7 @@
int int
cli_handle_p(__unused const char *arg, void *config_void) cli_handle_p(__unused const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
SET_FLAG(config->flags, FLAG_P); SET_FLAG(config->flags, FLAG_P);
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -6,7 +6,7 @@
int int
cli_handle_q(__unused const char *arg, void *config_void) cli_handle_q(__unused const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
SET_FLAG(config->flags, FLAG_Q); SET_FLAG(config->flags, FLAG_Q);
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -6,7 +6,7 @@
int int
cli_handle_r(__unused const char *arg, void *config_void) cli_handle_r(__unused const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
SET_FLAG(config->flags, FLAG_R); SET_FLAG(config->flags, FLAG_R);
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -4,9 +4,9 @@
int int
cli_handle_s(const char *arg, void *config_void) cli_handle_s(const char *arg, void *config_void)
{ {
struct ssl_config *config = (struct ssl_config *)config_void; struct digest_config *config = (struct digest_config *)config_void;
if (config->nb_strings >= SSL_MAX_STRINGS) if (config->nb_strings >= DIGEST_MAX_STRINGS)
return CLI_ERROR; return CLI_ERROR;
config->strings[config->nb_strings++] = arg; config->strings[config->nb_strings++] = arg;
return CLI_SUCCESS; return CLI_SUCCESS;

View file

@ -1,13 +1,12 @@
#include "internal/cli/cli_handlers.h" #include "internal/cmd/digest_cmd.h"
#include "internal/cli/option.h"
#undef X #undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \ #define X(short_opt, long_opt, has_arg, handler, arg_type, desc, flags) \
{ 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[] = { const struct option_descriptor g_digest_opts[] = {
FT_SSL_OPTION_LIST DIGEST_OPTION_LIST
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL, 0 } {0, NULL, 0, NULL, OPT_ARG_NONE, NULL, 0}
}; };
#undef X #undef X

View file

@ -1,72 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cli.h>
#include <libft_ssl.h>
#include "ft_ssl.h"
#include "internal/cli/interactive.h"
#define INTERACTIVE_BUFSIZ 4096
static const struct digest_algo * const s_algos[] = {
#define DIGEST_ALGO(lower, ds, bs) &g_##lower,
#include <digest_algos.h>
#undef DIGEST_ALGO
NULL
};
static const struct digest_algo *find_algo(const char *name);
static void print_available(void);
enum cli_code
run_interactive(void)
{
char buf[INTERACTIVE_BUFSIZ];
const struct digest_algo *algo;
struct ssl_config config;
while (fgets(buf, (int)sizeof(buf), stdin))
{
buf[strcspn(buf, "\n")] = '\0';
if (buf[0] == '\0')
continue;
algo = find_algo(buf);
if (NULL == algo)
{
fprintf(stderr, "ft_ssl: unknown command -- '%s'\n", buf);
print_available();
continue;
}
memset(&config, 0, sizeof(config));
config.algo = algo;
ssl_run(&config);
}
return CLI_EXIT_SUCCESS;
}
static const struct digest_algo *
find_algo(const char *name)
{
size_t i;
for (i = 0; s_algos[i]; ++i)
{
if (0 == strcmp(s_algos[i]->name, name))
return s_algos[i];
}
return NULL;
}
static void
print_available(void)
{
size_t i;
fprintf(stderr, "available commands:");
for (i = 0; s_algos[i]; ++i)
fprintf(stderr, " %s", s_algos[i]->name);
fprintf(stderr, "\n");
}

54
src/cmd/command_table.c Normal file
View file

@ -0,0 +1,54 @@
#include <stdio.h>
#include <string.h>
#include <libft_ssl.h>
#include "internal/cmd/command.h"
#include "internal/cmd/digest_cmd.h"
/* Forward-declare all digest algo globals */
#define DIGEST_ALGO(lower, ds, bs) extern const struct digest_algo g_##lower;
#include <digest_algos.h>
#undef DIGEST_ALGO
static const struct command_descriptor s_commands[] = {
/* Digest commands */
#define DIGEST_ALGO(lower, ds, bs) \
{ #lower, CMD_DIGEST, g_digest_opts, DIGEST_OPT_LEN, &g_##lower, digest_run },
#include <digest_algos.h>
#undef DIGEST_ALGO
/* Cipher commands (not yet implemented) */
{ "base64", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des-ecb", CMD_CIPHER, NULL, 0, NULL, NULL },
{ "des-cbc", CMD_CIPHER, NULL, 0, NULL, NULL },
/* Sentinel */
{ NULL, 0, NULL, 0, NULL, NULL }
};
const struct command_descriptor *
find_command(const char *name)
{
const struct command_descriptor *cmd;
for (cmd = s_commands; NULL != cmd->name; cmd++)
if (0 == strcmp(cmd->name, name))
return cmd;
return NULL;
}
void
print_commands(FILE *out)
{
const struct command_descriptor *cmd;
fprintf(out, "\nStandard commands:\n");
fprintf(out, "\nMessage Digest commands:\n");
for (cmd = s_commands; NULL != cmd->name; cmd++)
if (CMD_DIGEST == cmd->category)
fprintf(out, " %s\n", cmd->name);
fprintf(out, "\nCipher commands:\n");
for (cmd = s_commands; NULL != cmd->name; cmd++)
if (CMD_CIPHER == cmd->category)
fprintf(out, " %s\n", cmd->name);
}

75
src/cmd/digest_run.c Normal file
View file

@ -0,0 +1,75 @@
#include <getopt.h>
#include <stdio.h>
#include <cli.h>
#include <libft_ssl.h>
#include "compiler.h"
#include "ft_ssl.h"
#include "ft_ssl_flags.h"
#include "internal/cmd/command.h"
#include "internal/cmd/digest_cmd.h"
#include "internal/ssl/ssl.h"
#include "version_gen.h"
/* Forward declarations */
static int check_algo(const struct digest_config *config);
static void update_ret(int *ret, int result);
/* ------------------- */
int
digest_run(int argc, char **argv, const struct command_descriptor *self)
{
struct digest_config config = {0};
char opt_str[DIGEST_OPTSTR_LEN + 1];
struct option long_opts[DIGEST_OPT_LEN + 1];
enum cli_code ret;
int ret_val;
int has_input;
int i;
config.algo = (const struct digest_algo *)self->impl;
config.cmd_name = self->name;
ret = cli_parse(argc, argv, &config, self->opts, self->nb_opts,
opt_str, long_opts);
if (CLI_SUCCESS != ret)
return (int)ret;
config.files = argv + optind;
config.nb_files = argc - optind;
if (CLI_SUCCESS != check_algo(&config))
return (int)CLI_ERROR;
ret_val = (int)CLI_SUCCESS;
has_input = (0 < config.nb_strings || 0 < config.nb_files);
if (HAS_FLAG(config.flags, FLAG_P) || !has_input)
update_ret(&ret_val, run_stdin(&config));
for (i = 0; i < config.nb_strings; i++)
update_ret(&ret_val, run_string(&config, config.strings[i]));
for (i = 0; i < config.nb_files; i++)
update_ret(&ret_val, run_file(&config, config.files[i]));
return ret_val;
}
static int
check_algo(const struct digest_config *config)
{
if (NULL == config->algo->init
|| NULL == config->algo->update
|| NULL == config->algo->final)
{
fprintf(stderr, "%s: %s: not yet implemented\n",
g_prog_name, config->algo->name);
return (int)CLI_ERROR;
}
return (int)CLI_SUCCESS;
}
static void
update_ret(int *ret, int result)
{
if ((int)CLI_ERROR == result)
*ret = (int)CLI_ERROR;
}

View file

@ -1,24 +1,45 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <cli.h> #include <cli.h>
#include "ft_ssl.h" #include "internal/cmd/command.h"
#include "version_gen.h"
char *g_prog_name = NULL; char *g_prog_name = NULL;
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
struct ssl_config config = {0}; const struct command_descriptor *cmd;
int ret = CLI_ERROR; int ret;
g_prog_name = argv[0]; g_prog_name = argv[0];
cli_set_prog_name(g_prog_name); cli_set_prog_name(g_prog_name);
ret = cli_parse_arguments(argc, argv, &config); if (argc < 2)
if (CLI_SUCCESS != ret) {
goto cleanup; print_commands(stderr);
ret = ssl_run(&config); return 2;
}
cleanup: if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help"))
return (CLI_ERROR == ret) ? 2 : EXIT_SUCCESS; {
print_commands(stdout);
return EXIT_SUCCESS;
}
cmd = find_command(argv[1]);
if (NULL == cmd)
{
fprintf(stderr, "%s: Error: %s is an invalid command.\n",
argv[0], argv[1]);
print_commands(stderr);
return 2;
}
if (NULL == cmd->run)
{
fprintf(stderr, "%s: %s: not yet implemented\n", argv[0], argv[1]);
return 2;
}
ret = cmd->run(argc - 1, argv + 1, cmd);
return ((int)CLI_ERROR == ret) ? 2 : EXIT_SUCCESS;
} }

View file

@ -7,12 +7,12 @@
#include "internal/ssl/ssl.h" #include "internal/ssl/ssl.h"
/* Forward declarations */ /* Forward declarations */
static void process_chunk(const struct ssl_config *config, union digest_ctx *ctx, static void process_chunk(const struct digest_config *config, union digest_ctx *ctx,
const uint8_t *buf, size_t n, int echo); const uint8_t *buf, size_t n, int echo);
/* ------------------- */ /* ------------------- */
int int
digest_stream(const struct ssl_config *config, int fd, digest_stream(const struct digest_config *config, int fd,
const char *label, int show_algo, int echo) const char *label, int show_algo, int echo)
{ {
union digest_ctx ctx; union digest_ctx ctx;
@ -31,7 +31,7 @@ digest_stream(const struct ssl_config *config, int fd,
} }
static void static void
process_chunk(const struct ssl_config *config, union digest_ctx *ctx, process_chunk(const struct digest_config *config, union digest_ctx *ctx,
const uint8_t *buf, size_t n, int echo) const uint8_t *buf, size_t n, int echo)
{ {
if (echo) if (echo)

View file

@ -20,7 +20,7 @@ typedef void (*t_formatter)(const char *hex, const char *name_upper,
const char *label); const char *label);
/* Forward declarations */ /* Forward declarations */
static enum fmt_mode resolve_fmt(const struct ssl_config *config, static enum fmt_mode resolve_fmt(const struct digest_config *config,
int show_algo); int show_algo);
static void build_hex(char *buf, const uint8_t *digest, size_t size); static void build_hex(char *buf, const uint8_t *digest, size_t size);
static void build_upper(char *buf, const char *src, size_t size); static void build_upper(char *buf, const char *src, size_t size);
@ -42,7 +42,7 @@ static const t_formatter s_formatters[] = {
}; };
void void
print_digest(const struct ssl_config *config, const uint8_t *digest, print_digest(const struct digest_config *config, const uint8_t *digest,
const char *label, int show_algo) const char *label, int show_algo)
{ {
char hex[MAX_DIGEST_SIZE * 2 + 1]; char hex[MAX_DIGEST_SIZE * 2 + 1];
@ -54,7 +54,7 @@ print_digest(const struct ssl_config *config, const uint8_t *digest,
} }
static enum fmt_mode static enum fmt_mode
resolve_fmt(const struct ssl_config *config, int show_algo) resolve_fmt(const struct digest_config *config, int show_algo)
{ {
if (HAS_FLAG(config->flags, FLAG_Q)) if (HAS_FLAG(config->flags, FLAG_Q))
return FMT_QUIET; return FMT_QUIET;

View file

@ -11,7 +11,7 @@
#include "version_gen.h" #include "version_gen.h"
int int
run_file(const struct ssl_config *config, const char *path) run_file(const struct digest_config *config, const char *path)
{ {
int fd; int fd;
int ret; int ret;

View file

@ -11,13 +11,13 @@
#include "internal/ssl/ssl.h" #include "internal/ssl/ssl.h"
/* Forward declarations */ /* Forward declarations */
static int run_stdin_p(const struct ssl_config *config); static int run_stdin_p(const struct digest_config *config);
static ssize_t read_and_hash(const struct ssl_config *config, union digest_ctx static ssize_t read_and_hash(const struct digest_config *config, union digest_ctx
*ctx, char *label, size_t cap); *ctx, char *label, size_t cap);
/* ------------------- */ /* ------------------- */
int int
run_stdin(const struct ssl_config *config) run_stdin(const struct digest_config *config)
{ {
if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q)) if (HAS_FLAG(config->flags, FLAG_P) && !HAS_FLAG(config->flags, FLAG_Q))
return run_stdin_p(config); return run_stdin_p(config);
@ -26,7 +26,7 @@ run_stdin(const struct ssl_config *config)
} }
static ssize_t static ssize_t
read_and_hash(const struct ssl_config *config, union digest_ctx *ctx, read_and_hash(const struct digest_config *config, union digest_ctx *ctx,
char *label, size_t cap) char *label, size_t cap)
{ {
uint8_t buf[READ_BUFSIZE]; uint8_t buf[READ_BUFSIZE];
@ -47,7 +47,7 @@ read_and_hash(const struct ssl_config *config, union digest_ctx *ctx,
} }
static int static int
run_stdin_p(const struct ssl_config *config) run_stdin_p(const struct digest_config *config)
{ {
union digest_ctx ctx; union digest_ctx ctx;
uint8_t digest[MAX_DIGEST_SIZE]; uint8_t digest[MAX_DIGEST_SIZE];

View file

@ -8,7 +8,7 @@
#include "internal/ssl/ssl.h" #include "internal/ssl/ssl.h"
int int
run_string(const struct ssl_config *config, const char *s) run_string(const struct digest_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];