Created
August 20, 2012 10:44
-
-
Save thommyj/3403088 to your computer and use it in GitHub Desktop.
Find binary file inside other binary file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc,char **argv) | |
{ | |
FILE *afile = NULL; | |
FILE *bfile = NULL; | |
unsigned char *bbuffer = NULL; | |
int achar; | |
int bsize; | |
int acnt=0,bcnt=0,i; | |
if(argc<3){ | |
printf("Usage: %s file1 file2\r\n Program tries to find file2 (binary) inside file1\r\n",argv[0]); | |
return 1; | |
} | |
afile = fopen(argv[1], "rb"); | |
bfile = fopen(argv[2], "rb"); | |
if(afile == NULL || bfile == NULL){ | |
perror("Filename correct? "); | |
goto shutdown; | |
} | |
fseek(bfile, 0L, SEEK_END); | |
bsize = ftell(bfile); | |
fseek(bfile, 0L, SEEK_SET); | |
bbuffer = malloc(bsize); | |
if(1 != fread(bbuffer,bsize,1,bfile)){ | |
perror("Unable to read file2"); | |
goto shutdown; | |
} | |
/* for(i=0;i<bsize;i++){ | |
printf("bbuffer[%i]=%i\r\n",i,bbuffer[i]); | |
}*/ | |
//TODO: abort when not enugh bytes are left | |
while(1) | |
{ | |
achar = getc(afile); | |
acnt++; | |
if(achar == EOF){ | |
printf("Couldn't find blob2 inside blob1\r\n"); | |
goto shutdown; | |
} | |
// printf("achar: %u, acnt: %i, bbuffer[bcnt]: %u, bcnt: %i, bsize: %i\r\n",achar,acnt, bbuffer[bcnt],bcnt,bsize); | |
if((unsigned char)achar == bbuffer[bcnt]){ | |
bcnt++; | |
}else{ | |
bcnt = 0; | |
} | |
if(bcnt == bsize){ | |
printf("Found blob2 at offset 0x%08X\r\n",acnt-bcnt); | |
goto shutdown; | |
} | |
} | |
shutdown: | |
if(afile) | |
fclose(afile); | |
if(bfile) | |
fclose(bfile); | |
if(bbuffer) | |
free(bbuffer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment