0

I can create 49 images inside of my loop, but I'm missing something when I write to file. The images are only partly showing. I watched the video three times where Brian explains how to approach this, and most of it makes sense, but I'm not completely grasping the process. How big is this file and why exactly am I writing 1 byte at a time? What am I writing to the memory card?

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

int main(int argc, char *argv[])
{
    // If your program is not executed with exactly one command-line argument, it should remind the user of correct usage, and main should return 1.
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
    }

    // Open memory card
    FILE *file = fopen(argv[1], "r");

    // If the forensic image cannot be opened for reading, program should inform the user, and main should return 2.
    while (!file || file == NULL)
    {
        fprintf(stderr, "File is either an invalid type, or it cannot be opened\n");
        return 2;
    }

    // img pointer initially set to NULL
    FILE *img = NULL;
    // filename needs to have xxx.jpg length + 1
    char filename[8];

    // Create a 512 BYTE block for memory
    unsigned char byte[512];

    // Set a counter to add num to image name and increment in loop to automate naming
    int counter = 0;

    // Read 512 bytes into a buffer of filename "file" 1 byte at a time
        // fread(data, size, number, inptr);
    while (fread(byte, 512, 1, file))
    {
        // Look at jpg signature in the first four bytes for a jpeg file
        if (byte[0] == 0xff && byte[1] == 0xd8 && byte[2] == 0xff && (byte[3] & 0xf0) == 0xe0)
        {
            // If start of new JPEG
            // sprintf(filename, "%03i.jpg", 2); counter
            sprintf(filename, "%03i.jpg", counter);
            // Open a new jpg file after naming it
            // Create a new file to add data to ###.jpg starting at 000.jpg
            img = fopen(filename, "w");
            // fwrite (data, size, number, outptr);
            fwrite(byte, sizeof(byte), 512, img);
            // Add to counter each time an image is written to
            counter++;
            if (feof(img))
            {
                fclose(img);
            }
        }
        else
        {
            //???
        }
    }
}```

1 Answer 1

1

I'm not getting a really clear picture of the problems you're having (no pun intended), but I'll tackle it anyways. First, you don't need to know how big the files are, just that their data will be in sequence in the input file. Once the code finds a block of data with a signature, that signals the beginning of a new output file. If one's already open, close it. Then, open a new file. When the code gets to the end of the input file, that's the end. Close all the files and the program.

The code is NOT writing just 1 byte at a time, it's writing 512 bytes. Look at the call:

        fwrite(byte, sizeof(byte), 512, img);

The total amount of data written is the result of the 2nd parameter x the 3rd parameter, or 1 x 512. The following command would be the same, although you should always follow the standard practice of putting the size of each unit in the 2nd parameter and the number of units in the 3rd:

        fwrite(byte, 512,  sizeof(byte), img);

The same argument applies to fwrite.

Next, why does the code check for feof(img)? That's the output file, not the input file.

Finally, remember that after the first block with a signature, all the blocks that follow need to be written out to an output file. Only when a new signature is detected should a new output file be opened. If all your output files are 512 bytes, this is definitely not being done!

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

0

You must log in to answer this question.

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