0

I have been working with recover for a long time and I have a great problem with detecting the beginning of a JPEG. I have tried for a long time to no avail. I feel my problem may be with buffer but I'm not so sure. All help is appreciated.

`

include

include

int main(int argc, char *argv[])

{

// checking if a file name is entered
if(argc != 2)
{
    printf("Usage: ./recover image\n");
    return 1;
}

//Assigning a value to the file entered
char *image = argv[1];
// opening a file to read from
FILE *JPG = fopen(image, "r");
// checking if file could be opened
if (JPG == NULL)
{
    printf("Could not open %s.\n", image);
    return 2;
}
// declaring buffer, my counter and img
unsigned char buffer[513];
int n = 000;
FILE* img = NULL;
int x = fread(buffer, 1, 511, JPG);

while (1 == 1)
{
    //reading the start of a file
    fread(buffer, 1, 511, JPG);
    if (fread(buffer, 1, 512, JPG) == 0 && n != 0)
    {
        return 0;
    }
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        img = fopen(image, "w");
        char filename[8];
        sprintf(filename, "%03i.jpg", n);
        //writing the bytes obtained while no start of a JPEG is found
        while (1 == 1)
        {     
            x = fread(buffer, 1, 512, JPG);           
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
            {
                break;
            }
            if (x != 512)
            {
                return 0;
            }
            fwrite(buffer, 1, 512, img);
        }
        fclose(img);
        fseek(JPG, -512, SEEK_CUR);
        n++;

    }
}

}

`

1

Your main issue is that you have created an array of unsigned ints (4 bytes each) to hold your 512 bytes. A byte is an unsigned char (1 byte). Make sure your buffer has the correct type.

Also, you are calling fread twice in a row so you end up missing every other block.

As well,

(buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] & 0xf0) != 0xe0)

is not the opposite. the opposite of (a=x && b=y) is (a!=x || b!=y)

2
  • I aleady changed it and the program stil doesn't detect new JPG's. Any other suggestion you can give me? – Omi11 Jun 18 '19 at 23:24
  • That still hasn't solved the problem. I made some changes but I just can't see why this happens. Thanks for the advise. – Omi11 Jun 19 '19 at 1:57

You must log in to answer this question.

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