automated commit by check50 [check50=True]

This commit is contained in:
kukemuna 2024-03-13 09:30:50 +02:00
commit adc7293b70
1 changed files with 66 additions and 0 deletions

66
recover.c Normal file
View File

@ -0,0 +1,66 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 512
#define NAMESIZE 8
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover FILE\n");
return 1;
}
FILE *card = fopen(argv[1], "r");
if (card == NULL)
{
printf("Could not open file.\n");
return 1;
}
uint8_t buffer[BLOCKSIZE];
int count = 0, first = 1;
char *filename = malloc(NAMESIZE);
FILE *jpg = NULL;
while (fread(buffer, 1, BLOCKSIZE, card) == BLOCKSIZE)
{
// JPG header found
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
if (first == 1)
{
sprintf(filename, "%03i.jpg", count);
jpg = fopen(filename, "w");
if (jpg == NULL)
{
printf("Could not open file.\n");
return 1;
}
first = 0;
}
else
{
fclose(jpg);
sprintf(filename, "%03i.jpg", count);
jpg = fopen(filename, "w");
if (jpg == NULL)
{
printf("Could not open file.\n");
return 1;
}
}
count++;
}
if (first == 0) // Ignore data before first JPG header
{
fwrite(buffer, 1, BLOCKSIZE, jpg);
}
}
fclose(jpg);
fclose(card);
free(filename);
}