Skip to content

Instantly share code, notes, and snippets.

@wx5162839
Created May 14, 2020 11:17
Show Gist options
  • Select an option

  • Save wx5162839/3eeda9238649a7cee51100bbfaa161b9 to your computer and use it in GitHub Desktop.

Select an option

Save wx5162839/3eeda9238649a7cee51100bbfaa161b9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image");
return 1;
}
// Original file
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Usage: ./recover image");
return 1;
}
// file will be written into
FILE *img = NULL;
char filename[8];
int counter = 0;
int i = 0;
BYTE buffer[512];
while(fread(buffer, 512, 1, file) == 1)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
i = 1;
if (counter == 0)
{
sprintf(filename, "%03i.jpg", counter);
img = fopen(filename, "w");
fwrite(buffer, 512, 1, img);
counter++;
}
else
{
fclose(img);
sprintf(filename, "%03i.jpg", counter);
img = fopen(filename, "w");
fwrite(buffer, 512, 1, img);
counter++;
}
}
else
{
if (i == 0)
{
continue;
}
else
{
fwrite(buffer, 512, 1, img);
}
}
}
fclose(img);
fclose(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment