0

I've spent hours and hours on this code but I can't get any picture out of raw file. I tracked the problem with gdb and figured that when the it reaches to place to test for signature of jpeg it does not match with the condition. So I printed the temp[0] value many times in almost 5 iterations but non was ff. I don't get where the problem is, but something should be wrong with the placeholder of the infile. Can anyone help me please? It's getting really frustrating.

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

// prototypes
int fsize(FILE *fp);

int main(int argc, char* argv[])
{
    if (argc != 1)
    {
        printf("Usage: recover currupted-file");
        return 1;
    }

    // opening the corrupted file
    FILE* infile = fopen("card.raw", "r");
    if (infile == NULL)
    {
        return 1;
    }

    // declaring and initializing output filename
    int filenumber = 0;
    char* filename = malloc(8 * sizeof(char));
    FILE* outfile = NULL;

    // allocating memory to temp read variable
    uint8_t temp[512];

    //declaring a variable for file size
    long filesize = fsize(infile);

    for (int i = 0; i < filesize; i += 512)
    {
        // reading every 512 Bytes to a temp
        fread(&temp, sizeof(temp), 1, infile);

        if (temp[0] == 0xff && temp[1] == 0xd8 && temp[2] == 0xff && (temp[3] == 0xe0 || temp[3]  == 0xe1))
        {
            if (filenumber > 0)
            {
                fclose(outfile);
            }
            sprintf(filename, "%3i.jpg", filenumber);
            FILE* outfile = fopen(filename, "w");
            if (outfile == NULL)
            {
                return 1;
            }
            filenumber++;
            fwrite(temp, 512, 1, outfile);
        }
        else if (filenumber > 0)
        {
            fwrite(temp, 512, 1, outfile);
        }
    }
    free(filename);
    fclose(outfile);
    fclose(infile);
}

// file size finder code
int fsize(FILE *fp)
{
    //find current place holder to return to later
    int last=ftell(fp);
    fseek(fp, 0, SEEK_END);
    int size=ftell(fp);
    //go back to where we were
    fseek(fp,last,SEEK_SET);
    return size;
}
3
  • sorry I dont know the what the problem is with the libraries that does not show up but they are: stdio, stdlib and stdint
    – Farhad
    Commented Nov 3, 2015 at 8:45
  • 1
    Farhad, please take some time to read and explore the help tab at the top of this page. It will explain how to properly format and display code on this forum. You can then come back and edit your question. It will be a good exercise for you and will help everyone in the future. Nothing to apologize for, but a great time to learn for the future. ;-)
    – Cliff B
    Commented Nov 3, 2015 at 16:59
  • Thank you for the advice, I understand know how to send a block of code.
    – Farhad
    Commented Nov 4, 2015 at 3:44

1 Answer 1

1

You aren't seeing a match in the first several iterations because you have to run the loop many thousands of times before the first signature is detected.

However, the problem is elsewhere. You are hitting a seg fault because once you do detect the signature and create the file, your file pointer is disappearing before you can write the next block of data to the file.

The real problem is with these two lines:

FILE* outfile = NULL;
...
FILE* outfile = fopen(filename, "w");

You have declared the file pointer outfile twice. Since the second one is contained within a pair of curly braces, the second one supercedes the first one. It has scope within those curly braces. So, the second one points at the file when it is created, but the pointer disappears when you exit that if statement code block. Now, the original pointer, which points to NULL, becomes active again. When you try to write the second block of data, it seg faults.

The fix is simple. Remove 'FILE*' from the second declaration so that you are using the original one.

You have other issues, but that wasn't your question. Have fun! ;-)

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

1
  • Thank you so much, it works know. 16 pictures but with wrong naming, actually the problem of naming is with %3i that must be %.3i . now its finished thanks again you rock.
    – Farhad
    Commented Nov 4, 2015 at 3:48

You must log in to answer this question.

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