-1

I've been slowly moving through recover, and have figured out how to recover all 50 images. Unfortunately, the program never stops running (I hit ctrl + c to stop it) and the 50th image, while viewable, is really, really big. The longer the program loops, the bigger it gets.

It seems clear my issue is that I'm not using feof incorrectly. I've been searching all over, and can't figure out how to make it work. I'm beginning to think there's an issue with the structure of the rest of my code, but I haven't been able to solve it.

I think it involves adding this code: if (fread(&current, sizeof(BUFFER), 1, card) == 1), but I've tried it within all my loops, and it results in only one jpg recovered.

I would very much appreciate the help. Thank you.

// define a struct to act as a buffer for reading/writing
typedef uint8_t  BYTE;
typedef struct
{
    BYTE    byte[512];
} __attribute__((__packed__))
BUFFER;

int main(void)
{
    // open the card and store in card
    FILE* card = fopen("card.raw", "r");

    // initialize an int for jpg counter
    int counter = 0;

    // initialize a boolean to determine if we're in the middle of a jpg
    bool middle = false;

    // define a BUFFER called current
    BUFFER current;

    // ensure file can be opened
    if (card == NULL)
    {
        printf("Could not open card.raw");
        return 1;
    }

    // loop until end of data on card    
    while (!feof(card))
    {
        // begin reading data 512 at a time, store it in buffer
        fread(&current, sizeof(BUFFER), 1, card);

        // check if start of block matches start of jpeg
        if (current.byte[0] == 0xff && current.byte[1] == 0xd8 && current.byte[2] == 0xff && (current.byte[3] == 0xe0 || current.byte[3] == 0xe1))
        {
            // increment counter, indicate in middle of jpg
            counter++;
            middle = true;

            // create a new file with name based on counter value
            char filename[8];                
            sprintf(filename,"%.3d.jpg", counter);
            FILE* newjpeg = fopen(filename, "a");

            if (newjpeg == NULL)
            {
                fclose(card);
                return 2;
            }

            // write buffer to new file
            fwrite(&current, sizeof(BUFFER), 1, newjpeg);

            // until hit another jpeg start
            while (middle)
            {
                // continue reading and writing next 512 block
                fread(&current, sizeof(BUFFER), 1, card);

                // check if found next jpeg
                if (current.byte[0] == 0xff && current.byte[1] == 0xd8 && current.byte[2] == 0xff && (current.byte[3] == 0xe0 || current.byte[3] == 0xe1))
                {
                    middle = false;

                    // close open file
                    fclose(newjpeg);

                    // fseek back one step
                    fseek(card, -sizeof(BUFFER), SEEK_CUR);                  
                }                

                // write what's there
                else
                {
                    fwrite(&current, sizeof(BUFFER), 1, newjpeg);
                }
            }
        }     
    }  

    //close file
    fclose(card);

    return 0;
}
1
  1. I'm not sure why you're unnecessarily wrapping your BYTE array within a struct. If you could directly use something like

    byte[0]
    

    why would you use something like

    current.byte[0]
    

    ?

  2. Your first JPG should be named 000.jpg. By you, increasing counter by 1 before you create the filename, you appear to be starting with 001.jpg instead.

  3. You're repeating a lot of code. When you find yourself repeating code, you should know that there's a better approach.

  4. The reason why your code might be stuck into an infinite loop is that the last JPG ends when the EOF condition is reached not when there's a start of a new JPG because there are no more JPGs at this point.

3
  • 1
    Uggh, OK. Thanks. Recover is killing me. I've rebuilt using arrays, but the best I can do is get it to write the first 512 bytes of 50 different jpegs. – sf_stackexchange Nov 17 '14 at 2:14
  • @sf_stackexchange maybe you should use gdb to debug your program! – kzidane Nov 17 '14 at 2:21
  • 1
    Yes, I've been GDB'ing the pants off it. I know where my issues were, but I couldn't figure out the proper scope and syntax to do what I wanted. Seems to be working well now. Thank you. – sf_stackexchange Nov 17 '14 at 20:11

You must log in to answer this question.

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