0

0 down vote favorite 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;

}

1 Answer 1

1

Look at the following code:

unsigned char buf[512];
x = fread(buf, 512, 1, inptr);
//read into buffer
fread(buf, 512, 1, inptr);

This is doing two consecutive freads into buf, but the data from the first read is being overwritten before it is processed. In other words, the code is throwing away half the data.

There may be other issues, but this is a very big problem. You should have a chance to resolve it and have a chance to analyze and resolve any other problems, if any, on your own.

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

8
  • Actually, I thought about this problem and couldn't know if I used the fread to just return a value will that actually read them or will just return, so thanks for the clarification. But I've removed one of the freads and ran check50 on the code and still got the same error. Sep 24, 2017 at 21:33
  • Did you actually run make again? OK, let's assume you did. Now, do an ls -al *.jpg and check file size. What's happening to the data between signature blocks?
    – Cliff B
    Sep 24, 2017 at 21:37
  • yes, I ran make and still doesn't match. I made that line and I got this -rw------- 1 ubuntu ubuntu 512 Sep 26 08:06 049.jpg for all of the photos. The data between the signature blocks supposedly from what I see from the code is in its right place. Sep 26, 2017 at 8:08
  • Then you have misinterpreted what is supposed to happen. None of the files are just 512 bytes. Think about it for a minute. How could a 4 megapixel image, for example, be stored in 512 bytes? If you go back and carefully reread the pset instructions, the data before the first signature block is garbage data. The data between and after signature blocks is image data. The code above is discarding the vast majority of image data.
    – Cliff B
    Sep 26, 2017 at 8:17
  • I really had that on mind while writing the code and this condition is supposedly do that if(jpg != NULL && foundjpg > 0){ fwrite(buf, 512, 1, jpg); } The question here is why it runs on just the first 512 bytes and do not run on the middle? Sep 26, 2017 at 8:43

You must log in to answer this question.

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