0

I was hoping someone could help me with the following problem: when I run the recover.c program it does produce 50 jpeg images, but I am unable to view them (IDE error message "invalid or unsupported image format").

I've been trying to wrap my head around what I'm doing wrong. Should I be adding fread(buffer, 1, 512, inptr); before line 44? I believe that the while{} loop already executes the reading (until condition is not true), but perhaps that's where I went wrong.

Any help/tips would be greatly(!) appreciated.. :)

// recovers JPEG images

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

int main(int argc, char *argv[])
{
    // error check 1 command provided y/n
    if (argc != 2)
    {
        fprintf(stderr, "Provide name\n");
        return 1;
    }

    // remember infile name
    char *infile = argv[1];

    // open memory card file
    // error check file can be opened for reading y/n
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // temporary storage array 'buffer' of type uint8 (byte)
    typedef uint8_t BYTE;
    BYTE buffer[512];

    // declare FILE pointer and initialize to NULL
    // subsequently, if img is still equal to zero this means there is currently no opened file
    FILE *img = NULL;

    // declare char array for storing JPEG file names
    // declare int n to increment number for JPEG file name
    char filename[8];
    int n = 0;

    // once fread returns value less than 512, EOF has been reached
    while (fread(buffer, 1, 512, inptr) == 512)
    {
        // check if first 4 elements of current block equal JPEG format
        if (buffer[0] == 0xFF
            && buffer[1] == 0xD8
            && buffer[2] == 0xFF
            && ((buffer[3] & 0xF0) == 0xE0))
        {
            // if pointer is not equal to 0, close currently opened file
            // else open new file with "w"
            if (img != NULL)
            {
                fclose(img);
            }

            sprintf(filename, "%.3d.jpg", n);
            img = fopen(filename, "w");

            n++;

            // write the current block to the newly opened file
            fwrite(infile, 1, 512, img);
        }
        // the block did not start with JPEG format
        else
        {
            // the data in the block belongs to previously opened file (if img == 0, no JPEG data yet)
            if (img != NULL)
            {
                fwrite(infile, 1, 512, img);
            }
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(img);

    // success
    return 0;
}

1 Answer 1

0

I think I'll go with big hints on this one. ;-)

Have you looked at the first (say about 20 or 30) bytes of the output files using xxd?

Where are you getting the output data to write to the output file?

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

2
  • Feel slightly stupid for overlooking that hah.., but thanks so much! You saved me some hours of headache :) Commented Jun 8, 2018 at 1:45
  • Welcome to the club! ;-)
    – Cliff B
    Commented Jun 8, 2018 at 1:48

You must log in to answer this question.

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