diff --git a/kpack_c.c b/kpack_c.c index 194e889..ac6c977 100644 --- a/kpack_c.c +++ b/kpack_c.c @@ -14,6 +14,11 @@ static void error_load_infile() { exit(1); } +static void error_malloc_failed() { + printf("Error: memory allocation failed\n"); + exit(1); +} + int main(int argc, char *argv[]) { bool flag_nologo = false; @@ -44,7 +49,7 @@ int main(int argc, char *argv[]) return 1; } } - // Check for required infile + // check for required infile if (optind >= argc) { printf("Error: Input file is required.\n"); printf(str_usage, argv[0]); @@ -52,7 +57,7 @@ int main(int argc, char *argv[]) } infile_name = argv[optind]; - // Optional outfile + // optional outfile if (optind + 1 < argc) { outfile_name = argv[optind + 1]; optind++; @@ -98,8 +103,7 @@ int main(int argc, char *argv[]) unsigned char *infile_buf = malloc(infile_size); if (infile_buf == NULL) { - printf("Error: memory allocation failed\n"); - return 1; + error_malloc_failed(); } size_t bytes_read = fread(infile_buf, 1, infile_size, infile_handle); if (bytes_read != infile_size) { @@ -107,9 +111,16 @@ int main(int argc, char *argv[]) } fclose(infile_handle); - // printf("file contents = %s\n", infile_buf); + // calculate maximum size of the output: + unsigned bufsize = infile_size*9/8 + 0x400; // should be enough for header + printf("bufsize = 0x%x\n", bufsize); + unsigned char *buf = malloc(2*bufsize); // allocate memory for two copies of maximum output + if (buf == NULL) { + error_malloc_failed(); + } // TODO ... + // https://git.kolibrios.org/KolibriOS/kolibrios/src/commit/293e1d195a14d01a0f8812a954ff1449048f6209/programs/other/kpack/kerpack_linux/kpack64.asm#L236 return 0; } \ No newline at end of file