Skip to content

Instantly share code, notes, and snippets.

@ehahehah
Last active August 29, 2015 14:06
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 ehahehah/4b4038f7da99ab484b86 to your computer and use it in GitHub Desktop.
Save ehahehah/4b4038f7da99ab484b86 to your computer and use it in GitHub Desktop.
pset5 recover.c Version 1
/**
* recover.c
*
* Computer Science 50
* Problem Set 5
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "bmp.h"
int main(void)
{
// opens card.raw
FILE* inptr = fopen("card.raw", "r");
if ("inptr" == NULL)
{
printf("Could not open card.raw");
return 1;
}
int counter = 1;
char title[7];
sprintf(title, "%.3i.jpg", counter);
FILE* img = fopen(title, "a");
while (true)
{
BYTE JPEG[512];
fread(&JPEG, 512 * sizeof(BYTE), 1, inptr);
if (JPEG[0] == 0xff && JPEG[1] == 0xd8 && JPEG[2] == 0xff)
{
if (JPEG[3] == 0xe0 || JPEG[3] == 0xe1)
{
if (counter == 1)
{
fwrite(&JPEG, 512 * sizeof(BYTE), 1, img);
while (true)
{
BYTE JPEG[512];
fread(&JPEG, 512 * sizeof(BYTE), 1, inptr);
if (JPEG[0] == 0xff && JPEG[1] == 0xd8 && JPEG[2] == 0xff)
{
if (JPEG[3] == 0xe0 || JPEG[3] == 0xe1)
{
fclose(img);
counter++;
sprintf(title, "%.3i.jpg", counter);
FILE* img = fopen(title, "a");
fwrite(&JPEG, 512 * sizeof(BYTE), 1, img);
break;
}
}
else
{
fwrite(&JPEG, 512 * sizeof(BYTE), 1, img);
}
}
}
else
{
fclose(img);
counter++;
sprintf(title, "%.3i.jpg", counter);
FILE* img = fopen(title, "a");
fwrite(&JPEG, 512 * sizeof(BYTE), 1, img);
}
}
}
if (counter != 1)
{
fwrite(&JPEG, 512 * sizeof(BYTE), 1, img);
}
if (feof(inptr))
{
fclose(img);
break;
}
}
// closes card.raw
fclose(inptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment