0

This is the result of my code:

:) 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.
    if (argc != 2)
    {
        printf("$ ./recover card.raw\n");
        return 1;
    }
    FILE *file = fopen(argv[1], "r");
    
    BYTE newfile[512];
    
    if (file == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        return 2;
    }
    
    FILE *img = NULL;
    
    int n_jpegs = 0;
    
    int found = 0;
    
        while (fread(newfile, sizeof(newfile), 1, file))
       {
    
        if (is_start_of_a_new_jpeg(newfile) == 1)
        {
            if (found == 1)
            {
                // We found the start of a new pic so close out current picture
                fclose(img);
            }
            else
            {
                // jpg discovered and now we have the green light to write
                found = 1;
            }
            
            char filename[8];
            sprintf(filename, "%03i.jpg", n_jpegs);
            img = fopen(filename, "a");
            n_jpegs++;
            if (img == NULL)
            {
                return 1;
            }
            fwrite(&newfile, sizeof(newfile), 1, img);
        }
        
        else if (found == 1)//once new JPEGS are found
        {
            //copy over the blocks from buffer into new file
            fwrite(&newfile, sizeof(BYTE), 1, img);
        }
    }
    
    
    
    fclose(img);
    
    fclose(file);
    
    return 0;
1

The problem occurs when writing to an open file. You want to 512 to bytes using newfile, but you specify the write size to be of a BYTE. Change this to sizeof(newfile).

Hope this helps. If it does then please check the tickmark.

1
  • You should also open the files in write mode, not append mode. – Cliff B Mar 27 at 20:33

You must log in to answer this question.

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