From 6d653ae18370f49c25eeee6f525f07f9d9095b3c Mon Sep 17 00:00:00 2001 From: rgimad Date: Sun, 23 Mar 2025 19:45:33 +0300 Subject: [PATCH] added and tested input file size checks --- kpack_c.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/kpack_c.c b/kpack_c.c index f1076e8..ff96625 100644 --- a/kpack_c.c +++ b/kpack_c.c @@ -1,9 +1,11 @@ #include #include +#include +#include +#include #include #include -#include -#include +#include /* TODO @@ -47,8 +49,8 @@ int main(int argc, char *argv[]) } // Check for required infile if (optind >= argc) { - fprintf(stderr, "Error: Input file is required.\n"); - fprintf(stderr, str_usage, argv[0]); + printf("Error: Input file is required.\n"); + printf(str_usage, argv[0]); return 1; } infile = argv[optind]; @@ -61,8 +63,8 @@ int main(int argc, char *argv[]) // printf("argc = %d, optind = %d\n", argc - 1, optind); if (argc - 1 > optind) { - fprintf(stderr, "Error: too many arguments\n"); - fprintf(stderr, str_usage, argv[0]); + printf("Error: too many arguments\n"); + printf(str_usage, argv[0]); return 1; } @@ -72,8 +74,32 @@ int main(int argc, char *argv[]) printf("unpack: %s\n", unpack ? "true" : "false"); printf("infile: %s\n", infile); printf("outfile: %s\n", outfile ? outfile : "Not specified"); + printf("\n"); - // TODO logic here + if (!nologo) { + printf("KPackC - Kolibri Packer in C\nUses LZMA v4.32 compression library\n\n"); + } + + struct stat64 statbuf; + int res_stat = stat64(infile, &statbuf); + if (res_stat == -1) { + printf("Error: cannot load input file\n"); + return 1; + } + // printf("statbuf.st_size = %lld\n", statbuf.st_size); + + if (statbuf.st_size == 0 || statbuf.st_size >= 4294967296LL) { + printf("Error: input file must be non zero and less than 4G\n"); + return 1; + } + unsigned infile_size = (unsigned int)statbuf.st_size; + printf("infile_size = %u\n", infile_size); + + FILE *infile_handle = fopen(infile, "rb"); + if (!infile_handle) { + printf("Error: cannot load input file\n"); + return 1; + } return 0; } \ No newline at end of file