Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
// check if input is valid. else, return 1
{
// Check for invalid input
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
// open input file (the memory card)
char* input_file = argv[1];
FILE* input_pointer = fopen(input_file, "r");
if (input_file == NULL)
{
printf("Unable to open: %s\n", input_file);
return 1;
}
// repeat until end of card
int block_size = 0;
unsigned char buffer[block_size];
// File counter, initialise
int image_counter = 0;
FILE *output_pointer = NULL;
// Check if JPEG is found
int jpg_has_alr_been_found = 0;
// read 512 bytes into a buffer
while (fread(&buffer, block_size, 1, input_pointer) == 1)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (jpg_has_alr_been_found == 1)
{
fclose(output_pointer);
}
else
{
jpg_has_alr_been_found = 1;
}
char filename[8];
sprintf(filename, "%03i.jpg", image_counter);
output_pointer = fopen(filename, "w");
image_counter++;
}
if(jpg_has_alr_been_found == 1)
{
fwrite(&buffer, block_size, 1, output_pointer);
}
}
// if start of new jpeg
if(output_pointer == NULL)
{
fclose(output_pointer);
}
if (input_pointer == NULL)
{
fclose(input_pointer);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment