I have been struggling with the recover.c of pset4. I am not able to figure out the mistakes. It's taking a lot of time and even though gives non recovered JPEG images. Can anybody please verify my code.

* recover.c
* Computer Science 50
* Problem Set 4
* Recovers JPEGs from a forensic image.
#include <stdio.h>
#include <stdlib.h>
FILE* tempjpgg;
FILE* sdcard = fopen("card.raw", "r");
int main()
    if ( sdcard == NULL)
        printf("corrupted sd card");
        return 1;
    char* jpegname = (char*)malloc(8);
    char* tempstorage = (char*)malloc(512);
    float check = 0.0;
    int i = 0;
        fread(tempstorage, 512, 1, sdcard);
        sscanf(tempstorage, "%f", &check);
        if ( check >= 4292411360 && check <= 4292411376)
            sprintf(jpegname, "%03d.jpg", i);
            tempjpgg = fopen(jpegname, "w");
            fprintf(tempjpgg, "%f", check);
            fwrite( tempstorage, 480, 1, tempjpgg);
            tempstorage = NULL;
        else if ( i != 0)
            fprintf(tempjpgg, "%f",check);
            fwrite( tempstorage, 480, 1, tempjpgg);
            tempstorage = NULL;
    }while( check != (float)EOF);
    return 0;

check is basically float equivalent of 0xff 0xd8 0xff 0xe(0-f)


Your program surprises me a little, in the first place you should put your code, not an image, just copy, paste and format, it is easy, it is difficult to know what makes your code just reading, it is good practice to comment the programs to follow easily The logic we are trying to apply. For lack of more competent opinions your program seems to me far from the solution, I recommend to review the videos on the pset. For example, why use sscanf to write to a float-formatted variable (tempstorage)? You just write in the variable tempjpgg 480 bytes ???, Why 480? I think, humbly, that you should rewrite your program again with a new perspective, to facilitate things here you have a pseudocode that can serve you:

initiate image counter to zero
initiate a flag to zero
uint8_t buffer[512]   // We avoided the use of malloc and free
pointer to card
pointer to newjpg

while fread read something (a question about how to do this is a good question)
    IF find a new jpg // buffer[0] == 0xff etc...
       IF the jpg file is open 
           we closed it
        //end IF
       sprint title
       open new jpg
       increase counter
       we make flag 1
   // end IF

    IF flag is 1
       we write in jpg
// end while

fclose files
//end main

You must log in to answer this question.

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