0

I run my code and can see all 50 images, but check50 says:

:) 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 015.jpg correctly
    015.jpg not found

I've read plenty of posts here about the issue with Recover/check50 and I still couldn't find where my code is wrong. Here it is:

 #include < stdint.h> 

 #include < stdio.h>

 #include < stdlib.h>

typedef uint8_t BYTE;

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

{

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

    // define input file name
    char *infile = argv[1];

    // define output file name
    char *outfile = 0;

    // define filename
    char filename[7];

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

    // counter of .jpg files created
    int image = 0;

    // name the outfile
    sprintf(filename, "%03i.jpg", image);

    // open output file
    FILE *outptr = fopen(filename, "w");
    if (outptr == NULL)
    {
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // temporary storage for 512-byte chunk of data, called "block"
    BYTE block[512];

    int start = 0;

    // find first JPEG
    while (start == 0)
    {
        // read block
        fread(&block, 1, 512, inptr);

        // check if it's the beginning of a JPEG
        if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
        {
            fwrite(&block, 1, 512, outptr);
            start++;
        }
    }

    int eof = 0;

    // stop if EOF reached
    while (eof == 0)
    {
        if (fread(&block, 1, 512, inptr) == 512)
        {
            if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xff) == 0xe0)
            {
                fclose(outptr);
                image++;

                // create new output file
                sprintf(filename, "%03i.jpg", image);

                // open new output file
                fopen(filename, "w");
                if (outptr == NULL)
                {
                    fprintf(stderr, "Could not create %s.\n", outfile);
                    return 3;
                }

                // copy 512 bytes to output file
                fwrite(&block, 1, 512, outptr);
            }
            else
            {
                fwrite(&block, 1, 512, outptr);
            }
        }
        else
        {
            eof++;
        }
    }

    // close the outfile
    fclose(outptr);

    // close infile
    fclose(inptr);

    // success
    return 0;
}

Could someone please help me find the bug? Thanks a lot!!

1

hehe, the different test data has snagged another victim! You've been snagged by check50's use of a different set of test data than what is provided! The problem doesn't show up with your test data. (A hidden lesson that demonstrates the need for exhaustive and comprehensive testing of code. )

Let's look at two lines of code:

// from first while loop
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff 
    && (block[3] & 0xf0) == 0xe0)

// from second while loop
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff 
    && (block[3] & 0xff) == 0xe0)

See any problems???? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • No way did I make such a stupid typo!!! :-D Thank you so much for bringing back my sanity, Cliff, it works perfectly now! :-) – Mata Kralova Sep 4 '17 at 6:51
  • It's always the simple ones, the ones that hide in plain sight! – Cliff B Sep 4 '17 at 7:17

You must log in to answer this question.

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