diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-09-08 23:12:31 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-09-08 23:25:32 -0800 |
| commit | d029f1176422c59681124310e674ab0b0bf81670 (patch) | |
| tree | 8042c1752b3dcaf140cd80e62d943c60af09f01f | |
| parent | 0f9ce647dda3c59cc9ac26c63d26477522f4b5fc (diff) | |
| download | bindletools-d029f1176422c59681124310e674ab0b0bf81670.tar.gz bindletools-d029f1176422c59681124310e674ab0b0bf81670.tar.bz2 bindletools-d029f1176422c59681124310e674ab0b0bf81670.tar.xz bindletools-d029f1176422c59681124310e674ab0b0bf81670.zip | |
bindletools: adding -i and -o options to encoding widgets
| -rw-r--r-- | doc/mans/bindletools-base16.1.in | 8 | ||||
| -rw-r--r-- | doc/mans/bindletools-base32.1.in | 8 | ||||
| -rw-r--r-- | doc/mans/bindletools-base32hex.1.in | 8 | ||||
| -rw-r--r-- | doc/mans/bindletools-base64.1.in | 8 | ||||
| -rw-r--r-- | doc/mans/bindletools-crockfordbase32.1.in | 8 | ||||
| -rw-r--r-- | doc/mans/bindletools-pctenc.1.in | 8 | ||||
| -rw-r--r-- | 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 | |||
| @@ -65,10 +65,18 @@ encode input data to a base 16 string | |||
| 65 | print this usage information and exit | 65 | print this usage information and exit |
| 66 | 66 | ||
| 67 | .TP 25 | 67 | .TP 25 |
| 68 | \fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR | ||
| 69 | use input file instead of stdin | ||
| 70 | |||
| 71 | .TP 25 | ||
| 68 | \fB-n\fR, \fB--newline\fR | 72 | \fB-n\fR, \fB--newline\fR |
| 69 | add newline to end of string | 73 | add newline to end of string |
| 70 | 74 | ||
| 71 | .TP 25 | 75 | .TP 25 |
| 76 | \fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR | ||
| 77 | use output file instead of stdout | ||
| 78 | |||
| 79 | .TP 25 | ||
| 72 | \fB-p\fR, \fB--nopadding\fR | 80 | \fB-p\fR, \fB--nopadding\fR |
| 73 | do not add padding to end of encoded string | 81 | do not add padding to end of encoded string |
| 74 | 82 | ||
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 | |||
| @@ -65,10 +65,18 @@ encode input data as a base 32 string | |||
| 65 | print this usage information and exit | 65 | print this usage information and exit |
| 66 | 66 | ||
| 67 | .TP 25 | 67 | .TP 25 |
| 68 | \fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR | ||
| 69 | use input file instead of stdin | ||
| 70 | |||
| 71 | .TP 25 | ||
| 68 | \fB-n\fR, \fB--newline\fR | 72 | \fB-n\fR, \fB--newline\fR |
| 69 | add newline to end of string | 73 | add newline to end of string |
| 70 | 74 | ||
| 71 | .TP 25 | 75 | .TP 25 |
| 76 | \fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR | ||
| 77 | use output file instead of stdout | ||
| 78 | |||
| 79 | .TP 25 | ||
| 72 | \fB-p\fR, \fB--nopadding\fR | 80 | \fB-p\fR, \fB--nopadding\fR |
| 73 | do not add padding to end of encoded string | 81 | do not add padding to end of encoded string |
| 74 | 82 | ||
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 | |||
| @@ -65,10 +65,18 @@ endcode input data as a base32hex string | |||
| 65 | print this usage information and exit | 65 | print this usage information and exit |
| 66 | 66 | ||
| 67 | .TP 25 | 67 | .TP 25 |
| 68 | \fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR | ||
| 69 | use input file instead of stdin | ||
| 70 | |||
| 71 | .TP 25 | ||
| 68 | \fB-n\fR, \fB--newline\fR | 72 | \fB-n\fR, \fB--newline\fR |
| 69 | add newline to end of string | 73 | add newline to end of string |
| 70 | 74 | ||
| 71 | .TP 25 | 75 | .TP 25 |
| 76 | \fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR | ||
| 77 | use output file instead of stdout | ||
| 78 | |||
| 79 | .TP 25 | ||
| 72 | \fB-p\fR, \fB--nopadding\fR | 80 | \fB-p\fR, \fB--nopadding\fR |
| 73 | do not add padding to end of encoded string | 81 | do not add padding to end of encoded string |
| 74 | 82 | ||
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 | |||
| @@ -65,10 +65,18 @@ encode input data as a base64 string | |||
| 65 | print this usage information and exit | 65 | print this usage information and exit |
| 66 | 66 | ||
| 67 | .TP 25 | 67 | .TP 25 |
| 68 | \fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR | ||
| 69 | use input file instead of stdin | ||
| 70 | |||
| 71 | .TP 25 | ||
| 68 | \fB-n\fR, \fB--newline\fR | 72 | \fB-n\fR, \fB--newline\fR |
| 69 | add newline to end of string | 73 | add newline to end of string |
| 70 | 74 | ||
| 71 | .TP 25 | 75 | .TP 25 |
| 76 | \fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR | ||
| 77 | use output file instead of stdout | ||
| 78 | |||
| 79 | .TP 25 | ||
| 72 | \fB-p\fR, \fB--nopadding\fR | 80 | \fB-p\fR, \fB--nopadding\fR |
| 73 | do not add padding to end of encoded string | 81 | do not add padding to end of encoded string |
| 74 | 82 | ||
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 | |||
| @@ -65,10 +65,18 @@ encode input data as a Crockford's base32 encoded string | |||
| 65 | print this usage information and exit | 65 | print this usage information and exit |
| 66 | 66 | ||
| 67 | .TP 25 | 67 | .TP 25 |
| 68 | \fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR | ||
| 69 | use input file instead of stdin | ||
| 70 | |||
| 71 | .TP 25 | ||
| 68 | \fB-n\fR, \fB--newline\fR | 72 | \fB-n\fR, \fB--newline\fR |
| 69 | add newline to end of string | 73 | add newline to end of string |
| 70 | 74 | ||
| 71 | .TP 25 | 75 | .TP 25 |
| 76 | \fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR | ||
| 77 | use output file instead of stdout | ||
| 78 | |||
| 79 | .TP 25 | ||
| 72 | \fB-p\fR, \fB--nopadding\fR | 80 | \fB-p\fR, \fB--nopadding\fR |
| 73 | do not add padding to end of encoded string | 81 | do not add padding to end of encoded string |
| 74 | 82 | ||
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 | |||
| @@ -61,6 +61,10 @@ decode input data as a percent-encoding string | |||
| 61 | encode input data as a percent-encoding string | 61 | encode input data as a percent-encoding string |
| 62 | 62 | ||
| 63 | .TP 25 | 63 | .TP 25 |
| 64 | \fB-i\fR \fIinput\fR, \fB--input\fR=\fIfile\fR | ||
| 65 | use input file instead of stdin | ||
| 66 | |||
| 67 | .TP 25 | ||
| 64 | \fB-h\fR, \fB--help\fR | 68 | \fB-h\fR, \fB--help\fR |
| 65 | print this usage information and exit | 69 | print this usage information and exit |
| 66 | 70 | ||
| @@ -69,6 +73,10 @@ print this usage information and exit | |||
| 69 | add newline to end of string | 73 | add newline to end of string |
| 70 | 74 | ||
| 71 | .TP 25 | 75 | .TP 25 |
| 76 | \fB-o\fR \fIinput\fR, \fB--output\fR=\fIfile\fR | ||
| 77 | use output file instead of stdout | ||
| 78 | |||
| 79 | .TP 25 | ||
| 72 | \fB-p\fR, \fB--nopadding\fR | 80 | \fB-p\fR, \fB--nopadding\fR |
| 73 | do not add padding to end of encoded string | 81 | do not add padding to end of encoded string |
| 74 | 82 | ||
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 @@ | |||
| 46 | #include <sys/uio.h> | 46 | #include <sys/uio.h> |
| 47 | #include <unistd.h> | 47 | #include <unistd.h> |
| 48 | #include <errno.h> | 48 | #include <errno.h> |
| 49 | #include <fcntl.h> | ||
| 49 | 50 | ||
| 50 | 51 | ||
| 51 | /////////////////// | 52 | /////////////////// |
| @@ -75,13 +76,23 @@ const char * const bindle_widget_encodings_options[] = | |||
| 75 | { | 76 | { |
| 76 | " -d, --decode decode string", | 77 | " -d, --decode decode string", |
| 77 | " -e, --encode encode string", | 78 | " -e, --encode encode string", |
| 79 | " -i file, --input=file use input file instead of stdin", | ||
| 78 | " -n, --newline add newline to output", | 80 | " -n, --newline add newline to output", |
| 81 | " -o file, --output=file use output file instead of stdout", | ||
| 79 | " -p, --nopadding disable padding", | 82 | " -p, --nopadding disable padding", |
| 80 | " -s string string to encode or decode", | 83 | " -s string string to encode or decode", |
| 81 | NULL | 84 | NULL |
| 82 | }; | 85 | }; |
| 83 | 86 | ||
| 84 | 87 | ||
| 88 | #pragma mark bindle_fsin | ||
| 89 | static int bindle_fdin = STDIN_FILENO; | ||
| 90 | |||
| 91 | |||
| 92 | #pragma mark bindle_fsout | ||
| 93 | static int bindle_fdout = STDOUT_FILENO; | ||
| 94 | |||
| 95 | |||
| 85 | ////////////////// | 96 | ////////////////// |
| 86 | // // | 97 | // // |
| 87 | // Prototypes // | 98 | // Prototypes // |
| @@ -173,20 +184,22 @@ bindle_widget_encodings( | |||
| 173 | const char * str; | 184 | const char * str; |
| 174 | 185 | ||
| 175 | // getopt options | 186 | // getopt options |
| 176 | static const char * short_opt = BINDLE_COMMON_SHORT "denps:"; | 187 | static const char * short_opt = BINDLE_COMMON_SHORT "dei:no:ps:"; |
| 177 | static struct option long_opt[] = | 188 | static struct option long_opt[] = |
| 178 | { | 189 | { |
| 179 | BINDLE_COMMON_LONG, | 190 | BINDLE_COMMON_LONG, |
| 180 | { "decode", no_argument, NULL, 'd' }, | 191 | { "decode", no_argument, NULL, 'd' }, |
| 181 | { "encode", no_argument, NULL, 'e' }, | 192 | { "encode", no_argument, NULL, 'e' }, |
| 182 | { "newline", no_argument, NULL, 'n' }, | 193 | { "input", optional_argument, NULL, 'i' }, |
| 183 | { "nopadding", no_argument, NULL, 'p' }, | 194 | { "newline", no_argument, NULL, 'n' }, |
| 195 | { "nopadding", no_argument, NULL, 'p' }, | ||
| 196 | { "output", optional_argument, NULL, 'o' }, | ||
| 184 | { NULL, 0, NULL, 0 } | 197 | { NULL, 0, NULL, 0 } |
| 185 | }; | 198 | }; |
| 186 | 199 | ||
| 187 | optind = 1; | 200 | optind = 1; |
| 188 | opt_index = 0; | 201 | opt_index = 0; |
| 189 | str = NULL; | 202 | str = NULL; |
| 190 | 203 | ||
| 191 | while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) | 204 | while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) |
| 192 | { | 205 | { |
| @@ -212,10 +225,38 @@ bindle_widget_encodings( | |||
| 212 | cnf->widget_flags &= ~BINDLE_FLG_DECODE; | 225 | cnf->widget_flags &= ~BINDLE_FLG_DECODE; |
| 213 | break; | 226 | break; |
| 214 | 227 | ||
| 228 | case 'i': | ||
| 229 | if (bindle_fdin != STDIN_FILENO) | ||
| 230 | { | ||
| 231 | fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); | ||
| 232 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 233 | return(1); | ||
| 234 | }; | ||
| 235 | if ((bindle_fdin = open(optarg, O_RDONLY)) == -1) | ||
| 236 | { | ||
| 237 | fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); | ||
| 238 | return(1); | ||
| 239 | }; | ||
| 240 | break; | ||
| 241 | |||
| 215 | case 'n': | 242 | case 'n': |
| 216 | cnf->widget_flags |= BINDLE_FLG_NEWLINE; | 243 | cnf->widget_flags |= BINDLE_FLG_NEWLINE; |
| 217 | break; | 244 | break; |
| 218 | 245 | ||
| 246 | case 'o': | ||
| 247 | if (bindle_fdout != STDOUT_FILENO) | ||
| 248 | { | ||
| 249 | fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); | ||
| 250 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 251 | return(1); | ||
| 252 | }; | ||
| 253 | if ((bindle_fdout = open(optarg, (O_WRONLY|O_CREAT|O_TRUNC), 0644)) == -1) | ||
| 254 | { | ||
| 255 | fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); | ||
| 256 | return(1); | ||
| 257 | }; | ||
| 258 | break; | ||
| 259 | |||
| 219 | case 'p': | 260 | case 'p': |
| 220 | cnf->widget_flags |= BINDLE_FLG_NOPAD; | 261 | cnf->widget_flags |= BINDLE_FLG_NOPAD; |
| 221 | break; | 262 | break; |
| @@ -262,7 +303,7 @@ bindle_widget_encodings_stdin( | |||
| 262 | char chunk[BINDLE_BUFF_SIZE+1]; | 303 | char chunk[BINDLE_BUFF_SIZE+1]; |
| 263 | char res[(BINDLE_BUFF_SIZE*3)+1]; | 304 | char res[(BINDLE_BUFF_SIZE*3)+1]; |
| 264 | 305 | ||
| 265 | while ((rc = read(STDIN_FILENO, chunk, BINDLE_BUFF_SIZE)) > 0) | 306 | while ((rc = read(bindle_fdin, chunk, BINDLE_BUFF_SIZE)) > 0) |
| 266 | { | 307 | { |
| 267 | chunk[rc] = '\0'; | 308 | chunk[rc] = '\0'; |
| 268 | if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE) | 309 | if ((cnf->widget_flags & BINDLE_FLG_DECODE) == BINDLE_FLG_DECODE) |
| @@ -270,7 +311,7 @@ bindle_widget_encodings_stdin( | |||
| 270 | else | 311 | else |
| 271 | len = bindle_encode(method, res, sizeof(res), chunk, (size_t)rc, (cnf->widget_flags & BINDLE_FLG_NOPAD)); | 312 | len = bindle_encode(method, res, sizeof(res), chunk, (size_t)rc, (cnf->widget_flags & BINDLE_FLG_NOPAD)); |
| 272 | if (len > 0) | 313 | if (len > 0) |
| 273 | write(STDOUT_FILENO, res, (size_t)len); | 314 | write(bindle_fdout, res, (size_t)len); |
| 274 | }; | 315 | }; |
| 275 | if (rc == -1) | 316 | if (rc == -1) |
| 276 | { | 317 | { |
| @@ -281,7 +322,7 @@ bindle_widget_encodings_stdin( | |||
| 281 | if ((cnf->widget_flags & BINDLE_FLG_NEWLINE)) | 322 | if ((cnf->widget_flags & BINDLE_FLG_NEWLINE)) |
| 282 | { | 323 | { |
| 283 | bindle_strlcpy(res, "\n", sizeof(res)); | 324 | bindle_strlcpy(res, "\n", sizeof(res)); |
| 284 | write(STDOUT_FILENO, res, strlen(res)); | 325 | write(bindle_fdout, res, strlen(res)); |
| 285 | }; | 326 | }; |
| 286 | 327 | ||
| 287 | return(0); | 328 | return(0); |
| @@ -310,7 +351,7 @@ bindle_widget_encodings_string( | |||
| 310 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); | 351 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); |
| 311 | memcpy(chunk, &str[off], size); | 352 | memcpy(chunk, &str[off], size); |
| 312 | if ((rc = bindle_decode(method, res, sizeof(res), chunk, size)) > 0) | 353 | if ((rc = bindle_decode(method, res, sizeof(res), chunk, size)) > 0) |
| 313 | write(STDOUT_FILENO, res, (size_t)rc); | 354 | write(bindle_fdout, res, (size_t)rc); |
| 314 | }; | 355 | }; |
| 315 | } | 356 | } |
| 316 | else | 357 | else |
| @@ -320,7 +361,7 @@ bindle_widget_encodings_string( | |||
| 320 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); | 361 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); |
| 321 | memcpy(chunk, &str[off], size); | 362 | memcpy(chunk, &str[off], size); |
| 322 | if ((rc = bindle_encode(method, res, sizeof(res), chunk, size, (cnf->widget_flags & BINDLE_FLG_NOPAD))) > 0) | 363 | if ((rc = bindle_encode(method, res, sizeof(res), chunk, size, (cnf->widget_flags & BINDLE_FLG_NOPAD))) > 0) |
| 323 | write(STDOUT_FILENO, res, (size_t)rc); | 364 | write(bindle_fdout, res, (size_t)rc); |
| 324 | }; | 365 | }; |
| 325 | }; | 366 | }; |
| 326 | 367 | ||
