I've now sunk ~20 hours into recover, and still can't get my code to work on the check50 data.
The issue I've seen on here that other people have been having was with checking the jpeg headers, but I think I'm doing this part correctly, as I check the fourth byte of the buffer with:
(buffer[3] & 0xf0) == 0xe0
From my understanding, the bitwise and operator should ensure that the first 4 bits of the byte are equal to 0xe, and ignore the final four bits.
I know the test data is different than card.raw, and my code must somehow be tailored to only solve card.raw, but I'm not sure what my issue is, or even how to go about fixing it.
My code:
#include <stdint.h>
#include <stdio.h>
#define BLOCK 512
typedef uint8_t (BYTE);
int main(int argc,char *argv[])
{
if(argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
//read in the file in a read state
FILE *infile = fopen(argv[1],"r");
if(infile == NULL)
{
fprintf(stderr, "Could not read in card\n");
return 2;
}
//create the buffer variable
BYTE buffer[BLOCK];
//creates name variable for image naming
char imagename[8];
//initialize counter variable for image naming
int counter = 0;
//creates null file pointer for image
FILE *img = NULL;
while(fread(&buffer, 1, BLOCK, infile)==BLOCK)
{
if(buffer[0] == 0xff && buffer[1] == 0xd8 &&
buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{ //condition to close open images before opening new ones
if(counter != 0)
{fclose(img);}
//writes name of image
sprintf(imagename, "%03d.jpg",counter++);
//opens a new image
img = fopen(imagename, "w");
}
//We skip using fseek until we find the jpeg header
if(counter==0)
{fseek(infile, BLOCK, SEEK_CUR);}
//if counter>0, we write the buffer to the image file
else{fwrite(&buffer, 1, BLOCK, img);}
}
//Closing out of our files
if(counter > 0)
{
fclose(img);
fclose(infile);
//fprintf(stdout,"Congrats you finished!\n");
return 0;
}
}
Any help would be great.