summaryrefslogtreecommitdiff
path: root/src/extractblocks.c
blob: 51505ef269ce535cc51afe11fa6733730828fa13 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <strings.h>
#include <stdio.h>
#include <fcntl.h>

int main( int args, char **argv )
{
  int            toindex;
  int            i, run = 1, filenum = 0, offset = 0, oldoffset = -1;
  struct stat    fstatus;
  unsigned char *mappedfile;

  if( args != 2 )
    { fputs( "Missing filenames.", stderr ); exit( 1 ); }

  if( ( toindex = open( argv[1], O_RDONLY ) ) < 0 )
    { fprintf( stderr, "Can't open file: %s.\n", argv[1] ); exit( toindex ); }

  fstat( toindex, &fstatus );

  printf( "Size of file: %d\n", fstatus.st_size );

  if( ( mappedfile = mmap( NULL, (size_t)fstatus.st_size, PROT_READ | PROT_WRITE, MAP_NOCORE | MAP_PRIVATE, toindex, 0) ) == MAP_FAILED )
    { fprintf( stderr, "Can't mmap file: %s.", argv[1] ); exit( 1 ); }

  while( run )
  {
    while( ( offset < fstatus.st_size ) && (
           ( mappedfile[ offset + 0 ] != 0xd9 ) ||
           ( mappedfile[ offset + 2 ] != 0x6f ) ||
           ( mappedfile[ offset + 3 ] != 0x6d ) ||
           ( mappedfile[ offset + 4 ] != 0xaa ) ||
           ( mappedfile[ offset + 5 ] != 0x11 ) ||
           ( mappedfile[ offset + 6 ] != 0x6f )
          ) ) offset++;

    printf( "Found an appropriate offset at: %d\n", oldoffset );

    if( offset == fstatus.st_size )
      run = 0;

    if( oldoffset != -1 )
    {
        unsigned long *mf = (unsigned long*)(mappedfile + oldoffset);
        unsigned char filename[20], cs = 0;

        for( i=0; i<8; ++i)
          mf[i]^=0x014224c2;

        snprintf( filename, sizeof( filename ), "%05d.lha", filenum++ );

        memcpy( ((unsigned char*)mf) + 22, filename, 5);

        for( i=2; i<29; ++i)
          cs += ((unsigned char*)mf)[i];
        ((unsigned char*)mf)[1] = cs;

        i = open( filename, O_CREAT | O_TRUNC | O_WRONLY, 0644 );
        write( i, mf, offset - oldoffset );
        close( i );
    }
    oldoffset = offset;
    offset++;
  }

  return 0;
}