summaryrefslogtreecommitdiff
path: root/src/extractblocks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/extractblocks.c')
-rw-r--r--src/extractblocks.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/extractblocks.c b/src/extractblocks.c
new file mode 100644
index 0000000..51505ef
--- /dev/null
+++ b/src/extractblocks.c
@@ -0,0 +1,69 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <sys/mman.h>
4#include <strings.h>
5#include <stdio.h>
6#include <fcntl.h>
7
8int main( int args, char **argv )
9{
10 int toindex;
11 int i, run = 1, filenum = 0, offset = 0, oldoffset = -1;
12 struct stat fstatus;
13 unsigned char *mappedfile;
14
15 if( args != 2 )
16 { fputs( "Missing filenames.", stderr ); exit( 1 ); }
17
18 if( ( toindex = open( argv[1], O_RDONLY ) ) < 0 )
19 { fprintf( stderr, "Can't open file: %s.\n", argv[1] ); exit( toindex ); }
20
21 fstat( toindex, &fstatus );
22
23 printf( "Size of file: %d\n", fstatus.st_size );
24
25 if( ( mappedfile = mmap( NULL, (size_t)fstatus.st_size, PROT_READ | PROT_WRITE, MAP_NOCORE | MAP_PRIVATE, toindex, 0) ) == MAP_FAILED )
26 { fprintf( stderr, "Can't mmap file: %s.", argv[1] ); exit( 1 ); }
27
28 while( run )
29 {
30 while( ( offset < fstatus.st_size ) && (
31 ( mappedfile[ offset + 0 ] != 0xd9 ) ||
32 ( mappedfile[ offset + 2 ] != 0x6f ) ||
33 ( mappedfile[ offset + 3 ] != 0x6d ) ||
34 ( mappedfile[ offset + 4 ] != 0xaa ) ||
35 ( mappedfile[ offset + 5 ] != 0x11 ) ||
36 ( mappedfile[ offset + 6 ] != 0x6f )
37 ) ) offset++;
38
39 printf( "Found an appropriate offset at: %d\n", oldoffset );
40
41 if( offset == fstatus.st_size )
42 run = 0;
43
44 if( oldoffset != -1 )
45 {
46 unsigned long *mf = (unsigned long*)(mappedfile + oldoffset);
47 unsigned char filename[20], cs = 0;
48
49 for( i=0; i<8; ++i)
50 mf[i]^=0x014224c2;
51
52 snprintf( filename, sizeof( filename ), "%05d.lha", filenum++ );
53
54 memcpy( ((unsigned char*)mf) + 22, filename, 5);
55
56 for( i=2; i<29; ++i)
57 cs += ((unsigned char*)mf)[i];
58 ((unsigned char*)mf)[1] = cs;
59
60 i = open( filename, O_CREAT | O_TRUNC | O_WRONLY, 0644 );
61 write( i, mf, offset - oldoffset );
62 close( i );
63 }
64 oldoffset = offset;
65 offset++;
66 }
67
68 return 0;
69}