Skip to content

Instantly share code, notes, and snippets.

@MohamedTaha98
Created September 3, 2017 12:01
Show Gist options
  • Save MohamedTaha98/cc62a068d933c7ad30fae9558de52f52 to your computer and use it in GitHub Desktop.
Save MohamedTaha98/cc62a068d933c7ad30fae9558de52f52 to your computer and use it in GitHub Desktop.
// recovers jpegs from memory card file
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
#define BLOCK 512
typedef uint8_t BYTE;
int main (int argc, char* argv[]) {
// insure proper usage
if (argc != 2) {
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
// remember forensic image name
char* raw = argv[1];
// open card for reading
FILE* raw_file = fopen(raw, "r");
if (raw_file == NULL) {
fprintf(stderr, "Could not open %s.\n", raw);
return 2;
}
// index of jpg written files
int i = 0;
// array of 512 Bytes
BYTE jpg[BLOCK];
// open the very first file for reading
char file_name[3];
sprintf(file_name, "%03i.jpg", i);
FILE* recovery = fopen(file_name, "w");
// to indicate if we already found a file or not
bool already_found = false;
while (!feof(raw_file)) {
fread(jpg, 512, 1, raw_file);
if ((jpg[0] == 0xff && jpg[1] == 0xd8 && jpg[2] == 0xff
&& (jpg[3] & 0xf0) == 0xe0)) {
if (already_found == true) {
// close current file
fclose(recovery);
// open a new file
i++;
sprintf(file_name, "%03i.jpg", i);
recovery = fopen(file_name, "w");
}
// write the current header into the file
fwrite(jpg, 512, 1, recovery);
already_found = true;
}
else if (already_found == true) {
// write the current header into the file
fwrite(jpg, 512, 1, recovery);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment