From d029f1176422c59681124310e674ab0b0bf81670 Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Thu, 8 Sep 2022 23:12:31 -0800 Subject: bindletools: adding -i and -o options to encoding widgets --- doc/mans/bindletools-base16.1.in | 8 ++++ doc/mans/bindletools-base32.1.in | 8 ++++ doc/mans/bindletools-base32hex.1.in | 8 ++++ doc/mans/bindletools-base64.1.in | 8 ++++ doc/mans/bindletools-crockfordbase32.1.in | 8 ++++ doc/mans/bindletools-pctenc.1.in | 8 ++++ src/widget-encodings.c | 67 +++++++++++++++++++++++++------ 7 files changed, 102 insertions(+), 13 deletions(-) diff --git a/doc/mans/bindletools-base16.1.in b/doc/mans/bindletools-base16.1.in index 732d5a0..326ba18 100644 --- a/doc/mans/bindletools-base16.1.in +++ b/doc/mans/bindletools-base16.1.in @@ -64,10 +64,18 @@ encode input data to a base 16 string \fB-h\fR, \fB--help\fR print this usage information and exit +.TP 25 +\fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR +use input file instead of stdin + .TP 25 \fB-n\fR, \fB--newline\fR add newline to end of string +.TP 25 +\fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR +use output file instead of stdout + .TP 25 \fB-p\fR, \fB--nopadding\fR do not add padding to end of encoded string diff --git a/doc/mans/bindletools-base32.1.in b/doc/mans/bindletools-base32.1.in index be739da..0494dc3 100644 --- a/doc/mans/bindletools-base32.1.in +++ b/doc/mans/bindletools-base32.1.in @@ -64,10 +64,18 @@ encode input data as a base 32 string \fB-h\fR, \fB--help\fR print this usage information and exit +.TP 25 +\fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR +use input file instead of stdin + .TP 25 \fB-n\fR, \fB--newline\fR add newline to end of string +.TP 25 +\fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR +use output file instead of stdout + .TP 25 \fB-p\fR, \fB--nopadding\fR do not add padding to end of encoded string diff --git a/doc/mans/bindletools-base32hex.1.in b/doc/mans/bindletools-base32hex.1.in index 090cdf7..949afec 100644 --- a/doc/mans/bindletools-base32hex.1.in +++ b/doc/mans/bindletools-base32hex.1.in @@ -64,10 +64,18 @@ endcode input data as a base32hex string \fB-h\fR, \fB--help\fR print this usage information and exit +.TP 25 +\fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR +use input file instead of stdin + .TP 25 \fB-n\fR, \fB--newline\fR add newline to end of string +.TP 25 +\fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR +use output file instead of stdout + .TP 25 \fB-p\fR, \fB--nopadding\fR do not add padding to end of encoded string diff --git a/doc/mans/bindletools-base64.1.in b/doc/mans/bindletools-base64.1.in index aeec26b..c9dd8bd 100644 --- a/doc/mans/bindletools-base64.1.in +++ b/doc/mans/bindletools-base64.1.in @@ -64,10 +64,18 @@ encode input data as a base64 string \fB-h\fR, \fB--help\fR print this usage information and exit +.TP 25 +\fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR +use input file instead of stdin + .TP 25 \fB-n\fR, \fB--newline\fR add newline to end of string +.TP 25 +\fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR +use output file instead of stdout + .TP 25 \fB-p\fR, \fB--nopadding\fR do not add padding to end of encoded string diff --git a/doc/mans/bindletools-crockfordbase32.1.in b/doc/mans/bindletools-crockfordbase32.1.in index 48acdef..4fd47a5 100644 --- a/doc/mans/bindletools-crockfordbase32.1.in +++ b/doc/mans/bindletools-crockfordbase32.1.in @@ -64,10 +64,18 @@ encode input data as a Crockford's base32 encoded string \fB-h\fR, \fB--help\fR print this usage information and exit +.TP 25 +\fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR +use input file instead of stdin + .TP 25 \fB-n\fR, \fB--newline\fR add newline to end of string +.TP 25 +\fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR +use output file instead of stdout + .TP 25 \fB-p\fR, \fB--nopadding\fR do not add padding to end of encoded string diff --git a/doc/mans/bindletools-pctenc.1.in b/doc/mans/bindletools-pctenc.1.in index e9d7cb4..6d22d90 100644 --- a/doc/mans/bindletools-pctenc.1.in +++ b/doc/mans/bindletools-pctenc.1.in @@ -60,6 +60,10 @@ decode input data as a percent-encoding string \fB-e\fR, \fB--encode\fR encode input data as a percent-encoding string +.TP 25 +\fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR +use input file instead of stdin + .TP 25 \fB-h\fR, \fB--help\fR print this usage information and exit @@ -68,6 +72,10 @@ print this usage information and exit \fB-n\fR, \fB--newline\fR add newline to end of string +.TP 25 +\fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR +use output file instead of stdout + .TP 25 \fB-p\fR, \fB--nopadding\fR do not add padding to end of encoded string diff --git a/src/widget-encodings.c b/src/widget-encodings.c index 09e7a87..7452465 100644 --- a/src/widget-encodings.c +++ b/src/widget-encodings.c @@ -46,6 +46,7 @@ #include #include #include +#include /////////////////// @@ -75,13 +76,23 @@ const char * const bindle_widget_encodings_options[] = { " -d, --decode decode string", " -e, --encode encode string", + " -i file, --input=file use input file instead of stdin", " -n, --newline add newline to output", + " -o file, --output=file use output file instead of stdout", " -p, --nopadding disable padding", " -s string string to encode or decode", NULL }; +#pragma mark bindle_fsin +static int bindle_fdin = STDIN_FILENO; + + +#pragma mark bindle_fsout +static int bindle_fdout = STDOUT_FILENO; + + ////////////////// // // // Prototypes // @@ -173,20 +184,22 @@ bindle_widget_encodings( const char * str; // getopt options - static const char * short_opt = BINDLE_COMMON_SHORT "denps:"; + static const char * short_opt = BINDLE_COMMON_SHORT "dei:no:ps:"; static struct option long_opt[] = { BINDLE_COMMON_LONG, - { "decode", no_argument, NULL, 'd' }, - { "encode", no_argument, NULL, 'e' }, - { "newline", no_argument, NULL, 'n' }, - { "nopadding", no_argument, NULL, 'p' }, + { "decode", no_argument, NULL, 'd' }, + { "encode", no_argument, NULL, 'e' }, + { "input", optional_argument, NULL, 'i' }, + { "newline", no_argument, NULL, 'n' }, + { "nopadding", no_argument, NULL, 'p' }, + { "output", optional_argument, NULL, 'o' }, { NULL, 0, NULL, 0 } }; - optind = 1; - opt_index = 0; - str = NULL; + optind = 1; + opt_index = 0; + str = NULL; while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) { @@ -212,10 +225,38 @@ bindle_widget_encodings( cnf->widget_flags &= ~BINDLE_FLG_DECODE; break; + case 'i': + if (bindle_fdin != STDIN_FILENO) + { + fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); + fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); + return(1); + }; + if ((bindle_fdin = open(optarg, O_RDONLY)) == -1) + { + fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); + return(1); + }; + break; + case 'n': cnf->widget_flags |= BINDLE_FLG_NEWLINE; break; + case 'o': + if (bindle_fdout != STDOUT_FILENO) + { + fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); + fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); + return(1); + }; + if ((bindle_fdout = open(optarg, (O_WRONLY|O_CREAT|O_TRUNC), 0644)) == -1) + { + fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); + return(1); + }; + break; + case 'p': cnf->widget_flags |= BINDLE_FLG_NOPAD; break; @@ -262,7 +303,7 @@ bindle_widget_encodings_stdin( char chunk[BINDLE_BUFF_SIZE+1]; char res[(BINDLE_BUFF_SIZE*3)+1]; - while ((rc = read(STDIN_FILENO, chunk, BINDLE_BUFF_SIZE)) > 0) + while ((rc = read(bindle_fdin, chunk, BINDLE_BUFF_SIZE)) > 0) { chunk[rc] = '\0'; if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE) @@ -270,7 +311,7 @@ bindle_widget_encodings_stdin( else len = bindle_encode(method, res, sizeof(res), chunk, (size_t)rc, (cnf->widget_flags & BINDLE_FLG_NOPAD)); if (len > 0) - write(STDOUT_FILENO, res, (size_t)len); + write(bindle_fdout, res, (size_t)len); }; if (rc == -1) { @@ -281,7 +322,7 @@ bindle_widget_encodings_stdin( if ((cnf->widget_flags & BINDLE_FLG_NEWLINE)) { bindle_strlcpy(res, "\n", sizeof(res)); - write(STDOUT_FILENO, res, strlen(res)); + write(bindle_fdout, res, strlen(res)); }; return(0); @@ -310,7 +351,7 @@ bindle_widget_encodings_string( size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); memcpy(chunk, &str[off], size); if ((rc = bindle_decode(method, res, sizeof(res), chunk, size)) > 0) - write(STDOUT_FILENO, res, (size_t)rc); + write(bindle_fdout, res, (size_t)rc); }; } else @@ -320,7 +361,7 @@ bindle_widget_encodings_string( size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); memcpy(chunk, &str[off], size); if ((rc = bindle_encode(method, res, sizeof(res), chunk, size, (cnf->widget_flags & BINDLE_FLG_NOPAD))) > 0) - write(STDOUT_FILENO, res, (size_t)rc); + write(bindle_fdout, res, (size_t)rc); }; }; -- cgit v1.2.3