Struggling with recover at this moment. My code creates just 8 pictures out of 50 - from 2nd to 9th. Other jpegs are also made but i am unable to see them due to "Invalid or Unsupported Image Format". My code is below. Would be grateful for any help!
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
char *infile = argv[1];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
BYTE buffer[512];
char filename[8];
int jpgcounter = 0;
FILE *img = NULL;
while(fread(&buffer, 512, 1, inptr) == 1)
{
if ((buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0) || img == NULL)
{
if(img != NULL)
{
fclose(img);
img = NULL;
}
sprintf(filename, "00%i.jpg", jpgcounter);
img = fopen(filename, "w");
if(img == NULL)
{
fprintf(stderr, "Could not open %s.\n", filename);
return 4;
}
jpgcounter++;
}
if (img != NULL)
fwrite(&buffer, 512, 1, img);
}
if(img != NULL)
fclose(img);
if(inptr != NULL)
fclose(inptr);
return 0;
}