summaryrefslogtreecommitdiff
path: root/src/extractstreets.c
blob: 097a73682c5633ea8c2f67b08722b767cb2716bf (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
#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 ] != 0x22 ) ||
           ( mappedfile[ offset + 2 ] != 0x2d ) ||
           ( mappedfile[ offset + 3 ] != 0x6c ) ||
           ( mappedfile[ offset + 4 ] != 0x68 ) ||
         ( ( mappedfile[ offset + 5 ] != 0x35 ) && ( mappedfile[ offset + 5 ] != 0x30 )) ||
           ( mappedfile[ offset + 6 ] != 0x2d )
          ) ) 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;

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

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

        for( i=2; i<36; ++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;
}