What it has to do: Iterate through each block of 512 bytes in input file. When found JPEG header, assuming that it is start of JPEG write it to a separate file, continue until new header is found, then close the JPEG fileand repeat to the end of the input file. Free all allocated memory and close input file.
What it actually does: Everything written above. It actually recovers all JPEG files (I am able to see and open all of them) and leaks no memory (checked with Valgrind) but still doesn't pass 'check50'.
:) recover.c exists
:) recover.c compiles
:) handles lack of forensic image
:) recovers 000.jpg correctly
:( recovers middle files correctly
:( recovers last file correctly
I've read lots of posts regarding this but still have no clue.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
//ensure proper usage
if (argc != 2){
fprintf(stderr, "Usage: ./recover imagefile.raw\n");
return 1;
}
// open input file
FILE *card = fopen(argv[1], "r");
if (card == NULL){
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
//create a buffer of 512 bytes
//pomenat'
uint8_t *buffer = malloc(sizeof(uint8_t)*512);
if ( buffer == NULL){
fprintf(stderr, "Could not create buffer.\n");
return 3;
}
//temp jpg found counter
int jpgfnd = 0;
// Counter of JPEGs
int jpg = 0;
char filename[10];
FILE *img = NULL;
while( fread(buffer, sizeof(buffer), 1, card) == 1)
{
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
if (jpgfnd == 1){
//close prev file
fclose(img);
}
//create a new file
sprintf(filename, "%03i.jpg", jpg);
img = fopen(filename, "w");
//write buffer to a new file
fwrite(buffer, sizeof(buffer), 1, img);
// change jpeg found value to 1
jpgfnd = 1;
//change jpg conter value by 1
jpg++;
}
else if (jpgfnd == 1){
//write buffer to an opened file
fwrite(buffer, sizeof(buffer), 1, img);
}
}
// close all opened files
fclose(img);
fclose(card);
// free malloced array
free(buffer);
// success
return 0;
}