From a31fae7960ed0ada95813cd5e0160c194cb81ebb Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 18 Aug 2024 12:52:07 +0200 Subject: Make zlib wrapper in v4 exporter more robust and easier to read --- src/export/extract_version_4.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/export/extract_version_4.c b/src/export/extract_version_4.c index 1dbaf40..e014e23 100644 --- a/src/export/extract_version_4.c +++ b/src/export/extract_version_4.c @@ -7,44 +7,56 @@ #include "mystdlib.h" #define XORLEN (29) +#define HUGEINBLOCK (128*1024) #define HUGEBLOCK (4*1024*1024) int main(int argc, char **argv) { unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds."; - unsigned char input [XORLEN]; + unsigned char input [HUGEINBLOCK]; unsigned char output [HUGEBLOCK]; char respath[32]; /* file_XXXXX\0 */ int zres = 0, filenum = 0, resfile; - size_t i, offs = 0, reported = 0; + size_t offs = 0, reported = 0; ssize_t temp = 0; MAP in; if( argc != 2 ) exit(111); in = map_file( argv[1], 1 ); - z_stream z; memset( &z, 0, sizeof(z)); + z_stream z; while( offs < in->size ) { - size_t inlen = offs + XORLEN < in->size ? XORLEN : in->size - offs; - for( i=0; iaddr[offs+i] ^ xorkey[i]; + /* std::min(remain, HUGEINBLOCK) */ + size_t inlen = in->size - offs; + if (inlen > sizeof(input)) + inlen = sizeof(input); + + /* Copy in block large enough */ + memcpy(input, in->addr + offs, inlen); + + /* De-"crypt" */ + for (size_t i = 0; i < sizeof(xorkey); ++i ) + input[i] ^= xorkey[i]; + + /* Prepare decompression struct */ + memset( &z, 0, sizeof(z)); z.next_in = input; z.avail_in = inlen; z.next_out = output; z.avail_out = HUGEBLOCK; - inflateInit( &z ); zres = inflate( &z, Z_NO_FLUSH ); - if( (zres != Z_OK) && (zres != Z_STREAM_END) ) - goto error_continue; + inflateInit( &z ); + zres = Z_OK; - z.next_in = in->addr + offs + inlen; - z.avail_in = (unsigned int)(in->size - offs - inlen); - while( zres == Z_OK ) zres = inflate( &z, Z_NO_FLUSH ); + while( zres == Z_OK ) + zres = inflate( &z, Z_NO_FLUSH ); if( zres != Z_STREAM_END ) { -error_continue: - inflateEnd(&z); memset( &z, 0, sizeof(z)); + inflateEnd(&z); offs++; continue; } - sprintf( respath, "file_%05X", filenum++ ); + // fprintf( stderr, "%08X\n", (unsigned int)(offs)); + //old_offs = offs; + snprintf( respath, sizeof(respath), "file_%05X", filenum++ ); resfile = open( respath, O_RDWR | O_CREAT, 0644 ); if( resfile < 0 ) { -- cgit v1.2.3