From 4affb3811c4a57cb4e033ddd40f5e99236b59250 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 8 Feb 2014 23:39:56 +0100 Subject: Speed up decompress script by avoiding to wastefully copying a megabyte of data each time we scan for a new decompression offset --- src/decompress.c | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/decompress.c b/src/decompress.c index 6074db6..fbf33ec 100644 --- a/src/decompress.c +++ b/src/decompress.c @@ -1,52 +1,55 @@ -#include +#include #include #include #include #include +#include +#include "mystdlib.h" #define XORLEN (29) #define HUGEBLOCK (1024*1024) int main(int argc, char **argv) { -// int infile = open("/Volumes/DasTelefonbuch/atb/phonebook.db", O_RDONLY); - - int infile = open( argv[1], O_RDONLY); + MAP in = map_file( argv[1], 1 ); unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds."; - unsigned char input [HUGEBLOCK]; + unsigned char input [XORLEN]; unsigned char output [HUGEBLOCK]; char respath[32]; /* file_XXXXX\0 */ - int i, offs = 0, zres = 0, filenum = 0, resfile, avail=1; - z_stream z; memset( &z, 0, sizeof(z)); + int i, zres = 0, filenum = 0, resfile; + size_t offs = 0; - while( avail ) { - do { - lseek( infile, offs, SEEK_SET ); - avail = read( infile, input, HUGEBLOCK ); - if( !avail) break; - for( i=0; isize - XORLEN ) { + for( i=0; iaddr[offs+i] ^ xorkey[i]; + z.next_in = input; z.avail_in = XORLEN; + 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; - if( (zres != Z_OK) && (zres != Z_STREAM_END) ) { - inflateEnd(&z); memset( &z, 0, sizeof(z)); - offs++; - } - } while((zres != Z_OK) && (zres != Z_STREAM_END)); + z.next_in = in->addr + offs + XORLEN; z.avail_in = in->size - offs - XORLEN; + while( zres == Z_OK ) zres = inflate( &z, Z_NO_FLUSH ); - if( !avail ) break; - if( zres == Z_OK) while( inflate( &z, Z_NO_FLUSH ) == Z_OK ); + if( zres != Z_STREAM_END ) { +error_continue: + inflateEnd(&z); memset( &z, 0, sizeof(z)); + offs++; + continue; + } sprintf( respath, "file_%05X", filenum++ ); resfile = open( respath, O_RDWR | O_CREAT, 0644 ); - /* I know, I know, error checking */ + if( resfile < 0 ) { + fprintf( stderr, "Could not open output file %s\n", respath ); + exit(1); + } write( resfile, output, z.total_out ); close( resfile ); offs += z.total_in; - inflateEnd(&z); memset( &z, 0, sizeof(z)); + inflateEnd(&z); memset( &z, 0, sizeof(z)); } - close( infile ); + unmap_file(&in); } -- cgit v1.2.3