0

#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\n"); return 1; }

FILE *card = fopen(argv[1], "r");
if (card == NULL)
{
    return 1;
}
char *filename = malloc(8 * sizeof(char));
if (filename == NULL)
{
    return 1;
}
int count = 0;
short check = 0;
FILE *image = NULL;

while (check == 0)
{
    BYTE *buffer = malloc(512);
    for (int i = 0; i < 512; i++)
    {
        buffer[i] = 0;
    }
    fread(buffer, 512, 1, card);
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0))
    {
        if (count == 0)
        {
            sprintf(filename, "%03i.jpg", count);
            image = fopen(filename, "w");
            if (image == NULL)
            {
                return 1;
            }
            fwrite(buffer, 512, 1, image);
            count++;
        }
        else
        {
            fclose(image);
            sprintf(filename, "%03i.jpg", count);
            image = fopen(filename, "w");
            if (image == NULL)
            {
                return 1;
            }
            fwrite(buffer, 512, 1, image);
            count++;
        }
    }
    else
    {
        fwrite(buffer, 512, 1, image);
    }
    
    for (int i = 0; i < 512; i++)
    {
        if (buffer[i] == 0)
        {
            check = 1;
            fclose(image);
            break;
        }
    }
}

free(filename);

}

You must log in to answer this question.

Browse other questions tagged .