Skip to content

Instantly share code, notes, and snippets.

Created March 8, 2016 08:43
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/7d2ef73580ea2acdd58c to your computer and use it in GitHub Desktop.
Save anonymous/7d2ef73580ea2acdd58c to your computer and use it in GitHub Desktop.
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <cs50.h>
#include <stdint.h>
#define BLOCKSIZE 512
#define BYTE 8
int main(void)
{
FILE* memory = fopen("card.raw", "r");
if (memory == NULL)
{
printf("Couldn't open memory card\n");
return 1;
}
uint8_t buffer[BLOCKSIZE];
int counter = 0;
FILE* images = NULL;
while(fread(buffer, BLOCKSIZE, 1, memory) == 1)
{
if(((buffer[0] = 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff)) &&
((buffer[3] == 0xe0) || (buffer[3] == 0xe1)))
{
if (images != NULL)
{
fclose(images);
}
char recovered[BYTE];
sprintf(recovered, "%03d.jpg", counter);
images = fopen(recovered, "w");
counter++;
}
if (images != NULL)
{
fwrite(buffer, BLOCKSIZE, 1, images);
}
}
if (images != NULL)
{
fclose(images);
}
fclose(memory);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment