diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/extractblocks_new.c | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/src/extractblocks_new.c b/src/extractblocks_new.c index 0a4792e..94ab0b7 100644 --- a/src/extractblocks_new.c +++ b/src/extractblocks_new.c | |||
@@ -2,35 +2,33 @@ | |||
2 | #include "mystdlib.h" | 2 | #include "mystdlib.h" |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <stdint.h> | ||
6 | #include <unistd.h> | ||
5 | 7 | ||
6 | int main( int args, char **argv ) | 8 | int main( int args, char **argv ) |
7 | { | 9 | { |
8 | int toindex; | 10 | int filenum = 0; |
9 | int i, run = 1, filenum = 0, offset = 0, oldoffset = -1; | 11 | size_t offset = 0, oldoffset = -1; |
10 | unsigned char *mappedfile; | 12 | uint8_t *mappedfile; |
11 | MAP map; | 13 | MAP map; |
12 | 14 | ||
13 | unsigned char mantra0 = 0x1b, mantra1 = 0x35, mantra2 = 0x2d, mantra3 = 0x6c; /* clean*/ | 15 | uint8_t mantra0 = 0x1b, mantra1 = 0x35, mantra2 = 0x2d, mantra3 = 0x6c; /* clean*/ |
14 | // unsigned char mantra0 = 0x08, mantra1 = 0x57, mantra2 = 0x64, mantra3 = 0x69; /* 98 H */ | 16 | // unsigned char mantra0 = 0x08, mantra1 = 0x57, mantra2 = 0x64, mantra3 = 0x69; /* 98 H */ |
15 | // unsigned char mantra0 = 0x13, mantra1 = 0xe4, mantra2 = 0x55, mantra3 = 0x05; /* 99 F */ | 17 | // unsigned char mantra0 = 0x13, mantra1 = 0xe4, mantra2 = 0x55, mantra3 = 0x05; /* 99 F */ |
16 | // unsigned char mantra0 = 0x12, mantra1 = 0x08, mantra2 = 0x23, mantra3 = 0x01; /* 99 H */ | 18 | // unsigned char mantra0 = 0x12, mantra1 = 0x08, mantra2 = 0x23, mantra3 = 0x01; /* 99 H */ |
17 | 19 | ||
18 | unsigned long mantra; | ||
19 | |||
20 | if( args != 2 ) | 20 | if( args != 2 ) |
21 | { fputs( "Missing filenames.", stderr ); exit( 1 ); } | 21 | { fputs( "Missing filenames.", stderr ); exit( 1 ); } |
22 | 22 | ||
23 | map = map_file( argv[1], 1 ); | 23 | map = map_file( argv[1], 1 ); |
24 | mappedfile = map->addr; | 24 | mappedfile = map->addr; |
25 | 25 | ||
26 | mantra0 ^= mappedfile[0]; | 26 | mantra0 ^= mappedfile[0]; |
27 | mantra1 ^= mappedfile[5]; | 27 | mantra1 ^= mappedfile[5]; |
28 | mantra2 ^= mappedfile[2]; | 28 | mantra2 ^= mappedfile[2]; |
29 | mantra3 ^= mappedfile[3]; | 29 | mantra3 ^= mappedfile[3]; |
30 | |||
31 | mantra = mantra3 + (mantra2<<8) + (mantra1<<16) + (mantra0<<24); | ||
32 | 30 | ||
33 | while( run ) | 31 | while( 1 ) |
34 | { | 32 | { |
35 | while( ( offset < map->size ) && ( | 33 | while( ( offset < map->size ) && ( |
36 | ( mappedfile[ offset + 0 ] != ( 0x1b^ mantra0 )) || | 34 | ( mappedfile[ offset + 0 ] != ( 0x1b^ mantra0 )) || |
@@ -41,28 +39,37 @@ int main( int args, char **argv ) | |||
41 | ( mappedfile[ offset + 6 ] != ( '-' ^ mantra2 )) | 39 | ( mappedfile[ offset + 6 ] != ( '-' ^ mantra2 )) |
42 | ) ) offset++; | 40 | ) ) offset++; |
43 | 41 | ||
44 | printf( "Found an appropriate offset at: %d\n", oldoffset ); | 42 | printf( "Found an appropriate offset at: %zd\n", oldoffset ); |
45 | 43 | ||
46 | if( offset == map->size ) | 44 | if( offset == map->size ) |
47 | run = 0; | 45 | { |
46 | unmap_file( &map ); | ||
47 | exit(0); | ||
48 | } | ||
48 | 49 | ||
49 | if( oldoffset != -1 ) | 50 | if( oldoffset != -1 ) |
50 | { | 51 | { |
51 | unsigned long *mf = (unsigned long*)(mappedfile + oldoffset); | 52 | uint8_t cs = 0, *mf = mappedfile + oldoffset, df[32]; |
52 | unsigned long df[8]; | 53 | char filename[20]; |
53 | char filename[20], cs = 0; | 54 | int i; |
54 | 55 | ||
55 | for( i=0; i<8; ++i) df[i]=mf[i]^mantra; | 56 | for( i=0; i<8; ++i) |
57 | { | ||
58 | df[4*i+0] = mf[4*i+0] ^ mantra0; | ||
59 | df[4*i+1] = mf[4*i+1] ^ mantra1; | ||
60 | df[4*i+2] = mf[4*i+2] ^ mantra2; | ||
61 | df[4*i+3] = mf[4*i+3] ^ mantra3; | ||
62 | } | ||
56 | 63 | ||
57 | snprintf( filename, sizeof( filename ), "%05d.lha", filenum++ ); | 64 | snprintf( filename, sizeof( filename ), "%05d.lha", filenum++ ); |
58 | memcpy( ((unsigned char*)df) + 22, filename, 5); | 65 | memcpy( ((uint8_t*)df) + 22, filename, 5); |
59 | 66 | ||
60 | for( i=2; i<29; ++i) cs += ((unsigned char*)df)[i]; | 67 | for( i=2; i<29; ++i) cs += ((uint8_t*)df)[i]; |
61 | ((unsigned char*)df)[1] = cs; | 68 | ((uint8_t*)df)[1] = cs; |
62 | 69 | ||
63 | i = open( filename, O_CREAT | O_TRUNC | O_WRONLY, 0644 ); | 70 | i = open( filename, O_CREAT | O_TRUNC | O_WRONLY, 0644 ); |
64 | write( i, df, 32 ); | 71 | write( i, df, 32 ); |
65 | write( i, mf + 8, offset - oldoffset - 32 ); | 72 | write( i, mf + 32, offset - oldoffset - 32 ); |
66 | close( i ); | 73 | close( i ); |
67 | } | 74 | } |
68 | oldoffset = offset; | 75 | oldoffset = offset; |