0

I don't know why but when i try working with check50 it tells everything is ok except that the015 jpg i not recovered correctly but for some reason when i actually try it and run it it says it works just fine.enter image description here

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

typedef uint8_t BYTE;

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

char nameFile[9];
// creates int to keep count of numbers of files opened
int numFiles = 0;
// Creates array to store info in chunks
BYTE fat[512];

//Creates both files to be used
FILE* card = fopen("card.raw", "r");
FILE* image;

if (card == NULL)
{
    printf("Could not open card.raw");
    return 1;
}

while(feof(card) == 0 )
{
    for( int i  = 0; i < 512; i++)
        fread(&fat[i], sizeof (BYTE), 1, card);

    // cheks wheter is the begining of a file
    if((fat[0] == 0xff) && (fat[1] == 0xd8) && (fat[2] == 0xff) && (fat[3] == 0xe0 || fat[3] == 0xe1))
    {

        // if there is something opened
        if(numFiles > 0)
        {
            fclose(image);
            sprintf(nameFile , "%03d.jpg",numFiles );
            image = fopen(nameFile,"w"); 
            numFiles++;
        }
        // if nothing has been opened 
        if( numFiles < 1)
        {
            sprintf(nameFile, "%03d.jpg",numFiles );
            image = fopen(nameFile,"w"); 
            numFiles++;
        }
    }
    // if somethinf has been opened everything that goes after it must be written in a file
    if(numFiles > 0)
        fwrite(fat, 1, 512, image);     
}
// closes remaining windows
fclose(image);
fclose(card);

}

2 Answers 2

1

Your algorithm does not end correctly. The video by Zamyla talks briefly about not being able to just loop until feof. This is that scenario. You will need to use fread in a conditional, where it evalutes if there is still another chunk to read, as she described.

3
  • I thought so, but i don't understand what should i fread() for? Apr 4, 2015 at 18:44
  • How can i know where the 015.jpg ends if i don't have another jpg Apr 4, 2015 at 18:46
  • You will want to fread a block into the buffer so long as a block of proper size exists. You can put this conditional inside "a forever loop" such that when there is less than 512 bytes remaining you break out of the forever loop and can exit the program gracefully. It works because if the conditional evaluates true, you read the block, but when false, you break out of the forever loop. Apr 4, 2015 at 22:34
0

If there's someone feeling stuck with this problem and if you are absolutely confident you have no memory leaks and/or data loss whatsoever, here's another solution you might try out - make sure your code detects ALL 16 variations of JPEG headers: 0xff 0xd8 0xff (0xe0 | 0xe1 | 0xe2 | 0xe3 | 0xe4 | 0xe5 | 0xe6 | 0xe7 | 0xe8 | 0xe9 | 0xea | 0xeb | 0xec | 0xed | 0xee | 0xef). If you just match 0xff 0xd8 0xff 0xe0, you will fare fine and recover all of the .jpeg files in the PSET BUT you won't pass the check50 check. Also think of the implications of NOT detecting ALL of the headers - your program will only support one type of JPEG (JFIF), whilst gracefully ignoring the other 15 types.

This is how you could detect all of the JPEG types with a mask:

if (((*(unsigned int*)dataBuffer) & 0xe0ffd8ffU) == 0xe0ffd8ffU) {
    /* your code here */
}

Here we assume that dataBuffer is an array of unsigned chars with dataBuffer being of type unsigned char*.

Hope that helps.

2
  • NOTE: This answer applies to the 2016 problem where the signature is as noted above and there are 50 output files. In 2015, there were only two possible signatures and 16 output files. Your solution needs to match whichever version you are working on.
    – Cliff B
    Feb 4, 2016 at 0:28
  • Thanks, Cliff. Did not realize that. Feb 7, 2016 at 0:24

You must log in to answer this question.

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