I am trying to recover the deleted files in card.raw (argv[1]) I managed to open the jpeg files in order(001 to 049) but the problem is, when I click on jpg files, the following message show "invalid or unsupported image format" with blank images.
And I am still looking for the problem in my code.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char *argv[])
{
// ensure proper usage 2arguments and .raw file
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover filename.raw\n");
return 1;
}
// open input file
FILE *memCard = fopen(argv[1], "r");
if (memCard == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
FILE *img=NULL;
char filename[8];
int count=0;
uint8_t buffer[512];
// if there is a block with 512bytes long continue the process
while( fread(buffer, 512, 1, memCard) == 1){
//Start of new jpeg file
if( buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0){
//close current image file if there is any.
if(img != NULL){
fclose(img);
}
sprintf(filename, "%03i.jpg", count);
img = fopen(filename, "w");
fwrite(memCard, 512, 1, img);
count++;
}else{
// jpg file already open, continue writing
if(img != NULL){
fwrite(memCard, 512, 1, img);
}
}
}
// close infile
fclose(memCard);
// close outfile
fclose(img);
// success
return 0;
}