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/e6d579dcb08649a4d65b to your computer and use it in GitHub Desktop.
Save ehahehah/e6d579dcb08649a4d65b to your computer and use it in GitHub Desktop.
pset5 recover.c Version 3
/**
* 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[8];
sprintf(title, "%.3i.jpg", counter);
FILE* img = fopen(title, "a");
int other_counter = 2;
char other_title[8];
sprintf(other_title, "%.3i.jpg", other_counter);
FILE* new_img = fopen(other_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)
{
fread(&JPEG, 512 * sizeof(BYTE), 1, inptr);
if (JPEG[0] == 0xff && JPEG[1] == 0xd8 && JPEG[2] == 0xff)
{
if (JPEG[3] == 0xe0 || JPEG[3] == 0xe1)
{
counter++;
fclose(img);
fwrite(&JPEG, 512 * sizeof(BYTE), 1, new_img);
fread(&JPEG, 512 * sizeof(BYTE), 1, inptr);
break;
}
}
else
{
fwrite(&JPEG, 512 * sizeof(BYTE), 1, img);
}
}
}
else
{
fclose(new_img);
other_counter++;
sprintf(other_title, "%.3i.jpg", other_counter);
FILE* new_img = fopen(other_title, "a");
fwrite(&JPEG, 512 * sizeof(BYTE), 1, new_img);
fread(&JPEG, 512 * sizeof(BYTE), 1, inptr);
}
}
}
if (counter != 1)
{
fwrite(&JPEG, 512 * sizeof(BYTE), 1, new_img);
}
if (feof(inptr))
{
fclose(new_img);
break;
}
}
// closes card.raw
fclose(inptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment