1

I'm having a lot of trouble with recover. I have written some code, just to get the basic structure. I was trying to iterate over the raw file until the end, printing when stumbled upon a jpeg.

Anyway, when I run my code. It stops after 4 blocks of 512B, having encountered 1 jpeg. How do I make my code run over the whole raw file?.

Here's my code:

//typedef uint8_t  BYTE;

int main(int argc, char* argv[])
{
    //open card image
    FILE* file = fopen("card.raw", "r");

    //initialize counters
    int counter = 0;
    int iterations = 0;

    //open buffer
    BYTE buffer[512];

    do
    {
        //store 512B in buffer
        fread(&buffer, sizeof(BYTE), 512, file);

        //check for start of jpeg
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe0)
        {
            printf("JPEG found\n");
            counter++;
        }
        else if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe1)
        {
            printf("JPEG found\n");
            counter++;
        }

        iterations++;
    }
    while(buffer[0] != 0x00 && buffer[1] != 0x00 && buffer[2] != 0x00 && buffer[3] != 0x00);

    printf("Counter is %d\n", counter);
    printf("Iterations is %d\n", iterations);
    fclose(file);
    return 0;
}

1 Answer 1

2

You need an outer loop that runs until the end of file (EOF) is detected, e.g.

While(!feof(file))

// do this thing

Within that loop you should have your buffer declared. You can leave those if conditions within that loop, but you don't need a printf statement. Instead, you need to start writing the 512 byte pieces from the detection of the jpg signature to the detection of the next such signature (remember they are stored next to each other in memory!) into an outfile called 000.jpg, 001.jpg, and so on.

Be careful with the feof() function though. Read the manual in your terminal to find out when exactly it stops the loop from being executed, many here, including myself, have not been able to recover the last (50th) picture because of using feof() inappropriately.

2
  • while(!feof(file)) is equivalent to while(feof(file) != true)
    – pat_krat
    Commented Sep 8, 2014 at 10:19
  • Correction: do {...} while (!feof(file)), as otherwise you will segfault unless you have already performed an fread(). You are also guaranteed that your input file, "card.raw", is more than 0 bytes.
    – ChiCubed
    Commented Dec 31, 2014 at 5:23

You must log in to answer this question.

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