0

Edit: I checked the output and it turns out that I am getting duplicate images, can anyone help?


So I finally finished Recover, and it works fantastically whenever I run it outside of check50. I finally found the last image: enter image description here

But I can't seem to get my code to pass Check50:

Results for cs50/problems/2020/x/recover generated by check50 v3.1.2
:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:) recovers 000.jpg correctly
:( recovers middle images correctly
    recovered image does not match
:( recovers 049.jpg correctly
    recovered image does not match
To see the results in your browser go to https://submit.cs50.io/check50/a64373bc60a06b3de54521879bf784af412f6a73

Here is the code I used:

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



int counter = 0;
int findJpg(unsigned char buffer[512], int blocks);
int recover(FILE *ptr, unsigned char buffer[512],int blocks, int fileNumber);
int findAllJpg(FILE *ptr,unsigned char buffer[512], int blocks);

int main(int argc, char *argv[])
{
    //DONE: Open a memory card file
    if((argc-1)< 1)
    {
        printf("Please provide RAW file to work with.");
        return 1;
    }
    FILE *ptr = fopen(argv[1], "r");
    //Store a 512 bit block in an array
    unsigned char buffer[512];
    bool writing = false;
    int blocks = 0;
    //DONE: Look for the beginning of a JPEG

    //TODO: Open a new JPG File To begin writing to
    int fileNumber = 0;
    int offset = 512 * blocks;

    findAllJpg(ptr ,buffer, blocks);





    //DONE: Write 512 Bytes till a new JPEG is found
    //DONE: Close old JPEG
    //DONE: Open New one
    //DONE: Repeat
    //DONE: Close "ptr"
    fclose(ptr);

    return 0;
}

int findJpg(unsigned char buffer[512], int blocks)
{

    for(int j = 0; j < 512; j++)
        {

            if(buffer[j] == 0xff)
            {

                if(buffer[j+1] == 0xd8)
                {

                    if(buffer[j+2] == 0xff)
                    {

                        if((buffer[j+3] & 0xF0) == 0xe0 || buffer[j+3] == 0xe1)
                        {
                            return 2;

                        }
                    }
                }
            }
        }

//Since there will be no JPG header in this block, the program returns -1, a value which can't exist in an array.
    return -1;
}

int recover(FILE *ptr, unsigned char buffer[512],int blocks, int fileNumber)
{
    char *filename = malloc(3);
    sprintf(filename, "%03i.jpg", fileNumber);

    FILE *img = fopen(filename, "w");

    bool found = false;
    while(found == false)
    {
            while(true)
            {

                fwrite(buffer, 512, 1, img);
                fread(buffer, 512, 1, ptr);
                blocks++;

                if(findJpg(buffer, blocks) > -1)
                {
                    fseek(ptr, (512 * blocks), SEEK_SET);
                    found = true;
                    fclose(img);

                    
                    return 0;
                    break;
                }
            }
    }

    return -1;
}



//At first this was for debugging, but now it is fully integrated into the code. 
int findAllJpg(FILE *ptr,unsigned char buffer[512], int blocks)
{
    for(int i = 0; counter < 50 ;i++)
    {
        fread(buffer, 512, 1, ptr);
        for(int j = 0; j < 1; j++)
        {

            if(buffer[j] == 0xff)
            {

                if(buffer[j+1] == 0xd8)
                {


                    if(buffer[j+2] == 0xff)
                    {

                        if((buffer[j+3] & 0xF0) == 0xe0 || buffer[j+3] == 0xe1)
                        {
                            
                            recover(ptr, buffer, i, counter);
                            counter++;

                        }
                    }
                }
            }
        }
    }
    return 0;
}

You must log in to answer this question.

Browse other questions tagged .