#include <stdio.h> #include <stdlib.h> #include <stdint.h>
//number of bytes in jpeg file const int BLOCK_SIZE = 512;
int main(int argc, char *argv[]) {
//ensure proper use
if(argc != 2)
{
printf("Usage: ./recover [IMAGE]\n");
return 1;
}
char* infile = argv[1];
char* outfile = argv[1];
//open input file
FILE *input = fopen(infile, "r");
if(input==NULL)
{
printf("Unable to open\n");
return 1;
}
//open output file
FILE *output = fopen(outfile, "w");
if(output==NULL)
{
fclose(input);
printf("Could not create\n");
return 1;
}
typedef uint8_t BYTE;
BYTE header[BLOCK_SIZE];
fread(header, sizeof(BLOCK_SIZE), 1, input);
fwrite(header, sizeof(BLOCK_SIZE), 1, output);
//buffer file
int16_t buffer[BLOCK_SIZE];
while(fread(&buffer, BLOCK_SIZE, 1, input)==BLOCK_SIZE)
{
char *filecount = argv[1];
FILE *img= fopen(argv[1], "w");
//start of jpeg file
if(buffer[0] == 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3] & 0xf0) ==0xe0)
{
sprintf(filecount, "%03i.jpg", 2);
fwrite(&buffer, BLOCK_SIZE, 1, img);
continue;
//check new block if start of next jpeg
if(buffer[0] == 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3] & 0xf0) ==0xe0)
{
fclose(output);
fclose(img);
}
else
{
fwrite(&buffer, BLOCK_SIZE, 1, img);
}
}
}
}