0

Thanks for seeing my question, I appreciate your help. :)

Let's get to the point. My code compiles and runs without any errors. Images are generated, but the images are incomplete, here's an example of 000.jpg: https://i.imgur.com/Ovs2Xv8.png

#include <string.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: recover <raw_file>\n");
        return 1;
    }

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

    int jpegsFound = 0;
    unsigned char buffer[512];
    FILE *img;

    while (fread(&buffer, 1, 512, inptr) == 512)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // Block consists header of a new jpeg file

            if (jpegsFound > 0)
            {
                // Already found image, should close now
                fclose(img);
            }

            jpegsFound++;

            // Now handle the new file
            char filename[7]; // 000.jpg

            sprintf(filename, "%03i.jpg", jpegsFound - 1); // Start from 000.jpg

            img = fopen(filename, "w");
            if (img == NULL)
            {
                fclose(img);
                fprintf(stderr, "Could not create %s.\n", filename);
                return 3;
            }

            fwrite(&buffer, sizeof(buffer), 1, img);
        }

        if (jpegsFound > 0)
        {
            // A continued data of the last image
            fwrite(&buffer, sizeof(buffer), 1, img);
        }

        // Clears the buffer
        memset(&buffer, 0, 512);
    }

    fclose(inptr);
    fclose(img);

    return 0;
}
1

It's been a while since I've seen this one. Look at the following code:

        // Now handle the new file
        char filename[7]; // 000.jpg

        sprintf(filename, "%03i.jpg", jpegsFound);

When sprintf executes, it starts writing the data starting at the memory address given. In this case, it's writing to filename. Unfortunately, if insufficient space isn't allocated, sprintf will keep writing beyond the end of that space, overwriting whatever happens to be next in physical memory.

Now, filename had space for 7 chars. The filenames are actually 7 chars long. BUT, sprintf is also going to write the EOF char, which happens to be hex 0. Where do you think it's being put???? ;-)

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

3
  • Thanks :) It makes more sense now, thanks for the explanation. The first byte of image is now normal, but the image still seems broken. I've since updated my question - could you check it out? Jul 31 '18 at 3:26
  • Ummm..... no. The question no longer has any meaning to the answer. Could you please put the question back the way it was for the benefit of future readers? If you have a new question, please open it in a new question, but please take some time to work on it first. And don't forget to accept the answer. thanks.
    – Cliff B
    Jul 31 '18 at 3:30
  • Sorry - I have accepted your answer, after looking around I can't really tell where the problem is. I've opened another question, cs50.stackexchange.com/questions/29760/…. Jul 31 '18 at 3:44

You must log in to answer this question.

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