diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-09-12 22:55:08 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-09-15 09:35:48 -0800 |
| commit | 4e59aa23fba4a8f963a172cadbc13cb0371b3f79 (patch) | |
| tree | 1a3e4eda0ab7204e7780d41ea272327ac1fa4d77 | |
| parent | ef48173562b14a80dbb29d942c7ed4619af8de1f (diff) | |
| download | bindletools-4e59aa23fba4a8f963a172cadbc13cb0371b3f79.tar.gz bindletools-4e59aa23fba4a8f963a172cadbc13cb0371b3f79.tar.bz2 bindletools-4e59aa23fba4a8f963a172cadbc13cb0371b3f79.tar.xz bindletools-4e59aa23fba4a8f963a172cadbc13cb0371b3f79.zip | |
bindletools: stripping newlines from data to be encoded
| -rw-r--r-- | src/widget-encodings.c | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/src/widget-encodings.c b/src/widget-encodings.c index eeaa760..eafb264 100644 --- a/src/widget-encodings.c +++ b/src/widget-encodings.c | |||
| @@ -45,6 +45,7 @@ | |||
| 45 | #include <sys/types.h> | 45 | #include <sys/types.h> |
| 46 | #include <sys/uio.h> | 46 | #include <sys/uio.h> |
| 47 | #include <unistd.h> | 47 | #include <unistd.h> |
| 48 | #include <stdlib.h> | ||
| 48 | #include <errno.h> | 49 | #include <errno.h> |
| 49 | #include <fcntl.h> | 50 | #include <fcntl.h> |
| 50 | 51 | ||
| @@ -406,18 +407,50 @@ bindle_encodings_action_decode( | |||
| 406 | { | 407 | { |
| 407 | ssize_t len; | 408 | ssize_t len; |
| 408 | bindle_state_t * state; | 409 | bindle_state_t * state; |
| 410 | size_t pos; | ||
| 411 | size_t off; | ||
| 412 | size_t chunk_size; | ||
| 413 | int fullbuff; | ||
| 409 | 414 | ||
| 410 | state = cnf->state; | 415 | state = cnf->state; |
| 411 | 416 | ||
| 412 | if ((len = bindle_decode(method, state->res, state->res_size, state->buff, state->buff_len)) == -1) | 417 | fullbuff = (state->buff_size == state->buff_len) ? BNDL_YES : BNDL_NO; |
| 418 | |||
| 419 | // strip newlines | ||
| 420 | for(pos = 0, off = 0; (pos < state->buff_len); pos++) | ||
| 413 | { | 421 | { |
| 414 | fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno)); | 422 | if ( (state->buff[pos] == '\n') || (state->buff[pos] == '\r') ) |
| 415 | return(-1); | 423 | { |
| 424 | off++; | ||
| 425 | continue; | ||
| 426 | }; | ||
| 427 | if (off != 0) | ||
| 428 | state->buff[pos-off] = state->buff[pos]; | ||
| 416 | }; | 429 | }; |
| 417 | if (len > 0) | 430 | state->buff_len -= off; |
| 418 | write(cnf->state->fdout, cnf->state->res, (size_t)len); | 431 | state->buff[state->buff_len] = '\0'; |
| 419 | 432 | ||
| 420 | state->buff_len = 0; | 433 | // decode data |
| 434 | chunk_size = state->buff_len; | ||
| 435 | if (fullbuff == BNDL_YES) | ||
| 436 | { | ||
| 437 | chunk_size = (state->buff_len / state->chunk_size) * state->chunk_size; | ||
| 438 | if ( (chunk_size > state->buff_len) || (chunk_size < state->chunk_size) ) | ||
| 439 | return(0); | ||
| 440 | }; | ||
| 441 | if ((len = bindle_decode(method, state->res, state->res_size, state->buff, chunk_size)) == -1) | ||
| 442 | { | ||
| 443 | fprintf(stderr, "%s: bindle_decode(): %s\n", cnf->prog_name, strerror(errno)); | ||
| 444 | return(-1); | ||
| 445 | }; | ||
| 446 | if (len == 0) | ||
| 447 | return(0); | ||
| 448 | write(state->fdout, state->res, (size_t)len); | ||
| 449 | |||
| 450 | // shift unencoded data | ||
| 451 | for(pos = chunk_size; (pos < state->buff_len); pos++) | ||
| 452 | state->buff[pos-chunk_size] = state->buff[pos]; | ||
| 453 | state->buff_len -= chunk_size; | ||
| 421 | 454 | ||
| 422 | return(0); | 455 | return(0); |
| 423 | } | 456 | } |
