0

I tried to implement recover through the simplest and shortest way I could imagine.

The code compiles and saves 50 JPGs with proper names, but when I open them, all of them are just empty and even ls -l shows that the files have 0B.

Also I tried debugging using printf and I found that each file is missing 1 block. I checked online and found that each file should have either 19 or 20 blocks, but my files have 18 or 19 blocks.

I couldn't figure out why the files are empty or why I have been missing 1 block.

Here's the code:

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

#define block_size 512
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover card\n");
        return 1;
    }
    FILE *card = fopen(argv[1], "r");
    if (card == NULL)
    {
        printf("%s: could not open\n", argv[1]);
        return 2;
    }

    FILE *jpg = NULL;
    BYTE buffer[block_size];
    int jpg_count = 0;

    // 7 characters for name + 1 character for null
    char jpg_name[8] = {0};



    // Iterate through the card
    while (fread(&buffer, 1, block_size, card) == block_size)
    {
        // Check for start of jpg
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff
        && (buffer[3] & 0xf0)== 0xe0)
        {
            // If found start close previous jpg if available
            if(.....)
            {
                fclose(jpg);
            }

            // Create new jpg
            sprintf(jpg_name, "%03i.jpg",jpg_count++);

            jpg = fopen(jpg_name, "w");
            if (jpg == NULL)
            {
                printf("error\n");
                fclose(jpg);
            }
        }
        // The first if and this if are not mutually exclusive
        // if jpg exists keep writing until EOF or next JPEG found(above condition)
        if(..........)
        {
            fwrite(&buffer, 1, block_size, jpg);
        }
    }

    fclose(jpg);
    fclose(card);
    return 0;
}

** Made a few tweaks to the code in order to make it compliant to the accademic honesty rule.

1
  • Best to not post the entire code as that contributes to academic dishonesty.
    – guyewhite
    Commented Oct 6, 2023 at 0:45

1 Answer 1

0

As it is known copy-pasting code is bad practice, I did the same mistake.

I finally found the mistake and changed:

fwrite(&buffer, 1, block_size, card);

with:

fwrite(&buffer, 1, block_size, jpg);

and the empty file problem was resolved. It was a silly mistake.

Also looks like the missing block is not actually missing. I have 50 complete JPGs and it passes check50.

4
  • if that answered your problem accept your answer
    – UpAndAdam
    Commented Oct 5, 2023 at 15:01
  • I'm sure he'll accept his/her own answer when possible. The system makes you wait before you can do so. Can't remember if it's 1 day or 3 days.
    – Cliff B
    Commented Oct 5, 2023 at 15:47
  • I can accept my own answer only after 2 days.
    – Fawz
    Commented Oct 6, 2023 at 3:48
  • One more thing I noticed is that my code works fine with or without & in the fread and fwrite lines. I wanted to know why there is no difference.
    – Fawz
    Commented Oct 6, 2023 at 3:55

You must log in to answer this question.

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