I can't figure out what is wrong with my code. I recovered images from 000.jpg to 050.jpg. This is my code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// only take filename as input
if (argc != 2)
{
fprintf(stderr, "./recover file\n");
return 1;
}
// open file
FILE *fptr = fopen(argv[1], "r");
if (fptr == NULL)
{
fprintf(stderr, "Cannot open file\n");
}
// array to store
unsigned char *buffer = malloc(sizeof(BYTE) * 512);
// counter
int j = 0;
// file to write into
FILE *pic = NULL;
while (fread(buffer, 512, 1, fptr))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
j++;
}
if (j > 2)
{
fclose(pic);
}
char temp[100];
sprintf(temp, "%.03i.jpg", j);
pic = fopen(temp, "a");
// write from temp
fwrite(buffer, 512, 1, pic);
}
fclose(fptr);
fclose(pic);
return 0;
}
It shows this error message
:) 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
recovered image does not match
Please help!