Skip to content

Instantly share code, notes, and snippets.

@sameekapdi
Created October 2, 2017 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sameekapdi/736f4cbbe16d8913ef3c254fce142150 to your computer and use it in GitHub Desktop.
Save sameekapdi/736f4cbbe16d8913ef3c254fce142150 to your computer and use it in GitHub Desktop.
/**
* recover.c
*/
#include <stdio.h>
#define BLOCK_SIZE 512
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Please enter the correct number of arguments\n");
return 1;
}
// open input file
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 2;
}
//create block
unsigned char block[BLOCK_SIZE];
//create outfile file
int jpgcount = 0;
FILE* pic = NULL;
int jpg_found = 0;
//read through the file til the end
while(fread(block,BLOCK_SIZE,1, file)==1)
{
//Check for jpg signiture
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff &&
(block[3] == 0xe0 || block[3] == 0xe1 || block[3] == 0xe2 || block[3] == 0xe3
|| block[3] == 0xe4 || block[3] == 0xe5 || block[3] == 0xe6 || block[3] == 0xe7
|| block[3] == 0xe8 || block[3] == 0xe9 || block[3] == 0xea || block[3] == 0xeb
|| block[3] == 0xec || block[3] == 0xed || block[3] == 0xee || block[3] == 0xef))
{
//check to see if a picture has already been found, if so close it
if(jpg_found ==1)
{
fclose(pic);
}
else
{
jpg_found = 1;
}
//construct filename
char filename[8];
sprintf(filename,"%03d.jpg",jpgcount);
pic = fopen(filename, "a");
jpgcount++;
}
//write the file
if(jpg_found == 1)
{
fwrite(&block, BLOCK_SIZE, 1, pic);
}
}
//close files
fclose(file);
fclose(pic);
//success
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment