From 6daff6bcdaa1a0dc59ad243fd9c1329520664b1b Mon Sep 17 00:00:00 2001 From: rgimad Date: Thu, 27 Mar 2025 01:37:29 +0300 Subject: [PATCH] add first packing method --- kpack_c.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/kpack_c.c b/kpack_c.c index b85105e..1645fd1 100644 --- a/kpack_c.c +++ b/kpack_c.c @@ -8,6 +8,11 @@ #include #include "lzma_c/LZMAEncoderApi.h" +#define METHOD_LZMA 1 +#define METHOD_FLAG_CALLTRICK 0 +#define METHOD_FLAG_CALLTRICK_1 0x40 +#define METHOD_FLAG_CALLTRICK_2 0x80 + static const char* str_usage = "Usage: %s [--nologo / -n] [--kernel / -k] [--unpack / -u] []\n"; static void error_load_infile() { @@ -20,6 +25,23 @@ static void error_malloc_failed() { exit(1); } +unsigned byteswap(unsigned val) +{ + val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF ); + return (val << 16) | (val >> 16); +} + +unsigned pack_lzma(unsigned char *infile, unsigned char *outfile, unsigned insize, unsigned char *workmem, unsigned is_kerpack) +{ + unsigned outsize = lzma_compress(infile, outfile + 11, insize, workmem, is_kerpack); + unsigned tmp = 0; + memcpy(&tmp, outfile + 12, 4); + tmp = byteswap(tmp); + memcpy(outfile + 12, &tmp, 4); + return outsize - 1; +} + + int main(int argc, char *argv[]) { bool flag_nologo = false; @@ -112,6 +134,8 @@ int main(int argc, char *argv[]) } fclose(infile_handle); + // TODO check option -k or not. Below goes packing for without -k option + // calculate maximum size of the output: unsigned max_output_size = infile_size*9/8 + 0x400; // should be enough for header printf("max_output_size = 0x%x\n", max_output_size); @@ -137,12 +161,18 @@ int main(int argc, char *argv[]) error_malloc_failed(); } - printf("Compressing ... "); + printf("Compressing ... \n"); outfile = outfile2; memcpy(outfile2, outfile1, 8); - // TODO call pack_lzma + unsigned outsize = pack_lzma(infile_buf, outfile, infile_size, workmem, 0); + printf("1. outsize = %u bytes\n", outsize); + outfilebest = outfile; + unsigned method = METHOD_LZMA; + // TODO ... return 0; -} \ No newline at end of file +} + + \ No newline at end of file