I am not sure why my images are unsupported. Everything looks as if it works until you try to open the images. Even the image sizes look correct.(meaning they aren't 0bytes or all the same size or 512 bytes) Why are the images unsupported?
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover forensic_image\n");
return 1;
}
FILE *raw = fopen(argv[1], "r");
if (raw == NULL)
{
printf("Could not open file.\n");
return 2;
}
int buffer[512];
int JPEG_num = 0;
FILE *img[50];
char filename[9];
while(true)
{
for(int i = 0; i < 512; i++)
{
fread(&buffer[i], 1, 1, raw);
}
if(buffer[0] == 0xff && buffer[1] == 0xd8 && (buffer[3] >= 0xe0 && buffer[3] <= 0xef))
{
if(JPEG_num == 0)
{
sprintf(filename, "%03i.jpg", 0);
img[0] = fopen(filename, "w");
fwrite(&buffer, 1, 512, img[0]);
JPEG_num++;
}
else
{
fclose(img[JPEG_num - 1]);
sprintf(filename, "%03i.jpg", JPEG_num);
img[JPEG_num] = fopen(filename, "w");
fwrite(&buffer, 1, 512, img[JPEG_num]);
JPEG_num++;
}
}
else
{
if(JPEG_num != 0)
{
fwrite(&buffer, 1, 512, img[JPEG_num - 1]);
}
}
if(JPEG_num >= 50)
{
return false;
}
}
fclose(img[JPEG_num - 1]);
return 0;
}