Originally, my code always returned Segmentation Faults whenever running it. I used printf statements to find and fix the segmentation faults, but now it is only finding 22 out of the 50 photos and are all corrupted/incomplete. I've been working on this problem for a couple of days and can't seem to identify where I'm making my mistakes! Any help would be appreciated!
Check50 Results: :) recover.c exists. :) recover.c compiles. :) handles lack of forensic image :( recovers 000.jpg correctly recovered image does not match :( recovers middle images correctly recovered image does not match :( recovers 049.jpg correctly 049.jpg not found
include
include
include
int main(int argc, char *argv[]) { //Declaration typedef uint8_t BYTE; BYTE buffer[512]; char filename[8]; FILE *memorycard; FILE *img = NULL; int counter = 0;
//Check for correct command line usage
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
//Open memory card (inptr)
char *file = argv[1];
memorycard = fopen(file, "r");
//Check if memorycard OR argv[1] is empty
if (memorycard == NULL)
{
printf("Could not open %s\n", argv[1]);
return 1;
}
//Loop until end of 512 block
while (fread(buffer, sizeof(BYTE), 512, memorycard) == 512)
{
//Read 512 bytes into buffer
fread(buffer, sizeof(BYTE), 512, memorycard);
//If JPEG header is found
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
//If first JPEG (counter == 0)
if (counter == 0)
{
sprintf(filename, "%03i.jpg", counter); //Create new filename to write into
img = fopen(filename, "w"); //Open img (outptr) to put filename into
fwrite(buffer, sizeof(BYTE), 512, img); //Write
counter++;
}
//If not first JPEG
else if (counter > 0)
{
//Close open filename
fclose(img);
sprintf(filename, "%03i.jpg", counter); //Create new filename to write into
img = fopen(filename, "w"); //Open img (outptr) to put filename into
fwrite(buffer, sizeof(BYTE), 512, img); //Write
counter++;
}
}
//If no JPEG header is found
else
{
//Continue writing in to img (outptr)
if (img != NULL)
{
fwrite(buffer, sizeof(BYTE), 512, img); //Write
}
}
}
//Close all remaining files
fclose(img);
fclose(memorycard);
}