I got stuck on recover.c. It saves 10 JPG's to file but i'm only able to open the first one. Of this first JPG i can only see a small part of the upper side of the picture.
when I look at the first 4 bytes of the 2nd till the 10nd JPG, it shows a part of the JPG signature. ff or d8 but not the whole signature ffd8 ffe0 or ffd8 ffe1.
/** * recover.c * * Computer Science 50 * Problem Set 4 * * Recovers JPEGs from a forensic image. */
include
int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: ./...., infile (Compact Flash)"); return 1; }
//assign a pointer to argv[1], infile
char* infile = argv[1];
//opens the infile and allow to read. Assing to a FILE pointer
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", infile);
return 2;
}
//+1 every time a new JPG is opened
int counter = 0;
// buffer to store a part of JPG
unsigned char buffer[512];
// place to store the filename of each new JPG
char filename[10];
do
{
fread(&buffer, sizeof(buffer), 1, inptr);
}
//read till JPG begins
while (buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] != 0xe0 || buffer[3] != 0xe1));
do
{
// open a new JPG
counter = counter + 1;
sprintf(filename, "%03d.jpg", counter);
FILE* img = fopen(filename, "w");
do //read and write from the start of JPG
{
// write 512 bytes into JPG
fwrite(&buffer, sizeof(buffer), 1, img);
// read 512 bytes into buffer
fread(&buffer, sizeof(buffer), 1, inptr);
}
// while not a new JPG or end of file
while (buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] != 0xe0 || buffer[3] != 0xe1));
fclose(img);
}
// check if not end of file
while (buffer[0] != 0x00 && buffer[1] != 0x00 && buffer[2] != 0x00);
fclose(inptr);
}