Buffer size to 32KiB, fixes
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s

This commit is contained in:
Stephen Seo 2024-10-07 15:16:05 +09:00
parent 8bae61d36d
commit aa46172aa7

View file

@ -44,6 +44,8 @@
#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%lulu of %%%lulu.\n" #define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%lulu of %%%lulu.\n"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%lulu/%%%lulu]\n" #define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%lulu/%%%lulu]\n"
#define SIMPLE_ARCHIVER_BUFFER_SIZE (1024 * 32)
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
@ -246,8 +248,8 @@ int write_files_fn(void *data, void *ud) {
} }
// Write file to pipe, and read from other pipe. // Write file to pipe, and read from other pipe.
char write_buf[1024]; char write_buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
char read_buf[1024]; char read_buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
int_fast8_t write_again = 0; int_fast8_t write_again = 0;
int_fast8_t write_done = 0; int_fast8_t write_done = 0;
int_fast8_t read_done = 0; int_fast8_t read_done = 0;
@ -265,7 +267,8 @@ int write_files_fn(void *data, void *ud) {
// Read from file. // Read from file.
if (!write_done) { if (!write_done) {
if (!write_again) { if (!write_again) {
write_count = fread(write_buf, 1, 1024, file_fd); write_count =
fread(write_buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, file_fd);
} }
if (write_count > 0) { if (write_count > 0) {
ret = write(pipe_into_cmd[1], write_buf, write_count); ret = write(pipe_into_cmd[1], write_buf, write_count);
@ -307,7 +310,7 @@ int write_files_fn(void *data, void *ud) {
// Read from compressor. // Read from compressor.
if (!read_done) { if (!read_done) {
ret = read(pipe_outof_cmd[0], read_buf, 1024); ret = read(pipe_outof_cmd[0], read_buf, SIMPLE_ARCHIVER_BUFFER_SIZE);
if (ret > 0) { if (ret > 0) {
read_count = fwrite(read_buf, 1, (size_t)ret, tmp_fd); read_count = fwrite(read_buf, 1, (size_t)ret, tmp_fd);
if (read_count != (size_t)ret) { if (read_count != (size_t)ret) {
@ -463,9 +466,9 @@ int write_files_fn(void *data, void *ud) {
// Write file. // Write file.
fprintf(stderr, "Writing compressed file: %s\n", file_info->filename); fprintf(stderr, "Writing compressed file: %s\n", file_info->filename);
do { do {
write_count = fread(write_buf, 1, 1024, tmp_fd); write_count = fread(write_buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, tmp_fd);
if (write_count == 1024) { if (write_count == SIMPLE_ARCHIVER_BUFFER_SIZE) {
fwrite(write_buf, 1, 1024, state->out_f); fwrite(write_buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, state->out_f);
} else if (write_count > 0) { } else if (write_count > 0) {
fwrite(write_buf, 1, write_count, state->out_f); fwrite(write_buf, 1, write_count, state->out_f);
} }
@ -604,12 +607,12 @@ int write_files_fn(void *data, void *ud) {
// Write file. // Write file.
fprintf(stderr, "Writing file: %s\n", file_info->filename); fprintf(stderr, "Writing file: %s\n", file_info->filename);
char buf[1024]; char buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
size_t ret; size_t ret;
do { do {
ret = fread(buf, 1, 1024, fd); ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, fd);
if (ret == 1024) { if (ret == SIMPLE_ARCHIVER_BUFFER_SIZE) {
fwrite(buf, 1, 1024, state->out_f); fwrite(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, state->out_f);
} else if (ret > 0) { } else if (ret > 0) {
fwrite(buf, 1, ret, state->out_f); fwrite(buf, 1, ret, state->out_f);
} }
@ -958,19 +961,19 @@ int read_fd_to_out_fd(FILE *in_fd, FILE *out_fd, char *read_buf,
int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f, int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f,
char *buf, const size_t buf_size, char *hold_buf, char *buf, const size_t buf_size, char *hold_buf,
int *has_hold) { ssize_t *has_hold) {
if (*to_dec_pipe >= 0) { if (*to_dec_pipe >= 0) {
if (*chunk_remaining > 0) { if (*chunk_remaining > 0) {
if (*chunk_remaining > buf_size) { if (*chunk_remaining > buf_size) {
if (*has_hold < 0) { if (*has_hold < 0) {
size_t fread_ret = fread(buf, 1, 1024, in_f); size_t fread_ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, in_f);
if (fread_ret == 0) { if (fread_ret == 0) {
goto TRY_WRITE_TO_DECOMP_END; goto TRY_WRITE_TO_DECOMP_END;
} else { } else {
ssize_t write_ret = write(*to_dec_pipe, buf, fread_ret); ssize_t write_ret = write(*to_dec_pipe, buf, fread_ret);
if (write_ret < 0) { if (write_ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
*has_hold = (int)fread_ret; *has_hold = (ssize_t)fread_ret;
memcpy(hold_buf, buf, fread_ret); memcpy(hold_buf, buf, fread_ret);
return SDAS_SUCCESS; return SDAS_SUCCESS;
} else { } else {
@ -978,6 +981,11 @@ int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f,
} }
} else if (write_ret == 0) { } else if (write_ret == 0) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} else if ((size_t)write_ret < fread_ret) {
*chunk_remaining -= (size_t)write_ret;
*has_hold = (ssize_t)fread_ret - write_ret;
memcpy(hold_buf, buf + write_ret, (size_t)*has_hold);
return SDAS_SUCCESS;
} else { } else {
*chunk_remaining -= (size_t)write_ret; *chunk_remaining -= (size_t)write_ret;
} }
@ -992,6 +1000,12 @@ int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f,
} }
} else if (write_ret == 0) { } else if (write_ret == 0) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} else if (write_ret < *has_hold) {
*chunk_remaining -= (size_t)write_ret;
memcpy(buf, hold_buf + write_ret, (size_t)(*has_hold - write_ret));
memcpy(hold_buf, buf, (size_t)(*has_hold - write_ret));
*has_hold = *has_hold - write_ret;
return SDAS_SUCCESS;
} else { } else {
*chunk_remaining -= (size_t)*has_hold; *chunk_remaining -= (size_t)*has_hold;
*has_hold = -1; *has_hold = -1;
@ -1014,6 +1028,11 @@ int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f,
} }
} else if (write_ret == 0) { } else if (write_ret == 0) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} else if ((size_t)write_ret < fread_ret) {
*chunk_remaining -= (size_t)write_ret;
*has_hold = (ssize_t)fread_ret - write_ret;
memcpy(hold_buf, buf + write_ret, (size_t)*has_hold);
return SDAS_SUCCESS;
} else if ((size_t)write_ret <= *chunk_remaining) { } else if ((size_t)write_ret <= *chunk_remaining) {
*chunk_remaining -= (size_t)write_ret; *chunk_remaining -= (size_t)write_ret;
} else { } else {
@ -1030,6 +1049,12 @@ int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f,
} }
} else if (write_ret == 0) { } else if (write_ret == 0) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} else if (write_ret < *has_hold) {
*chunk_remaining -= (size_t)write_ret;
memcpy(buf, hold_buf + write_ret, (size_t)(*has_hold - write_ret));
memcpy(hold_buf, buf, (size_t)(*has_hold - write_ret));
*has_hold = *has_hold - write_ret;
return SDAS_SUCCESS;
} else { } else {
*chunk_remaining -= (size_t)*has_hold; *chunk_remaining -= (size_t)*has_hold;
*has_hold = -1; *has_hold = -1;
@ -1040,7 +1065,7 @@ int try_write_to_decomp(int *to_dec_pipe, uint64_t *chunk_remaining, FILE *in_f,
} }
TRY_WRITE_TO_DECOMP_END: TRY_WRITE_TO_DECOMP_END:
if (*to_dec_pipe >= 0 && *chunk_remaining == 0) { if (*to_dec_pipe >= 0 && *chunk_remaining == 0 && *has_hold < 0) {
close(*to_dec_pipe); close(*to_dec_pipe);
*to_dec_pipe = -1; *to_dec_pipe = -1;
} }
@ -1053,7 +1078,7 @@ int read_decomp_to_out_file(const char *out_filename, int in_pipe,
char *read_buf, const size_t read_buf_size, char *read_buf, const size_t read_buf_size,
const uint64_t file_size, int *to_dec_pipe, const uint64_t file_size, int *to_dec_pipe,
uint64_t *chunk_remaining, FILE *in_f, uint64_t *chunk_remaining, FILE *in_f,
char *hold_buf, int *has_hold) { char *hold_buf, ssize_t *has_hold) {
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *out_fd = __attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *out_fd =
NULL; NULL;
if (out_filename) { if (out_filename) {
@ -1809,7 +1834,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
char buf[1024]; char buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
uint16_t u16 = 1; uint16_t u16 = 1;
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -2293,8 +2318,8 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
fopen(file_info_struct->filename, "rb"); fopen(file_info_struct->filename, "rb");
int_fast8_t to_comp_finished = 0; int_fast8_t to_comp_finished = 0;
char hold_buf[1024]; char hold_buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
int has_hold = -1; ssize_t has_hold = -1;
while (!to_comp_finished) { while (!to_comp_finished) {
if (!to_comp_finished) { if (!to_comp_finished) {
// Write to compressor. // Write to compressor.
@ -2303,7 +2328,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
if (has_hold < 0) { if (has_hold < 0) {
size_t fread_ret = fread(buf, 1, 1024, fd); size_t fread_ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, fd);
if (fread_ret > 0) { if (fread_ret > 0) {
ssize_t write_ret = write(pipe_into_write, buf, fread_ret); ssize_t write_ret = write(pipe_into_write, buf, fread_ret);
if (write_ret < 0) { if (write_ret < 0) {
@ -2317,15 +2342,18 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
"ERROR: Writing to compressor, pipe write error!\n"); "ERROR: Writing to compressor, pipe write error!\n");
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} }
} else if ((size_t)write_ret != fread_ret) { } else if (write_ret == 0) {
fprintf( fprintf(
stderr, stderr,
"ERROR: Writing to compressor, unable to write bytes!\n"); "ERROR: Writing to compressor, unable to write bytes!\n");
return SDAS_FAILED_TO_WRITE; return SDAS_FAILED_TO_WRITE;
} else if ((size_t)write_ret < fread_ret) {
has_hold = (ssize_t)fread_ret - write_ret;
memcpy(hold_buf, buf + write_ret, (size_t)has_hold);
} }
} }
if (feof(fd)) { if (feof(fd) && has_hold < 0) {
to_comp_finished = 1; to_comp_finished = 1;
} }
} else { } else {
@ -2337,6 +2365,11 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
} else { } else {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
} else if (write_ret < has_hold) {
memcpy(buf, hold_buf + write_ret,
(size_t)(has_hold - write_ret));
memcpy(hold_buf, buf, (size_t)(has_hold - write_ret));
has_hold = has_hold - write_ret;
} else if (write_ret != has_hold) { } else if (write_ret != has_hold) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} else { } else {
@ -2346,7 +2379,8 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
} }
// Write compressed data to temp file. // Write compressed data to temp file.
ssize_t read_ret = read(pipe_outof_read, buf, 1024); ssize_t read_ret =
read(pipe_outof_read, buf, SIMPLE_ARCHIVER_BUFFER_SIZE);
if (read_ret < 0) { if (read_ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read. // Non-blocking read.
@ -2375,7 +2409,8 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
// Finish writing. // Finish writing.
if (!to_temp_finished) { if (!to_temp_finished) {
while (1) { while (1) {
ssize_t read_ret = read(pipe_outof_read, buf, 1024); ssize_t read_ret =
read(pipe_outof_read, buf, SIMPLE_ARCHIVER_BUFFER_SIZE);
if (read_ret < 0) { if (read_ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read. // Non-blocking read.
@ -2424,7 +2459,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
if (ferror(temp_fd)) { if (ferror(temp_fd)) {
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
size_t fread_ret = fread(buf, 1, 1024, temp_fd); size_t fread_ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, temp_fd);
if (fread_ret > 0) { if (fread_ret > 0) {
size_t fwrite_ret = fwrite(buf, 1, fread_ret, out_f); size_t fwrite_ret = fwrite(buf, 1, fread_ret, out_f);
written_size += fread_ret; written_size += fread_ret;
@ -2468,7 +2503,7 @@ int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state,
fprintf(stderr, "ERROR: Writing to chunk, file read error!\n"); fprintf(stderr, "ERROR: Writing to chunk, file read error!\n");
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
size_t fread_ret = fread(buf, 1, 1024, fd); size_t fread_ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, fd);
if (fread_ret > 0) { if (fread_ret > 0) {
size_t fwrite_ret = fwrite(buf, 1, fread_ret, out_f); size_t fwrite_ret = fwrite(buf, 1, fread_ret, out_f);
if (fwrite_ret != fread_ret) { if (fwrite_ret != fread_ret) {
@ -2513,8 +2548,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract, int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state) { const SDArchiverState *state) {
uint8_t buf[1024]; uint8_t buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
memset(buf, 0, 1024);
uint16_t u16; uint16_t u16;
uint32_t u32; uint32_t u32;
uint64_t u64; uint64_t u64;
@ -2547,11 +2581,11 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
} }
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
fprintf(stderr, "Compressor size is %u\n", u16); fprintf(stderr, "Compressor size is %u\n", u16);
if (u16 < 1024) { if (u16 < SIMPLE_ARCHIVER_BUFFER_SIZE) {
if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
buf[1023] = 0; buf[SIMPLE_ARCHIVER_BUFFER_SIZE - 1] = 0;
fprintf(stderr, "Compressor cmd: %s\n", buf); fprintf(stderr, "Compressor cmd: %s\n", buf);
} else { } else {
__attribute__(( __attribute__((
@ -2571,11 +2605,11 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
} }
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
fprintf(stderr, "Decompressor size is %u\n", u16); fprintf(stderr, "Decompressor size is %u\n", u16);
if (u16 < 1024) { if (u16 < SIMPLE_ARCHIVER_BUFFER_SIZE) {
if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
buf[1023] = 0; buf[SIMPLE_ARCHIVER_BUFFER_SIZE - 1] = 0;
fprintf(stderr, "Decompressor cmd: %s\n", buf); fprintf(stderr, "Decompressor cmd: %s\n", buf);
decompressor_cmd = malloc(u16 + 1); decompressor_cmd = malloc(u16 + 1);
memcpy((char *)decompressor_cmd, buf, u16 + 1); memcpy((char *)decompressor_cmd, buf, u16 + 1);
@ -2637,11 +2671,11 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_cleanup_malloced))) void *out_f_name = NULL; simple_archiver_helper_cleanup_malloced))) void *out_f_name = NULL;
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *out_f = __attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) FILE *out_f =
NULL; NULL;
if (u16 < 1024) { if (u16 < SIMPLE_ARCHIVER_BUFFER_SIZE) {
if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
buf[1023] = 0; buf[SIMPLE_ARCHIVER_BUFFER_SIZE - 1] = 0;
fprintf(stderr, " Filename: %s\n", buf); fprintf(stderr, " Filename: %s\n", buf);
if (simple_archiver_validate_file_path((char *)buf)) { if (simple_archiver_validate_file_path((char *)buf)) {
fprintf(stderr, " ERROR: Invalid filename!\n"); fprintf(stderr, " ERROR: Invalid filename!\n");
@ -2951,7 +2985,7 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
int_fast8_t write_pipe_done = 0; int_fast8_t write_pipe_done = 0;
int_fast8_t read_pipe_done = 0; int_fast8_t read_pipe_done = 0;
size_t fread_ret = 0; size_t fread_ret = 0;
char recv_buf[1024]; char recv_buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
size_t amount_to_read; size_t amount_to_read;
while (!write_pipe_done || !read_pipe_done) { while (!write_pipe_done || !read_pipe_done) {
if (is_sig_pipe_occurred) { if (is_sig_pipe_occurred) {
@ -2964,8 +2998,8 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
// Read from file. // Read from file.
if (!write_pipe_done) { if (!write_pipe_done) {
if (!write_again && compressed_file_size != 0) { if (!write_again && compressed_file_size != 0) {
if (compressed_file_size > 1024) { if (compressed_file_size > SIMPLE_ARCHIVER_BUFFER_SIZE) {
amount_to_read = 1024; amount_to_read = SIMPLE_ARCHIVER_BUFFER_SIZE;
} else { } else {
amount_to_read = compressed_file_size; amount_to_read = compressed_file_size;
} }
@ -3007,7 +3041,8 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
// Read output from decompressor and write to file. // Read output from decompressor and write to file.
if (!read_pipe_done) { if (!read_pipe_done) {
ssize_t read_ret = read(pipe_outof_cmd[0], recv_buf, 1024); ssize_t read_ret = read(pipe_outof_cmd[0], recv_buf,
SIMPLE_ARCHIVER_BUFFER_SIZE);
if (read_ret > 0) { if (read_ret > 0) {
size_t fwrite_ret = size_t fwrite_ret =
fwrite(recv_buf, 1, (size_t)read_ret, out_f); fwrite(recv_buf, 1, (size_t)read_ret, out_f);
@ -3063,8 +3098,8 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
uint64_t compressed_file_size = u64; uint64_t compressed_file_size = u64;
size_t fread_ret; size_t fread_ret;
while (compressed_file_size != 0) { while (compressed_file_size != 0) {
if (compressed_file_size > 1024) { if (compressed_file_size > SIMPLE_ARCHIVER_BUFFER_SIZE) {
fread_ret = fread(buf, 1, 1024, in_f); fread_ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, in_f);
if (ferror(in_f)) { if (ferror(in_f)) {
// Error. // Error.
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
@ -3107,8 +3142,8 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
#endif #endif
} else { } else {
while (u64 != 0) { while (u64 != 0) {
if (u64 > 1024) { if (u64 > SIMPLE_ARCHIVER_BUFFER_SIZE) {
size_t read_ret = fread(buf, 1, 1024, in_f); size_t read_ret = fread(buf, 1, SIMPLE_ARCHIVER_BUFFER_SIZE, in_f);
if (read_ret > 0) { if (read_ret > 0) {
u64 -= read_ret; u64 -= read_ret;
} else if (ferror(in_f)) { } else if (ferror(in_f)) {
@ -3142,11 +3177,11 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
if (u16 == 0) { if (u16 == 0) {
fprintf(stderr, " Link does not have absolute path.\n"); fprintf(stderr, " Link does not have absolute path.\n");
} else if (u16 < 1024) { } else if (u16 < SIMPLE_ARCHIVER_BUFFER_SIZE) {
if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
buf[1023] = 0; buf[SIMPLE_ARCHIVER_BUFFER_SIZE - 1] = 0;
fprintf(stderr, " Link absolute path: %s\n", buf); fprintf(stderr, " Link absolute path: %s\n", buf);
abs_path = malloc((size_t)u16 + 1); abs_path = malloc((size_t)u16 + 1);
strncpy(abs_path, (char *)buf, (size_t)u16 + 1); strncpy(abs_path, (char *)buf, (size_t)u16 + 1);
@ -3165,11 +3200,11 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
if (u16 == 0) { if (u16 == 0) {
fprintf(stderr, " Link does not have relative path.\n"); fprintf(stderr, " Link does not have relative path.\n");
} else if (u16 < 1024) { } else if (u16 < SIMPLE_ARCHIVER_BUFFER_SIZE) {
if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) { if (fread(buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} }
buf[1023] = 0; buf[SIMPLE_ARCHIVER_BUFFER_SIZE - 1] = 0;
fprintf(stderr, " Link relative path: %s\n", buf); fprintf(stderr, " Link relative path: %s\n", buf);
rel_path = malloc((size_t)u16 + 1); rel_path = malloc((size_t)u16 + 1);
strncpy(rel_path, (char *)buf, (size_t)u16 + 1); strncpy(rel_path, (char *)buf, (size_t)u16 + 1);
@ -3344,8 +3379,7 @@ int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract,
int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract, int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state) { const SDArchiverState *state) {
uint8_t buf[1024]; uint8_t buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
memset(buf, 0, 1024);
uint16_t u16; uint16_t u16;
uint32_t u32; uint32_t u32;
uint64_t u64; uint64_t u64;
@ -3395,7 +3429,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
compressor_cmd = malloc(u16 + 1); compressor_cmd = malloc(u16 + 1);
int ret = int ret =
read_buf_full_from_fd(in_f, (char *)buf, 1024, u16 + 1, compressor_cmd); read_buf_full_from_fd(in_f, (char *)buf, SIMPLE_ARCHIVER_BUFFER_SIZE,
u16 + 1, compressor_cmd);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3409,8 +3444,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
memcpy(&u16, buf, 2); memcpy(&u16, buf, 2);
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
decompressor_cmd = malloc(u16 + 1); decompressor_cmd = malloc(u16 + 1);
ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, u16 + 1, ret = read_buf_full_from_fd(in_f, (char *)buf, SIMPLE_ARCHIVER_BUFFER_SIZE,
decompressor_cmd); u16 + 1, decompressor_cmd);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3457,8 +3492,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
cleanup(simple_archiver_helper_cleanup_c_string))) char *link_name = cleanup(simple_archiver_helper_cleanup_c_string))) char *link_name =
malloc(u16 + 1); malloc(u16 + 1);
int ret = int ret = read_buf_full_from_fd(
read_buf_full_from_fd(in_f, (char *)buf, 1024, u16 + 1, link_name); in_f, (char *)buf, SIMPLE_ARCHIVER_BUFFER_SIZE, u16 + 1, link_name);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3500,7 +3535,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
__attribute__(( __attribute__((
cleanup(simple_archiver_helper_cleanup_c_string))) char *path = cleanup(simple_archiver_helper_cleanup_c_string))) char *path =
malloc(u16 + 1); malloc(u16 + 1);
ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, u16 + 1, path); ret = read_buf_full_from_fd(in_f, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE, u16 + 1, path);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3571,7 +3607,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
__attribute__(( __attribute__((
cleanup(simple_archiver_helper_cleanup_c_string))) char *path = cleanup(simple_archiver_helper_cleanup_c_string))) char *path =
malloc(u16 + 1); malloc(u16 + 1);
ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, u16 + 1, path); ret = read_buf_full_from_fd(in_f, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE, u16 + 1, path);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3675,8 +3712,9 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
file_info->filename = malloc(u16 + 1); file_info->filename = malloc(u16 + 1);
int ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, u16 + 1, int ret =
file_info->filename); read_buf_full_from_fd(in_f, (char *)buf, SIMPLE_ARCHIVER_BUFFER_SIZE,
u16 + 1, file_info->filename);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3848,8 +3886,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
return SDAS_INTERNAL_ERROR; return SDAS_INTERNAL_ERROR;
} }
char hold_buf[1024]; char hold_buf[SIMPLE_ARCHIVER_BUFFER_SIZE];
int has_hold = -1; ssize_t has_hold = -1;
while (node->next != file_info_list->tail) { while (node->next != file_info_list->tail) {
node = node->next; node = node->next;
@ -3883,7 +3921,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
fprintf(stderr, fprintf(stderr,
" WARNING: File already exists and " " WARNING: File already exists and "
"\"--overwrite-extract\" is not specified, skipping!\n"); "\"--overwrite-extract\" is not specified, skipping!\n");
read_decomp_to_out_file(NULL, pipe_outof_read, (char *)buf, 1024, read_decomp_to_out_file(NULL, pipe_outof_read, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE,
file_info->file_size, &pipe_into_write, file_info->file_size, &pipe_into_write,
&chunk_remaining, in_f, hold_buf, &chunk_remaining, in_f, hold_buf,
&has_hold); &has_hold);
@ -3893,9 +3932,9 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_make_dirs(file_info->filename); simple_archiver_helper_make_dirs(file_info->filename);
int ret = read_decomp_to_out_file( int ret = read_decomp_to_out_file(
file_info->filename, pipe_outof_read, (char *)buf, 1024, file_info->filename, pipe_outof_read, (char *)buf,
file_info->file_size, &pipe_into_write, &chunk_remaining, in_f, SIMPLE_ARCHIVER_BUFFER_SIZE, file_info->file_size,
hold_buf, &has_hold); &pipe_into_write, &chunk_remaining, in_f, hold_buf, &has_hold);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3925,15 +3964,17 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
fprintf(stderr, " File size: %lu\n", file_info->file_size); fprintf(stderr, " File size: %lu\n", file_info->file_size);
} }
int ret = read_decomp_to_out_file( int ret = read_decomp_to_out_file(
NULL, pipe_outof_read, (char *)buf, 1024, file_info->file_size, NULL, pipe_outof_read, (char *)buf, SIMPLE_ARCHIVER_BUFFER_SIZE,
&pipe_into_write, &chunk_remaining, in_f, hold_buf, &has_hold); file_info->file_size, &pipe_into_write, &chunk_remaining, in_f,
hold_buf, &has_hold);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
} else { } else {
int ret = read_decomp_to_out_file( int ret = read_decomp_to_out_file(
NULL, pipe_outof_read, (char *)buf, 1024, file_info->file_size, NULL, pipe_outof_read, (char *)buf, SIMPLE_ARCHIVER_BUFFER_SIZE,
&pipe_into_write, &chunk_remaining, in_f, hold_buf, &has_hold); file_info->file_size, &pipe_into_write, &chunk_remaining, in_f,
hold_buf, &has_hold);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
@ -3941,7 +3982,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
} }
// Ensure EOF is left from pipe. // Ensure EOF is left from pipe.
ssize_t read_ret = read(pipe_outof_read, buf, 1024); ssize_t read_ret =
read(pipe_outof_read, buf, SIMPLE_ARCHIVER_BUFFER_SIZE);
if (read_ret > 0) { if (read_ret > 0) {
fprintf(stderr, "WARNING decompressor didn't reach EOF!\n"); fprintf(stderr, "WARNING decompressor didn't reach EOF!\n");
} }
@ -3987,7 +4029,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
fprintf(stderr, fprintf(stderr,
" WARNING: File already exists and " " WARNING: File already exists and "
"\"--overwrite-extract\" is not specified, skipping!\n"); "\"--overwrite-extract\" is not specified, skipping!\n");
int ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, int ret = read_buf_full_from_fd(in_f, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE,
file_info->file_size, NULL); file_info->file_size, NULL);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
@ -3998,7 +4041,8 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
simple_archiver_helper_make_dirs(file_info->filename); simple_archiver_helper_make_dirs(file_info->filename);
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
FILE *out_fd = fopen(file_info->filename, "wb"); FILE *out_fd = fopen(file_info->filename, "wb");
int ret = read_fd_to_out_fd(in_f, out_fd, (char *)buf, 1024, int ret = read_fd_to_out_fd(in_f, out_fd, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE,
file_info->file_size); file_info->file_size);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
@ -4029,13 +4073,15 @@ int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract,
} else { } else {
fprintf(stderr, " File size: %lu\n", file_info->file_size); fprintf(stderr, " File size: %lu\n", file_info->file_size);
} }
int ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, int ret = read_buf_full_from_fd(in_f, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE,
file_info->file_size, NULL); file_info->file_size, NULL);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;
} }
} else { } else {
int ret = read_buf_full_from_fd(in_f, (char *)buf, 1024, int ret = read_buf_full_from_fd(in_f, (char *)buf,
SIMPLE_ARCHIVER_BUFFER_SIZE,
file_info->file_size, NULL); file_info->file_size, NULL);
if (ret != SDAS_SUCCESS) { if (ret != SDAS_SUCCESS) {
return ret; return ret;