0

I have gone through about 12 questions on the same issue - the code for recover works, but check50 isn't being kind:

:) 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
:| program is free of memory errors
    can't check until a frown turns upside down**

However, I still haven't been able to figure out where I'm making the mistake. I'd immensely appreciate it if someone could direct me towards the issue in my code:

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

typedef uint8_t BYTE;
int BLOCK_SIZE = 512;

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

    if (argc != 2)
    {
        printf("./recover [name of a forensic image]\n");
        return 1;
    }

    FILE *file = fopen(argv[1], "r");

    if (file == NULL)
    {
        printf("forensic image cannot be opened for reading\n");
        return 1;
    }

    BYTE buffer[512];
    char *filename = malloc(8*(sizeof(char)));

    int counter = 0;

    sprintf(filename, "%03i.jpg", counter);
    FILE *img = fopen(filename, "w");

    while (fread(buffer, 1, BLOCK_SIZE, file) == BLOCK_SIZE)
    {
        if (buffer[0] == 0xff && buffer[0] == 0xff && buffer[0] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            counter++;
            if (counter > 1)
            {
                fclose(img);
                sprintf(filename, "%03i.jpg", counter - 1);
                img = fopen(filename, "w");
            }
        }

        if (counter >= 1)
        {
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
    }

    fclose(img);
    fclose(file);
    free(filename);
}

1 Answer 1

2

actually, the code seems to be creating 52 files, numbered 000 to 051, so something is amiss.

Perhaps you should consider reworking the logic a little bit?

Hint: why open an output file before the first signature block is found? What if the input file didn't have any signature code blocks and was just garbage data? This code would have opened at least one bad file unnecessarily.

It is possible to create a file pointer without opening a file.

FILE *output = NULL;

This should also allow you to simplify the use of counter in the code.

Otherwise, the code is written almost as efficiently as it can be for this pset.

1
  • Thank you for the tips mate! That really helped. Plus, I was making a silly mistake in the while loop (it should be buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0). However, check50 hits me with all greens except for the Valgrind (final) condition. Cannot figure out why I have a memory leak of 472 bytes in 1 block. Posting a separate question for it. Would immensely appreciate your help on that one. Thanks once again! Jan 20, 2023 at 13:13

You must log in to answer this question.

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