I have been working with recover for a long time and I have a great problem with detecting the beginning of a JPEG. I have tried for a long time to no avail. I feel my problem may be with buffer but I'm not so sure. All help is appreciated.
`
include
include
int main(int argc, char *argv[])
{
// checking if a file name is entered
if(argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
//Assigning a value to the file entered
char *image = argv[1];
// opening a file to read from
FILE *JPG = fopen(image, "r");
// checking if file could be opened
if (JPG == NULL)
{
printf("Could not open %s.\n", image);
return 2;
}
// declaring buffer, my counter and img
unsigned char buffer[513];
int n = 000;
FILE* img = NULL;
int x = fread(buffer, 1, 511, JPG);
while (1 == 1)
{
//reading the start of a file
fread(buffer, 1, 511, JPG);
if (fread(buffer, 1, 512, JPG) == 0 && n != 0)
{
return 0;
}
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
img = fopen(image, "w");
char filename[8];
sprintf(filename, "%03i.jpg", n);
//writing the bytes obtained while no start of a JPEG is found
while (1 == 1)
{
x = fread(buffer, 1, 512, JPG);
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
break;
}
if (x != 512)
{
return 0;
}
fwrite(buffer, 1, 512, img);
}
fclose(img);
fseek(JPG, -512, SEEK_CUR);
n++;
}
}
}
`