0

I can't figure out what is wrong with my code. I recovered images from 000.jpg to 050.jpg. This is my code

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // only take filename as input
    if (argc != 2)
    {
        fprintf(stderr, "./recover file\n");
        return 1;
    }
    // open file
    FILE *fptr = fopen(argv[1], "r");
    if (fptr == NULL)
    {
        fprintf(stderr, "Cannot open file\n");
    }
    // array to store
    unsigned char *buffer = malloc(sizeof(BYTE) * 512);
    // counter
    int j = 0;

    // file to write into
    FILE *pic = NULL;

    while (fread(buffer, 512, 1, fptr))
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            j++;
        }
        if (j > 2)
        {
            fclose(pic);
        }
        char temp[100];
        sprintf(temp, "%.03i.jpg", j);
        pic = fopen(temp, "a");

        // write from temp
        fwrite(buffer, 512, 1, pic);
    }
fclose(fptr);
fclose(pic);
return 0;

}

It shows this error message

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
    recovered image does not match
:( recovers middle images correctly
    recovered image does not match
:( recovers 049.jpg correctly
    recovered image does not match

Please help!

1 Answer 1

0

When you say "I recovered images", did you actually look at the jpgs you created? Were you able to open each one and see a photo?

I'd revisit your logic in your reading/writing loop. When do you want to open a new file? When do you want to write to that file? You currently have something like:

read in 512 bytes
    if it's a jpg signature, increment j.
    if j > 2, close file.
    open a file and write the block to it.

Do you want to write a block every time?

2
  • Yes I able to open the jpgs and see see a photo.
    – Deku
    May 14, 2019 at 4:48
  • If you remove all the jpgs in your folder, then run recover again, can you still open 000.jpg?
    – curiouskiwi
    May 15, 2019 at 1:39

You must log in to answer this question.

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