I'm having trouble finding the issue with my code. It successfully recovers 50 JPEG files 000 - 049 which can all be opened and viewed and seem correct. But my code fails check50. Does anybody have any ideas?
:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
000.jpg not found
:( recovers middle images correctly
001.jpg not found
:( recovers 049.jpg correctly
049.jpg not found
My code is as follows:
typedef uint8_t byte;
//Function prototype
bool isJPEG(byte *block);
// Global Variables
int picnumber = 0;
bool firstJPEG;
FILE* input = NULL;
FILE* outfile = NULL;
int main(int argc, char *argv[])
{
// Checks that one command-line argument has been entered, if so opens memory card
if (argc != 2)
{
printf("Usage: ./recover [___.raw]");
return 1;
}
else
{
input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open %s", argv[1]);
return 1;
}
}
byte block[512];
// Loop through the file until no more data
while (fread(&block, 512, 1, input) == 1)
{
// Search through file for JPEG headers
if (isJPEG(block) == true)
{
// First JPEG header found, set first JPEG to true
if (firstJPEG == false)
{
firstJPEG = true;
}
// Already a JPEG in the block. Close it
else
{
fclose(outfile);
}
// Open and Print out recovered JPEGs into outfile.
char filename[8];
sprintf(filename, "%03i.jpg", picnumber++);
outfile = fopen(filename, "w");
if (outfile == NULL)
{
return 1;
}
fwrite(&block, 512, 1, outfile);
}
// If finding another JPEG which is not the first, continue writing JPEGs to outfile
else if (firstJPEG == true)
{
fwrite(&block, 512, 1, outfile);
}
}
// End of memory card - close remaining files
fclose(input);
fclose(outfile);
return 0;
}
bool isJPEG (byte *block)
{
// Scan block to see if it is a JPEG
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
{
return true;
}
else
{
return false;
}
}