Skip to content

Instantly share code, notes, and snippets.

@aryamanarora
Created November 2, 2016 00:19
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 aryamanarora/07221c170d3833e2445d5e92ba7ca87f to your computer and use it in GitHub Desktop.
Save aryamanarora/07221c170d3833e2445d5e92ba7ca87f to your computer and use it in GitHub Desktop.
temporary
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
// define a BYTE for convenience
typedef uint8_t BYTE;
int main(int argc, char* argv[])
{
// open the RAW file
FILE* file = fopen("card.raw", "r");
if (file == NULL)
{
printf("Could not open the file...\n");
return 1;
}
// I learned this from CS50 StackExchange; initialize 512 byte buffer
BYTE buffer[512];
// initialize imagenumber
int filenum = 0;
// here we go!
// while the next buffer exists, keep on going
while (fread(&buffer, 512, 1, file) == 1)
{
// if the current buffer has the first 4 bytes of a header
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] >= 0xe0 && buffer[3] <= 0xef)
{
// filename
char* filename = malloc(8 * sizeof(BYTE));
// set filename to nnn.jpg
sprintf(filename, "%0.3d.jpg", filenum);
// open the file
FILE* currentfile = fopen(filename, "w");
// error check
if (currentfile == NULL)
{
printf("Could not create the file...\n");
return 2;
}
// while it is not a header
while (buffer[0] != 0xff || buffer[1] != 0xd8 || buffer[2] != 0xff || buffer[3] < 0xe0 || buffer[3] > 0xef)
{
// read into file
fread(&buffer, 512, 1, file);
// write into the file
fwrite(&buffer, 512, 1, currentfile);
}
// close the file
fclose(currentfile);
filenum++;
}
}
// close file
fclose(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment