Skip to content

Instantly share code, notes, and snippets.

/recover.c Secret

Created November 22, 2016 20:33
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 anonymous/6e9bfa3282645ac1bac2953227d935e9 to your computer and use it in GitHub Desktop.
Save anonymous/6e9bfa3282645ac1bac2953227d935e9 to your computer and use it in GitHub Desktop.
recover.c - shared from CS50 IDE
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <stdint.h>
// prevent magic numbers
#define BLOCKSIZE 512
typedef uint8_t BYTE;
int main(void)
{
// open memory card
FILE* fp = fopen("card.raw", "r");
// check file permission
if (fp == NULL)
{
fclose(fp);
printf("Unable to retrieve file from card.raw\n");
return 1;
}
// create a char to name new image files
char iname[8];
// set a counter
int counter = 0;
// set a buffer of 512 bytes
BYTE buff[BLOCKSIZE];
// open output file
FILE* outfile = NULL;
// repeat until the end
while (!feof(fp))
{
// find the start of the jpg
if (buff[0] == 0xff && buff[1] == 0xd8 && buff[2] == 0xff && (buff[3] == 0xe0 || && buff[3] == 0xe1))
{
// if an outfile is open, close it
if (outfile != NULL)
{
fclose(outfile);
}
// label files with three digits
sprintf(iname, "%03d.jpg", counter);
// open new file with name given by sprinft
outfile = fopen(iname, "w");
counter++;
if (NULL == outfile)
{
printf("Couldn't open %s.\n", iname);
return 1;
}
fwrite(buff, sizeof(buff), 1, outfile);
}
else if (counter > 0)
{
fwrite(buff, sizeof(buff), 1, outfile);
}
fwrite(buff, sizeof(buff), 1, fp);
}
// close file
fclose(fp);
// that's all folks
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment