103 lines
3.2 KiB
C
103 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
int main(int argc, char **argv){
|
|
unsigned char convert_table[] = {
|
|
//origin converted
|
|
0xeb,0x07, 0xeb,0x0a, //first address to find!
|
|
0xe1,0x00, 0xe1,0x04,
|
|
0xe7,0x04, 0xe7,0x08,
|
|
0xe2,0xc0, 0xe2,0x80,
|
|
0x60,0x13, 0xe0,0x00
|
|
};
|
|
if(argc == 1){
|
|
printf("\033[1;31m"); //red
|
|
printf("error: ");
|
|
printf("\033[0;37m"); //white
|
|
printf("no input and output file!\n");
|
|
return 1;
|
|
}
|
|
if(argc == 2){
|
|
printf("\033[1;31m"); //red
|
|
printf("error: ");
|
|
printf("\033[0;37m"); //white
|
|
printf("no output file!\n");
|
|
return 2;
|
|
}
|
|
|
|
if(access(argv[1], F_OK ) == 0) {
|
|
//none
|
|
} else {
|
|
printf("\033[1;31m"); //red
|
|
printf("error: ");
|
|
printf("\033[0;37m"); //white
|
|
printf("cannot open file!\n");
|
|
return 3;
|
|
}
|
|
printf("\033[0;37m"); //white text
|
|
|
|
FILE * file; //file pointer
|
|
file = fopen(argv[1], "rb"); //open file
|
|
fseek(file, 0L, SEEK_END); //seek file size
|
|
int filesize = ftell(file); //get file size
|
|
|
|
unsigned char* filedata = NULL; //make file data table
|
|
filedata = malloc(filesize);
|
|
for(int i=0; i<filesize; i++){
|
|
filedata[i] = 0x00;
|
|
}
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
int fread_result = fread(filedata, filesize, 1, file); //set file data in filedata table
|
|
printf("fread result: %d\n",fread_result);
|
|
|
|
fclose(file); //close input file
|
|
|
|
int addr = 0; //address of data to change
|
|
printf("filesize: %d\n\n",filesize);
|
|
|
|
//////////////////////FIND SEQUENCE TO CHANGE/////
|
|
for(int i=0; i<filesize-80; i++){
|
|
if(filedata[i] == convert_table[0] && filedata[i+1] == convert_table[1]){
|
|
//for(int c=0; c<20;c++){printf("%02x ",filedata[i+c]);}
|
|
printf("%08x %02x%02x -> %08x %02x%02x\n",i,convert_table[0],convert_table[1], i,convert_table[2],convert_table[3]);
|
|
filedata[i] = convert_table[2];
|
|
filedata[i+1] = convert_table[3];
|
|
addr = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
for(int a=4; a<sizeof(convert_table); a=a+4){
|
|
for(int i=addr; i<addr+100;i++){
|
|
if(filedata[i] == convert_table[a+0] && filedata[i+1] == convert_table[a+1]){
|
|
printf("%08x %02x%02x -> %08x %02x%02x\n",i,convert_table[a+0],convert_table[a+1], i,convert_table[a+2],convert_table[a+3]);
|
|
filedata[i] = convert_table[a+2];
|
|
filedata[i+1] = convert_table[a+3];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(addr == 0){
|
|
printf("\033[1;31m"); //red
|
|
printf("error: ");
|
|
printf("\033[0;37m"); //white
|
|
printf("no valid file or no data to edit\n");
|
|
return 4;
|
|
}
|
|
|
|
|
|
|
|
file = fopen(argv[2], "wb"); //open output file
|
|
|
|
//write final data in output file
|
|
fwrite(filedata , sizeof(char) , filesize , file);
|
|
|
|
fclose(file); //close output file
|
|
free(filedata);
|
|
return 1;
|
|
}
|