0

It all seems in order, expect that when i try to open the jpg, the image viewer complains that a jpeg should start with "0xff" and not "0x00". I have no idea what i am doing wrong here! If you could give me an hint, i would aprecciate a lot!

/**
* recover.c
*
* Recovers JPEGs from a forensic image.
*/

#include <stdio.h>


// jpeg's signitures
char jpeg_v1[] = {0xff,0xd8,0xff,0xe0};
char jpeg_v2[] = {0xff,0xd8,0xff,0xe1}; 

// prototype
int validation (char buffer[]);

int main(void)
{
    // Open raw file
    FILE *raw = fopen("card.raw", "r");

    // checking for errors
    if (raw == NULL) 
    {
        printf("\nFailed to open raw file!");
        return 1;
    }

    //Pointer to each jpeg
    FILE *image = NULL;

    // name, counter,buffer
    short counter = 0;
    char buffer[512];
    char name[6+1];
    short state = 0;


    while(fread(buffer, sizeof(buffer), 1, raw) > 0)
    {
        if (validation(buffer) == 1) {
            if (state == 1) {
                fclose(image);
                state = 0;
                }
            if (state == 0) {   
                sprintf(name, "%.3i.jpg", counter);
                image = fopen(name, "w");
                counter++;
                fwrite(buffer, sizeof(buffer), 1, image);
                state = 1;
                continue;
            }
        }
        if (state == 1)
            fwrite (buffer, sizeof(buffer), 1, image);

    }
    fclose(image);
    return 0;
}

// comparing the first 4B of each 512B
int validation (char buffer[])
{
    if (jpeg_v1[0] == buffer[0]) {
        if(jpeg_v1[1] == buffer[1]) {
            if(jpeg_v1[2] == buffer[2]) {
                if(jpeg_v1[3] == buffer[3]) {
                    return 1;
                }
            }
        }
    }
    if (jpeg_v2[0] == buffer[0]) {
        if(jpeg_v2[1] == buffer[1]) {
            if(jpeg_v2[2] == buffer[2]) {
                if(jpeg_v2[3] == buffer[3]) {
                    return 1;
                }
            }
        }
    }
    return 0;
}
1

The following line is causing a problem with the file pointer:

char name[6+1];

name is then used in the following lines to create the file name and pointer:

            sprintf(name, "%.3i.jpg", counter);
            image = fopen(name, "w");

The problem is that name was declared too short. You didn't allow space for the string terminator character \0 at the end. It should have been declared to be 8 characters, not 7. This is causing problems with the file pointer and the output files. When I changed this, the program works.

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

1
  • It is funny how a simple number can mess up the entire program! Thanks a lot buddy! – David Lopes Oct 14 '15 at 18:18

You must log in to answer this question.

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