0

I've been working at recovery for a while - I believe I'm nearly done with it - but right now my code is giving me serious problems. Currently, running ./recover card.raw produces 49 of the 50 images (based on check50, I think it skips the first jpeg it finds for some reason) as well as a corrupt null file, which disappears whenever I try to delete or open it.

(SIDENOTE: The null file seems to reappear randomly whenever it is removed - possibly having to do with the compiled "recover" file.)

I've messed with recover.c to try and resolve this, but so far I've ended up nowhere and somehow produced 5 more corrupt files, all behaving like the first one.

(EDIT: I was able to remove the existing corrupt files by moving recover.c and card.raw to a new directory, and deleting the old directory. I am also able to confirm that this version of my code still produces a brand new corrupt file whenever it is compiled and ran.)

Here's the code I've been using

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

int main(int argc, char *argv[])
{
    // Check if 1 file name was provided
    if (argc != 2)
    {
        printf("Usage: ./recover FILENAME\n");
        return 1;
    }
    // Attempt to read the file
    FILE *file = fopen(argv[1], "r");

    // Check if the file is valid
    if (file == NULL)
    {
        printf("File not found\n");
        return 1;
    }
    // Location to store block
    unsigned char block[512];
    // Recovered jpg count
    int numfound = 0;
    // File for recovered jpgs
    FILE *jpg;
    // New filename placeholder
    char filename [8];
    while (fread(block, 512, 1, file))
    {
        if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
    {
        if (numfound == 0)
        {
            jpg = fopen(filename, "w");
            sprintf(filename, "%03i.jpg", numfound);
            fwrite(&block, 512, 1, jpg);
            numfound++;
        }
        else
        {
            fclose(jpg);
            jpg = fopen(filename, "w");
            sprintf(filename, "%03i.jpg", numfound);
            numfound++;
            fwrite(&block, 512, 1, jpg);
        }
    }
    else if (numfound > 0)
    {
        fwrite(&block, 512, 1, jpg);
    }
}
// Close all, successful execution
fclose(file);
if (jpg != NULL)
{
    fclose(jpg);
}
return 0;
}
1

"Ready, fire, aim!"

The problem lies here:

        jpg = fopen(filename, "w");
        sprintf(filename, "%03i.jpg", numfound);

The sprintf command builds the name of the file to be opened by the fopen call. It needs to be before the fopen call.

It also explains the files with wierd file names. To delete them, you'll need to get the names from an ls -al file listing and delete them explicitly from the command line. Since filename hasn't been initialized when the first file is created, the fopen creates a file name using the garbage data in memory to name the file.

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

| improve this answer | |

You must log in to answer this question.

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