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

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

//function doesn't need to get arg
int main(void)
{
    // open memory card file
    FILE* mCard = fopen("card.raw", "r");

    //check for error
    if(mCard == NULL)
    {
        free(mCard);
        fprintf(stderr, "Oops...can't open this memory card file.\n");
        return 1;
    }

    //define FAT block as an array of 512 bytes and 4 bytes for test     
    //signature
    uint8_t block[512];

    //read 512 bytes (FAT block) each time
    fread(block, 512, 1, mCard);    

    //define outfile for jpg recover 
    FILE* jpgRec;

    //number of jpgs we found already
    int jpgFound = 0; 

    //keep track for open file that need to be closed
    int fileOpen = 0;    

    //Go over 'FAT blocks' - till eof
    while(fread(block, 512, 1, mCard) > 0) 
    {
        if((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff
        && block[3] == 0xe0) || (block[0] == 0xff && block[1] == 0xd8
        && block[2] == 0xff && block[3] == 0xe1))
        {

            //define file name
            char fileTitle[8]; 
            sprintf(fileTitle, "%03d.jpg",jpgFound); 

            //check for open file to close
            //open new file to 'w'
            if(fileOpen == 0)
            {
                 jpgRec = fopen(fileTitle, "w");
                 fwrite(block, 512, 1, jpgRec);

                 //from now on there will be an open file to close first
                 fileOpen = 1;   
            }

            if(fileOpen == 1) 
            {
                //close open file first
                fclose(jpgRec);

                //now can open new one
                jpgRec = fopen(fileTitle, "w");
                fwrite(block, 512, 1, jpgRec);                    
            }
            jpgFound++;

        }
        //incase first 4 bytes doesn't match jpgSig1/2
        else
        {
            if(fileOpen == 1)
            {
                fwrite(block, 512, 1, jpgRec);
            }

        }

    }
    //close every file open
    if(jpgRec)
    {
        fclose(jpgRec);
    }
    fclose(mCard);    
    return 0;
}
0

Not quite sure, but probably you're writing an additional block to the last jpeg file, it will still be readable, but will not match with the expected last file should be, i would try reviewing the while statement.

1

You must log in to answer this question.

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