summaryrefslogtreecommitdiff
path: root/src/extractblocks_new.c
blob: 0a4792edad24ea2c2bd196572e99149680d0635c (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
70
71
72
73
#include <fcntl.h>
#include "mystdlib.h"
#include <stdlib.h>
#include <string.h>

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

  unsigned char mantra0 = 0x1b, mantra1 = 0x35, mantra2 = 0x2d, mantra3 = 0x6c; /* clean*/
//  unsigned char mantra0 = 0x08, mantra1 = 0x57, mantra2 = 0x64, mantra3 = 0x69; /* 98 H */
//  unsigned char mantra0 = 0x13, mantra1 = 0xe4, mantra2 = 0x55, mantra3 = 0x05; /* 99 F */
//  unsigned char mantra0 = 0x12, mantra1 = 0x08, mantra2 = 0x23, mantra3 = 0x01; /* 99 H */

  unsigned long mantra;

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

  map = map_file( argv[1], 1 );
  mappedfile = map->addr;

  mantra0 ^= mappedfile[0]; 
  mantra1 ^= mappedfile[5]; 
  mantra2 ^= mappedfile[2]; 
  mantra3 ^= mappedfile[3]; 

  mantra = mantra3 + (mantra2<<8) + (mantra1<<16) + (mantra0<<24);

  while( run )
  {
    while( ( offset < map->size ) && (
           ( mappedfile[ offset + 0 ] != ( 0x1b^ mantra0 )) ||
           ( mappedfile[ offset + 2 ] != ( '-' ^ mantra2 )) ||
           ( mappedfile[ offset + 3 ] != ( 'l' ^ mantra3 )) ||
           ( mappedfile[ offset + 4 ] != ( 'h' ^ mantra0 )) ||
           ( mappedfile[ offset + 5 ] != ( '5' ^ mantra1 )) ||
           ( mappedfile[ offset + 6 ] != ( '-' ^ mantra2 ))
          ) ) offset++;

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

    if( offset == map->size )
      run = 0;

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

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

        snprintf( filename, sizeof( filename ), "%05d.lha", filenum++ );
        memcpy( ((unsigned char*)df) + 22, filename, 5);

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

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

  return 0;
}