#include #include #include #include #include #include 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; }