0
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover Card.raw\n");
        return 1;
    }

    char *card = argv[1];

    int jpgCount = 0;

    typedef uint8_t BYTE;

    BYTE buffer[512];

    FILE *file = fopen(card, "r");

    if (file == NULL)
    {
        fprintf(stderr, "fopen returned NULL.\n");
        return 2;
    }
    else
    {
        while (fread(&buffer, 1, 512, file) == 512)
        {
            if (buffer[0] == 0xff &&
                buffer[1] == 0xd8 &&
                buffer[2] == 0xff &&
                (buffer[3] & 0xf0) == 0xe0)
            {
                char Imgfile[8];

                sprintf(Imgfile, "%03d.jpg", jpgCount);

                FILE *img = fopen(Imgfile, "w");

                if (img == NULL)
                {
                    fprintf(stderr, "Couldn't create the file.\n");
                    return 1;
                }

                if (jpgCount == 0)
                {
                    fwrite(&buffer, 1, 512, img);

                    jpgCount++;
                }
                else
                {
                    fclose(img);

                    fwrite(&buffer, 1, 512, img);

                    jpgCount++;
                }
            }
        }
        fclose(file);
    }
    return 0;
}

Recover finds all 50 pictures and names them correctly but it doesn't open any of them and spits out "invalid or unsupported image format.", can someone please point out what i'm doing wrong here? Thanks in advance.

1 Answer 1

1

I'll wager that the first file is 512 bytes long and all the rest are empty. For every file after the first, it looks like the new file is being opened before the old one is closed, the output file is then being closed and finally, an attempt is being made to write to the output file. I'm surprised that it doesn't result in an error.

Only the first block in a file has a signature. What is being done with all the blocks that follow?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .