56 lines
1.3 KiB
C
56 lines
1.3 KiB
C
#include <cs50.h>
|
|
#include <math.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
// Accept a single command-line argument
|
|
if (argc != 2)
|
|
{
|
|
printf("Usage: ./recover FILE\n");
|
|
return 1;
|
|
}
|
|
|
|
// Open the memory card
|
|
FILE *inptr = fopen(argv[1], "r");
|
|
if (inptr == NULL)
|
|
{
|
|
printf("Unable to open file\n");
|
|
return 1;
|
|
}
|
|
|
|
// Create a buffer for a block of data
|
|
uint8_t buffer[512];
|
|
int block_counter = 0;
|
|
FILE *outptr = NULL;
|
|
char filename[8];
|
|
|
|
// While there's still data left to read from the memory card
|
|
int i = 0;
|
|
while (fread(buffer, 1, 512, inptr) == 512)
|
|
{
|
|
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
|
|
(buffer[3] & 0xf0) == 0xe0)
|
|
{
|
|
if (outptr != 0)
|
|
{
|
|
fclose(outptr);
|
|
}
|
|
sprintf(filename, "%03i.jpg", block_counter);
|
|
outptr = fopen(filename, "w");
|
|
block_counter++;
|
|
}
|
|
if (outptr != 0)
|
|
{
|
|
// if already found, continue writing
|
|
fwrite(buffer, 512, 1, outptr);
|
|
}
|
|
}
|
|
fclose(inptr);
|
|
fclose(outptr);
|
|
return 0;
|
|
}
|