diff options
| author | B. Watson <urchlay@slackware.uk> | 2025-11-26 05:44:24 -0500 |
|---|---|---|
| committer | B. Watson <urchlay@slackware.uk> | 2025-11-26 05:44:43 -0500 |
| commit | 664a630518f8f92a8b262e973790bbdb0dfc33ad (patch) | |
| tree | cac07182d82ed2d9304e4ebac570f704bc17aeb3 /src/alf.c | |
| parent | c5f84c999d5c04a32c3d526d16b4d58f456c2a99 (diff) | |
| download | alftools-664a630518f8f92a8b262e973790bbdb0dfc33ad.tar.gz | |
alf: Bounds checking on input and output sizes. Document unalf bug with files >=15MB. Use atarified filename in 'Crunching' message.
Diffstat (limited to 'src/alf.c')
| -rw-r--r-- | src/alf.c | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -26,7 +26,7 @@ u8 input_buf[MAX_INPUT_SIZE]; u8 output_buf[MAX_INPUT_SIZE]; u8 byte_tokens[256]; -int input_len, output_len, out_bitpos; +unsigned int input_len, output_len, out_bitpos; int opt_append = 0; int opt_overwrite = 0; @@ -145,6 +145,7 @@ void create_header(void) { char hdr_filename[13]; atarify_filename(hdr_filename); + printf("Crunching %s\n", hdr_filename); output_buf[0] = 0x1a; output_buf[1] = 0x0f; @@ -169,12 +170,19 @@ void open_input(const char *filename) { } } +void inc_output_len(void) { + if(++output_len == MAX_INPUT_SIZE) { + fprintf(stderr, "%s: fatal: compressed file would be >16MB.\n", self); + exit(1); + } +} + void append_bit(int bit) { output_buf[output_len] |= (bit << (7 - out_bitpos)); out_bitpos++; if(out_bitpos == 8) { out_bitpos = 0; - output_len++; + inc_output_len(); } } @@ -244,18 +252,20 @@ void crunch(void) { } store_token(TOK_END); - if(out_bitpos) output_len++; + if(out_bitpos) inc_output_len(); update_header(); } void crunch_file(const char *filename) { init_table(); - printf("Crunching %s\n", filename); open_input(filename); /* read in entire input, couldn't do it this way on the Atari */ input_len = fread(input_buf, 1, MAX_INPUT_SIZE - 1, in_file); + if(!feof(in_file)) { + fprintf(stderr, "%s: warning: this file is too large; only compressing the first 16MB.\n", self); + } output_len = 0; fstat(fileno(in_file), &in_file_stat); /* for timestamp */ fclose(in_file); |
