diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-09-15 11:28:54 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-09-15 11:28:54 -0800 |
| commit | aa588253f3d26f711a6bc76082cf324c9f349751 (patch) | |
| tree | b37324e9cc29a894eff82caa9b846d1d6a25fc93 | |
| parent | f36447006295eb40b30da25d176e8fbeb370b433 (diff) | |
| download | bindletools-aa588253f3d26f711a6bc76082cf324c9f349751.tar.gz bindletools-aa588253f3d26f711a6bc76082cf324c9f349751.tar.bz2 bindletools-aa588253f3d26f711a6bc76082cf324c9f349751.tar.xz bindletools-aa588253f3d26f711a6bc76082cf324c9f349751.zip | |
bindltools: switching from file handles to file streams in encoding widgets
Switching from file handles to file streams to avoid potential buffer issues.
| -rw-r--r-- | src/widget-encodings.c | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/src/widget-encodings.c b/src/widget-encodings.c index 2af715d..57d693e 100644 --- a/src/widget-encodings.c +++ b/src/widget-encodings.c | |||
| @@ -95,8 +95,8 @@ struct bindle_state | |||
| 95 | size_t line_off; | 95 | size_t line_off; |
| 96 | char * buff; | 96 | char * buff; |
| 97 | char * res; | 97 | char * res; |
| 98 | int fdin; | 98 | FILE * fsin; |
| 99 | int fdout; | 99 | FILE * fsout; |
| 100 | }; | 100 | }; |
| 101 | 101 | ||
| 102 | 102 | ||
| @@ -305,8 +305,8 @@ bindle_widget_encodings( | |||
| 305 | optind = 1; | 305 | optind = 1; |
| 306 | opt_index = 0; | 306 | opt_index = 0; |
| 307 | str = NULL; | 307 | str = NULL; |
| 308 | cnf->state->fdin = STDIN_FILENO; | 308 | cnf->state->fsin = NULL; |
| 309 | cnf->state->fdout = STDOUT_FILENO; | 309 | cnf->state->fsout = NULL; |
| 310 | 310 | ||
| 311 | while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) | 311 | while((c = bindle_getopt(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) |
| 312 | { | 312 | { |
| @@ -333,15 +333,15 @@ bindle_widget_encodings( | |||
| 333 | break; | 333 | break; |
| 334 | 334 | ||
| 335 | case 'i': | 335 | case 'i': |
| 336 | if (cnf->state->fdin != STDIN_FILENO) | 336 | if (cnf->state->fsin != NULL) |
| 337 | { | 337 | { |
| 338 | fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); | 338 | fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); |
| 339 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | 339 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); |
| 340 | return(1); | 340 | return(1); |
| 341 | }; | 341 | }; |
| 342 | if ((cnf->state->fdin = open(optarg, O_RDONLY)) == -1) | 342 | if ((cnf->state->fsin = fopen(optarg, "r")) == NULL) |
| 343 | { | 343 | { |
| 344 | fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); | 344 | fprintf(stderr, "%s: fopen(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); |
| 345 | return(1); | 345 | return(1); |
| 346 | }; | 346 | }; |
| 347 | break; | 347 | break; |
| @@ -351,15 +351,15 @@ bindle_widget_encodings( | |||
| 351 | break; | 351 | break; |
| 352 | 352 | ||
| 353 | case 'o': | 353 | case 'o': |
| 354 | if (cnf->state->fdout != STDOUT_FILENO) | 354 | if (cnf->state->fsout != NULL) |
| 355 | { | 355 | { |
| 356 | fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); | 356 | fprintf(stderr, "%s: duplicate option `--%c'\n", PROGRAM_NAME, c); |
| 357 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | 357 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); |
| 358 | return(1); | 358 | return(1); |
| 359 | }; | 359 | }; |
| 360 | if ((cnf->state->fdout = open(optarg, (O_WRONLY|O_CREAT|O_TRUNC), 0644)) == -1) | 360 | if ((cnf->state->fsout = fopen(optarg, "w")) == NULL) |
| 361 | { | 361 | { |
| 362 | fprintf(stderr, "%s: open(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); | 362 | fprintf(stderr, "%s: fopen(): %s: %s\n", cnf->prog_name, optarg, strerror(errno)); |
| 363 | return(1); | 363 | return(1); |
| 364 | }; | 364 | }; |
| 365 | break; | 365 | break; |
| @@ -400,8 +400,8 @@ bindle_widget_encodings( | |||
| 400 | fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); | 400 | fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); |
| 401 | return(1); | 401 | return(1); |
| 402 | }; | 402 | }; |
| 403 | 403 | cnf->state->fsout = (cnf->state->fsout == NULL) ? stdout : cnf->state->fsout; | |
| 404 | fflush(stdout); | 404 | cnf->state->fsin = (cnf->state->fsin == NULL) ? stdin : cnf->state->fsin; |
| 405 | 405 | ||
| 406 | if ((str)) | 406 | if ((str)) |
| 407 | rc = bindle_encodings_src_string(cnf, method, str); | 407 | rc = bindle_encodings_src_string(cnf, method, str); |
| @@ -409,7 +409,14 @@ bindle_widget_encodings( | |||
| 409 | rc = bindle_encodings_src_fileno(cnf, method); | 409 | rc = bindle_encodings_src_fileno(cnf, method); |
| 410 | 410 | ||
| 411 | if ((cnf->widget_flags & BINDLE_FLG_NEWLINE)) | 411 | if ((cnf->widget_flags & BINDLE_FLG_NEWLINE)) |
| 412 | write(cnf->state->fdout, "\n", strlen("\n")); | 412 | fprintf(cnf->state->fsout, "\n"); |
| 413 | |||
| 414 | fflush(cnf->state->fsout); | ||
| 415 | |||
| 416 | if (cnf->state->fsout != stdout) | ||
| 417 | fclose(cnf->state->fsout); | ||
| 418 | if (cnf->state->fsin != stdin) | ||
| 419 | fclose(cnf->state->fsin); | ||
| 413 | 420 | ||
| 414 | return(rc); | 421 | return(rc); |
| 415 | } | 422 | } |
| @@ -452,7 +459,7 @@ bindle_encodings_action_decode( | |||
| 452 | }; | 459 | }; |
| 453 | if (len == 0) | 460 | if (len == 0) |
| 454 | return(0); | 461 | return(0); |
| 455 | write(state->fdout, state->res, (size_t)len); | 462 | fwrite(state->res, 1, (size_t)len, state->fsout); |
| 456 | state->buff_len = 0; | 463 | state->buff_len = 0; |
| 457 | 464 | ||
| 458 | return(0); | 465 | return(0); |
| @@ -480,7 +487,7 @@ bindle_encodings_action_encode( | |||
| 480 | 487 | ||
| 481 | if (!(state->line_wrap)) | 488 | if (!(state->line_wrap)) |
| 482 | { | 489 | { |
| 483 | if (write(cnf->state->fdout, state->res, (size_t)len) == -1) | 490 | if (fwrite(state->res, 1, (size_t)len, state->fsout) == 0) |
| 484 | { | 491 | { |
| 485 | fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno)); | 492 | fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno)); |
| 486 | return(-1); | 493 | return(-1); |
| @@ -494,7 +501,7 @@ bindle_encodings_action_encode( | |||
| 494 | chunk_len = (ssize_t)(state->line_wrap - state->line_off); | 501 | chunk_len = (ssize_t)(state->line_wrap - state->line_off); |
| 495 | chunk_len = (len < (ssize_t)chunk_len) ? len : chunk_len; | 502 | chunk_len = (len < (ssize_t)chunk_len) ? len : chunk_len; |
| 496 | chunk_len = (chunk_len > (len-buff_off)) ? (len-buff_off) : chunk_len; | 503 | chunk_len = (chunk_len > (len-buff_off)) ? (len-buff_off) : chunk_len; |
| 497 | if (write(cnf->state->fdout, &cnf->state->res[buff_off], (size_t)chunk_len) == -1) | 504 | if (fwrite(&cnf->state->res[buff_off], 1, (size_t)chunk_len, state->fsout) == 0) |
| 498 | { | 505 | { |
| 499 | fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno)); | 506 | fprintf(stderr, "%s: %s\n", cnf->prog_name, strerror(errno)); |
| 500 | return(-1); | 507 | return(-1); |
| @@ -504,7 +511,7 @@ bindle_encodings_action_encode( | |||
| 504 | if (state->line_off == state->line_wrap) | 511 | if (state->line_off == state->line_wrap) |
| 505 | { | 512 | { |
| 506 | state->line_off = 0; | 513 | state->line_off = 0; |
| 507 | write(cnf->state->fdout, "\n", strlen("\n")); | 514 | fprintf(state->fsout, "\n"); |
| 508 | }; | 515 | }; |
| 509 | }; | 516 | }; |
| 510 | 517 | ||
| @@ -517,7 +524,7 @@ bindle_encodings_src_fileno( | |||
| 517 | bindle_conf_t * cnf, | 524 | bindle_conf_t * cnf, |
| 518 | int method ) | 525 | int method ) |
| 519 | { | 526 | { |
| 520 | ssize_t len; | 527 | size_t len; |
| 521 | bindle_state_t * state; | 528 | bindle_state_t * state; |
| 522 | char * buff; | 529 | char * buff; |
| 523 | 530 | ||
| @@ -526,7 +533,7 @@ bindle_encodings_src_fileno( | |||
| 526 | state->line_off = 0; | 533 | state->line_off = 0; |
| 527 | buff = state->buff; | 534 | buff = state->buff; |
| 528 | 535 | ||
| 529 | while ((len = read(cnf->state->fdin, &buff[state->buff_len], (state->buff_size-state->buff_len))) >= 0) | 536 | while ((len = fread(&buff[state->buff_len], 1, (state->buff_size-state->buff_len), state->fsin)) >= 0) |
| 530 | { | 537 | { |
| 531 | state->buff_len += (size_t)len; | 538 | state->buff_len += (size_t)len; |
| 532 | if (state->buff_len == 0) | 539 | if (state->buff_len == 0) |
| @@ -541,11 +548,6 @@ bindle_encodings_src_fileno( | |||
| 541 | if (len == 0) | 548 | if (len == 0) |
| 542 | return(0); | 549 | return(0); |
| 543 | }; | 550 | }; |
| 544 | if (len == -1) | ||
| 545 | { | ||
| 546 | fprintf(stderr, "%s: read(): %s\n", cnf->prog_name, strerror(errno)); | ||
| 547 | return(1); | ||
| 548 | }; | ||
| 549 | 551 | ||
| 550 | return(0); | 552 | return(0); |
| 551 | } | 553 | } |
| @@ -573,7 +575,7 @@ bindle_encodings_src_string( | |||
| 573 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); | 575 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); |
| 574 | memcpy(chunk, &str[off], size); | 576 | memcpy(chunk, &str[off], size); |
| 575 | if ((rc = bindle_decode(method, res, sizeof(res), chunk, size)) > 0) | 577 | if ((rc = bindle_decode(method, res, sizeof(res), chunk, size)) > 0) |
| 576 | write(cnf->state->fdout, res, (size_t)rc); | 578 | fwrite(res, 1, (size_t)rc, cnf->state->fsout); |
| 577 | }; | 579 | }; |
| 578 | } | 580 | } |
| 579 | else | 581 | else |
| @@ -583,7 +585,7 @@ bindle_encodings_src_string( | |||
| 583 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); | 585 | size = ((off+BINDLE_BUFF_SIZE) < len) ? BINDLE_BUFF_SIZE : (len-off); |
| 584 | memcpy(chunk, &str[off], size); | 586 | memcpy(chunk, &str[off], size); |
| 585 | if ((rc = bindle_encode(method, res, sizeof(res), chunk, size, (cnf->widget_flags & BINDLE_FLG_NOPAD))) > 0) | 587 | if ((rc = bindle_encode(method, res, sizeof(res), chunk, size, (cnf->widget_flags & BINDLE_FLG_NOPAD))) > 0) |
| 586 | write(cnf->state->fdout, res, (size_t)rc); | 588 | fwrite(res, 1, (size_t)rc, cnf->state->fsout); |
| 587 | }; | 589 | }; |
| 588 | }; | 590 | }; |
| 589 | 591 | ||
