summaryrefslogtreecommitdiff
path: root/src/decompress.c
blob: 6074db60bfd2ebbeb72029e6dfb459a854a12029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <zlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.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);

  unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds.";
  unsigned char input  [HUGEBLOCK];
  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));

  while( avail ) {
    do {
      lseek( infile, offs, SEEK_SET );
      avail = read( infile, input, HUGEBLOCK );
      if( !avail) break;
      for( i=0; i<XORLEN; ++i ) input[i] ^= xorkey[i];

      z.next_in  = input;  z.avail_in  = avail;
      z.next_out = output; z.avail_out = HUGEBLOCK;
      inflateInit( &z ); zres = inflate( &z, Z_NO_FLUSH );

      if( (zres != Z_OK) && (zres != Z_STREAM_END) ) {
        inflateEnd(&z); memset( &z, 0, sizeof(z)); 
        offs++;
      }
    } while((zres != Z_OK) && (zres != Z_STREAM_END));

    if( !avail ) break;
    if( zres == Z_OK) while( inflate( &z, Z_NO_FLUSH ) == Z_OK );

    sprintf( respath, "file_%05X", filenum++ );
    resfile = open( respath, O_RDWR | O_CREAT, 0644 );
    /* I know, I know, error checking */
    write( resfile, output, z.total_out );
    close( resfile );
    offs += z.total_in;

    inflateEnd(&z); memset( &z, 0, sizeof(z)); 
  }
  close( infile );
}