0

the code recovers all 50 images. but the check50 2015.fall.pset4.recover recover.c returns errors for middle files and for the last file. :( the code is given below:-

/** * recover.c * * Computer Science 50 * Problem Set 4 * * Recovers JPEGs from a forensic image. */

include

include

int main() { // opening card file. FILE* card = fopen("card.raw","r"); if (card == NULL){ printf("cannot read from file. \n"); return 1; } // initializing a double for sprintf. int filenum = 0; int n = 0;

while(!feof(card)){

    // creating buffer.
    int* buf= malloc(512);

    // reading the info in 512 bytes
    fread(buf, 512, 1, card);

    //checking for characteristic bytes in 512 block
    if(buf[0]==0xe0ffd8ff){
        char* title= malloc(10*sizeof(char));
        sprintf(title, "%03i.jpg", filenum);
        FILE* jpg = fopen(title, "w");
        fwrite(buf, 512, 1, jpg);
        fclose(jpg);
        filenum += 1;
        n += 1;
        free(title);
    }else if (n>0){
        char* title= malloc(10*sizeof(char));
        sprintf(title, "%03i.jpg", filenum-1);
        FILE* jpg = fopen(title, "a");
        fwrite(buf, 512, 1, jpg);
        fclose(jpg);
        free(title);
    }
    free(buf);
}

fclose(card);
return 0;

}

2

I see one definite problem and some likely issues. First, your last file has one extra 512 byte block. This is a common problem, caused by reading from the input file, processing the data, and then checking for EOF. The EOF condition is not set until the EOF has actually been encountered. After the last word is read, the file pointer is set to the end of the file, but it has not yet attempted to read the EOF. It's like walking to the end of a diving board. You're at the end, but you don't get wet until you take one more step. ;-) So, the code executes one more read and encounters the EOF but doesn't read any data into the buffer. The previous read's data remains in the buffer and gets written out to the last file.

After that, it looks like you're only checking one of 16 possible signatures. Given that check50 actually uses different test data, it's probably catching this.

I'm concerned that you are using type int for your buffer instead of an unsigned single byte data type. It may work, but could potentially cause other issues. Or, it may actually work, I'll reserve judgement.

This should get you started in the right direction. 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 .