The code manages to identify the signature. However, it seems that it reads incorrect amount of bytes, because it's not identified again. Where did I do wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int checkSig(FILE* intpr,unsigned char* firsts,unsigned char* signature); // check for jpeg signature
int main(int argc, char* argv[])
{
char* Source = "card.raw"; // source file, raw data
FILE* intpr = fopen(Source,"r");
FILE* outptr;
unsigned char* firsts;
unsigned char signature[] = {0xff,0xd8,0xff}; // first 3 constant bytes of JPEG file signature
unsigned char* buffer;
char filename[ sizeof("999.jpg")]; // filename for every exported image
int c=0,s=0,k=0,f=0,e=0;
if (intpr == NULL){
printf("File %s is corrupted",Source);
return 2;
}
firsts = malloc( sizeof(unsigned char) * 4); // buffer for checking signature
buffer = malloc( sizeof(unsigned char) * 512);
while( !feof(intpr) ){
if (checkSig(intpr,firsts, &signature) == 1){ // checks for signature, return true/false
f = 1;
e = 1;
}
if (e == 1){ // if first signature found, keep writing to new image file until next signature found
s = sprintf(filename,"%03d.jpg",k);
if (s < 0){
printf("Error creating filename");
return -1;
}
if (f==1){ // if a new signature found, create a new image file
if (strcmp(filename, "000.jpg") != 0){ //avoid closing the first image file because it has not been created yet.
fclose(outptr);
}
outptr = fopen(filename,"w");
if (outptr == NULL){
printf("File %s is corrupted",filename);
return 2;
}
f = 0;
}
fread(buffer, sizeof(unsigned char) * 512, 1, intpr);
fwrite(buffer, sizeof(unsigned char) * 512, 1, outptr);
k++;
}
}
free(firsts);
free(buffer);
fclose(intpr);
fclose(outptr);
}
/* this function goes every 4 bytes and checks for a specific hex pattern */
int checkSig(FILE* intpr,unsigned char* firsts,unsigned char* signature){
int c=0;
if (fread(firsts, sizeof(unsigned char) * 4, 1, intpr) != 1){
printf("Couldn't read from file");
return -1;
}
for (int t=0; t < 3 ; t++){
if (signature[t] == firsts[t]){
c++;
}
if (c == 3){
if ((firsts[t+1]==0xe0 || firsts[t+1]==0xe1 || firsts[t+1]==0xe2 || firsts[t+1]==0xe3 || firsts[t+1]==0xe4 || firsts[t+1]==0xe5 || firsts[t+1]==0xe6 || firsts[t+1]==0xe7 || firsts[t+1]==0xe8 || firsts[t+1]==0xe9 || firsts[t+1]==0xea || firsts[t+1]==0xeb || firsts[t+1]==0xec || firsts[t+1]==0xed || firsts[t+1]==0xee || firsts[t+1]==0xef))
c++;
}
}
if (c==4){
fseek(intpr, sizeof(unsigned char) * -4, SEEK_CUR);
return 1;
}
else{
return 0;
}
}