AbdElRahman Salim
AbdElRahman Salim

Reputation: 1

My recovered IMGs doesn't match the Original in recover CS50

The problem is to recover some JPGs from a .raw file.

when I run check50 I get "recovered img don't match".

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly – recovered image does not match
:( recovers middle images correctly – recovered image does not match
:( recovers 015.jpg correctly – 015.jpg not found

I really tried hard to identify the problem and every time I fail to Identify where the problem is, I hope someone can and give me a peace of advice.

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

int main(int argc, char *argv[]){

if(argc != 2){

     fprintf(stderr, "Usage: ./recover image");
    return 1;
}

//open file

FILE *inptr = fopen(argv[1], "r");
if (inptr == NULL){

    fprintf(stderr, "Could not open %s.\n", argv[1]);
    return 2;
}


int foundjpg = 0;
char filename[10];
int x=1;

//repeat until end of the card
while(x == 1){

    //buffer
    unsigned char buf[512];
    x = fread(buf, 512, 1, inptr);
    //read into buffer
    fread(buf, 512, 1, inptr);
    FILE *jpg = fopen(filename, "w");

    //start of a new jpg?
    if(buf[0]== 0xff  && buf[1] == 0xd8  && buf[2] == 0xff && (buf[3] & 0xf0) == 0xe0 ){


        if(jpg != NULL){// yes i found before

            fclose(jpg);
            sprintf(filename, "%03i.jpg" ,foundjpg );
            foundjpg++;
            jpg = fopen(filename, "w");


        }
        else{
            sprintf(filename, "%03i.jpg" ,foundjpg );
            jpg = fopen(filename , "w");
            foundjpg++;

        }
    }
    //already found a jpg?
    if(jpg != NULL && foundjpg > 0){

        fwrite(buf, 1, 512, jpg);

    }

}


fclose(inptr);

// success
return 0;

}

Upvotes: 0

Views: 674

Answers (1)

M Oehm
M Oehm

Reputation: 29126

The order in which you do things is quite confused and leads to errors. For example:

  • filename isn't initialised when you use it for the first time.
  • You increase the counter foundjpg after you use it to create the filename, which in your program means that the second image is called 01.jpg. All image indices are off by one and the last one is missing.
  • When the id bytes do not identify a valid jpg, no new record is read and your loop never ends.

You should re-organise your code so that it does one thing after another in a natural way. The program might look like this:

  • Check command line arguments
  • Open the raw file
  • Main loop:
    • Read fixed-size block. If it can't be read, exit the loop
    • Check if first bytes identify a jpg and if so:
      • Create file name
      • Open jpg file for writing
      • Write block and close jpg file
      • Increment block counter
  • Close raw file

You must decide how you handle errors. Do you just skip erroneous blocks or do you abort the program?

It is also not clear whether all images are 512 bytes long, which seems improbable. Perhaps you must read the actual image size from the header and then copy the whole image.

Upvotes: 1

Related Questions