0

First off, I did find that many people had this same issue, but none of the solutions given seemed to work for me.

I am able to recover all 50 images but check50 gives this

:) recover.c exists
:) recover.c compiles
:) handles lack of forensic image
:( recovers 000.jpg correctly
   \ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 215..."
:( recovers middle files correctly
   \ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 215..."
:( recovers last file correctly
   \ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 215..."
https://sandbox.cs50.net/checks/dab65ad7e34849e191fb37505e46ccfc

And here is my code:

/* To recover jpeg files given a raw file
*
*  Usage: ./recover image
*
*/

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

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

    //proper usage
    if(argc != 2){
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
    }

    //store raw file name
    char *card = argv[1];

    //open raw file
    FILE *raw = fopen(card, "r");
    if (raw == NULL){
        fprintf(stderr, "Could not open %s.\n", card);
        return 2;
    }   

    uint8_t buffer[512]; //to store a temporary block of memory of size 512 bytes

    int count = 0; //keep track of number of jpegs created

    FILE *outjpeg = NULL; //to create the jpeg
    char name[8]; //names of jepgs to be created

    //read in the bytes of the memory card, 512 blocks at a time, 1 byte each
    while(fread(buffer, 512, 1, raw)){

        //checking if start of a jpeg
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0){

            //closing previous files (excpet the first time)
            if(count != 0)
                fclose(outjpeg);

            //create a new jpg
            sprintf(name, "%03i.jpg", count++);
            outjpeg = fopen(name, "w");

        }

        //write the data to the jpeg
        fwrite(buffer, 512, 1, outjpeg);
    }

    //close card
    fclose(raw);

    //close jpg
    fclose(outjpeg);

    //success
    return 0;
}
1

This program seg faults if the first block read is not a jpg signature. It fails when it executes this line fwrite(buffer, 512, 1, outjpeg);, while outjpeg is NULL.

You must log in to answer this question.

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