Created
March 8, 2016 08:43
-
-
Save anonymous/7d2ef73580ea2acdd58c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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