summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2014-02-08 23:39:56 +0100
committerDirk Engling <erdgeist@erdgeist.org>2014-02-08 23:39:56 +0100
commit4affb3811c4a57cb4e033ddd40f5e99236b59250 (patch)
tree87474b22efd29e638d79169f3ea9dd95b52591fe
parenta9d5ab4dbf4a7c3e2caf54c96daecc8468eacf0c (diff)
Speed up decompress script by avoiding to wastefully copying a megabyte of data each time we scan for a new decompression offset
-rw-r--r--src/decompress.c55
1 files changed, 29 insertions, 26 deletions
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 @@
1#include <zlib.h> 1#include <stdlib.h>
2#include <stdio.h> 2#include <stdio.h>
3#include <unistd.h> 3#include <unistd.h>
4#include <fcntl.h> 4#include <fcntl.h>
5#include <string.h> 5#include <string.h>
6#include <zlib.h>
7#include "mystdlib.h"
6 8
7#define XORLEN (29) 9#define XORLEN (29)
8#define HUGEBLOCK (1024*1024) 10#define HUGEBLOCK (1024*1024)
9 11
10int main(int argc, char **argv) { 12int main(int argc, char **argv) {
11// int infile = open("/Volumes/DasTelefonbuch/atb/phonebook.db", O_RDONLY); 13 MAP in = map_file( argv[1], 1 );
12
13 int infile = open( argv[1], O_RDONLY);
14 14
15 unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds."; 15 unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds.";
16 unsigned char input [HUGEBLOCK]; 16 unsigned char input [XORLEN];
17 unsigned char output [HUGEBLOCK]; 17 unsigned char output [HUGEBLOCK];
18 char respath[32]; /* file_XXXXX\0 */ 18 char respath[32]; /* file_XXXXX\0 */
19 int i, offs = 0, zres = 0, filenum = 0, resfile, avail=1; 19 int i, zres = 0, filenum = 0, resfile;
20 z_stream z; memset( &z, 0, sizeof(z)); 20 size_t offs = 0;
21 21
22 while( avail ) { 22 z_stream z; memset( &z, 0, sizeof(z));
23 do {
24 lseek( infile, offs, SEEK_SET );
25 avail = read( infile, input, HUGEBLOCK );
26 if( !avail) break;
27 for( i=0; i<XORLEN; ++i ) input[i] ^= xorkey[i];
28 23
29 z.next_in = input; z.avail_in = avail; 24 while( offs < in->size - XORLEN ) {
30 z.next_out = output; z.avail_out = HUGEBLOCK; 25 for( i=0; i<XORLEN; ++i ) input[i] = in->addr[offs+i] ^ xorkey[i];
31 inflateInit( &z ); zres = inflate( &z, Z_NO_FLUSH ); 26 z.next_in = input; z.avail_in = XORLEN;
27 z.next_out = output; z.avail_out = HUGEBLOCK;
28 inflateInit( &z ); zres = inflate( &z, Z_NO_FLUSH );
29 if( (zres != Z_OK) && (zres != Z_STREAM_END) )
30 goto error_continue;
32 31
33 if( (zres != Z_OK) && (zres != Z_STREAM_END) ) { 32 z.next_in = in->addr + offs + XORLEN; z.avail_in = in->size - offs - XORLEN;
34 inflateEnd(&z); memset( &z, 0, sizeof(z)); 33 while( zres == Z_OK ) zres = inflate( &z, Z_NO_FLUSH );
35 offs++;
36 }
37 } while((zres != Z_OK) && (zres != Z_STREAM_END));
38 34
39 if( !avail ) break; 35 if( zres != Z_STREAM_END ) {
40 if( zres == Z_OK) while( inflate( &z, Z_NO_FLUSH ) == Z_OK ); 36error_continue:
37 inflateEnd(&z); memset( &z, 0, sizeof(z));
38 offs++;
39 continue;
40 }
41 41
42 sprintf( respath, "file_%05X", filenum++ ); 42 sprintf( respath, "file_%05X", filenum++ );
43 resfile = open( respath, O_RDWR | O_CREAT, 0644 ); 43 resfile = open( respath, O_RDWR | O_CREAT, 0644 );
44 /* I know, I know, error checking */ 44 if( resfile < 0 ) {
45 fprintf( stderr, "Could not open output file %s\n", respath );
46 exit(1);
47 }
45 write( resfile, output, z.total_out ); 48 write( resfile, output, z.total_out );
46 close( resfile ); 49 close( resfile );
47 offs += z.total_in; 50 offs += z.total_in;
48 51
49 inflateEnd(&z); memset( &z, 0, sizeof(z)); 52 inflateEnd(&z); memset( &z, 0, sizeof(z));
50 } 53 }
51 close( infile ); 54 unmap_file(&in);
52} 55}