diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-09-15 12:13:05 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-09-15 12:13:05 -0800 |
| commit | 2c9f0ca388f60540be8a20c9aa95c1f776e4537f (patch) | |
| tree | 7475fcb9a7535c69b4255b047a5c6b1df7f61133 | |
| parent | fd1aca25ce8f2bbdce8594289c1461b0509c35fc (diff) | |
| download | bindletools-2c9f0ca388f60540be8a20c9aa95c1f776e4537f.tar.gz bindletools-2c9f0ca388f60540be8a20c9aa95c1f776e4537f.tar.bz2 bindletools-2c9f0ca388f60540be8a20c9aa95c1f776e4537f.tar.xz bindletools-2c9f0ca388f60540be8a20c9aa95c1f776e4537f.zip | |
bindletools: adding ability to ignore bad characters when decoding data
| -rw-r--r-- | src/widget-encodings.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/widget-encodings.c b/src/widget-encodings.c index 57d693e..cf31e15 100644 --- a/src/widget-encodings.c +++ b/src/widget-encodings.c | |||
| @@ -63,6 +63,7 @@ | |||
| 63 | #define BINDLE_FLG_DECODE 0x00002UL | 63 | #define BINDLE_FLG_DECODE 0x00002UL |
| 64 | #define BINDLE_FLG_NOPAD 0x00004UL | 64 | #define BINDLE_FLG_NOPAD 0x00004UL |
| 65 | #define BINDLE_FLG_NEWLINE 0x00008UL | 65 | #define BINDLE_FLG_NEWLINE 0x00008UL |
| 66 | #define BINDLE_FLG_IGNOREBAD 0x00010UL | ||
| 66 | 67 | ||
| 67 | #define BNDLE_B16_CHUNK 2 | 68 | #define BNDLE_B16_CHUNK 2 |
| 68 | #define BNDLE_B16_BUFF_LEN (BNDLE_B16_CHUNK*32) | 69 | #define BNDLE_B16_BUFF_LEN (BNDLE_B16_CHUNK*32) |
| @@ -112,6 +113,7 @@ const char * const bindle_widget_encodings_options[] = | |||
| 112 | { | 113 | { |
| 113 | " -d, --decode decode string", | 114 | " -d, --decode decode string", |
| 114 | " -e, --encode encode string", | 115 | " -e, --encode encode string", |
| 116 | " -I, --ignore-garbage ignore invalid characters when decoding", | ||
| 115 | " -i file, --input=file use input file instead of stdin", | 117 | " -i file, --input=file use input file instead of stdin", |
| 116 | " -n, --newline add newline to output", | 118 | " -n, --newline add newline to output", |
| 117 | " -o file, --output=file use output file instead of stdout", | 119 | " -o file, --output=file use output file instead of stdout", |
| @@ -287,13 +289,14 @@ bindle_widget_encodings( | |||
| 287 | const char * str; | 289 | const char * str; |
| 288 | 290 | ||
| 289 | // getopt options | 291 | // getopt options |
| 290 | static const char * short_opt = BINDLE_COMMON_SHORT "b:dei:no:ps:w:"; | 292 | static const char * short_opt = BINDLE_COMMON_SHORT "b:deIi:no:ps:w:"; |
| 291 | static struct option long_opt[] = | 293 | static struct option long_opt[] = |
| 292 | { | 294 | { |
| 293 | BINDLE_COMMON_LONG, | 295 | BINDLE_COMMON_LONG, |
| 294 | { "break", no_argument, NULL, 'b' }, | 296 | { "break", no_argument, NULL, 'b' }, |
| 295 | { "decode", no_argument, NULL, 'd' }, | 297 | { "decode", no_argument, NULL, 'd' }, |
| 296 | { "encode", no_argument, NULL, 'e' }, | 298 | { "encode", no_argument, NULL, 'e' }, |
| 299 | { "ignore-garbage", no_argument, NULL, 'I' }, | ||
| 297 | { "input", optional_argument, NULL, 'i' }, | 300 | { "input", optional_argument, NULL, 'i' }, |
| 298 | { "newline", no_argument, NULL, 'n' }, | 301 | { "newline", no_argument, NULL, 'n' }, |
| 299 | { "nopadding", no_argument, NULL, 'p' }, | 302 | { "nopadding", no_argument, NULL, 'p' }, |
| @@ -332,6 +335,10 @@ bindle_widget_encodings( | |||
| 332 | cnf->widget_flags &= ~BINDLE_FLG_DECODE; | 335 | cnf->widget_flags &= ~BINDLE_FLG_DECODE; |
| 333 | break; | 336 | break; |
| 334 | 337 | ||
| 338 | case 'I': | ||
| 339 | cnf->widget_flags |= BINDLE_FLG_IGNOREBAD; | ||
| 340 | break; | ||
| 341 | |||
| 335 | case 'i': | 342 | case 'i': |
| 336 | if (cnf->state->fsin != NULL) | 343 | if (cnf->state->fsin != NULL) |
| 337 | { | 344 | { |
| @@ -427,6 +434,7 @@ bindle_encodings_action_decode( | |||
| 427 | bindle_conf_t * cnf, | 434 | bindle_conf_t * cnf, |
| 428 | int method ) | 435 | int method ) |
| 429 | { | 436 | { |
| 437 | char c; | ||
| 430 | ssize_t len; | 438 | ssize_t len; |
| 431 | bindle_state_t * state; | 439 | bindle_state_t * state; |
| 432 | size_t pos; | 440 | size_t pos; |
| @@ -439,7 +447,13 @@ bindle_encodings_action_decode( | |||
| 439 | // strip newlines | 447 | // strip newlines |
| 440 | for(pos = 0, off = 0; (pos < state->buff_len); pos++) | 448 | for(pos = 0, off = 0; (pos < state->buff_len); pos++) |
| 441 | { | 449 | { |
| 442 | if ( (state->buff[pos] == '\n') || (state->buff[pos] == '\r') ) | 450 | c = state->buff[pos]; |
| 451 | if ( (c == '\n') || (c == '\r') ) | ||
| 452 | { | ||
| 453 | off++; | ||
| 454 | continue; | ||
| 455 | }; | ||
| 456 | if ( ((cnf->widget_flags & BINDLE_FLG_IGNOREBAD)) && (bindle_encoded_char(method, c) == -1) ) | ||
| 443 | { | 457 | { |
| 444 | off++; | 458 | off++; |
| 445 | continue; | 459 | continue; |
