0

My program runs and it seems like nothing happens. I added a printf statement to show the current position in the infile. It updates and shows that its reading the memory card but never finds a jpg signature. Near the end of the file tho it reaches a location and remains at that location like its re-reading it over and over until I terminate. Can someone check my code. Does my logic make sense?

int check(FILE* input)
{
    int check[4];
    fread(check, sizeof(int), 4, input);
    if (check[0] == 0xff && check[1] == 0xd8 && check[2] == 0xff && (check[3] == 0xe0 || check[3] == 0xe1))
    {
        return 0;
    }
    else
        return 1;
}

and in my main function

do
{
    checker = check(inptr);
    unsigned long position = ftell(inptr);
    printf("%lu\n", position);
}
while (checker == 1);

// when signature found
if (checker == 0)
{
    do
    {
        // create and open new img file
        char title[8];
        sprintf(title, "00%c.jpg", (char) count);
        FILE* img = fopen(title, "w");

        // write 512 byte blocks while no new signature met
        do
        {
            fseek(inptr, -4 * sizeof(int), SEEK_CUR);
            write(inptr, img);
        }
        while (check(inptr) == 1);

        // if found increment counter & close old image
        if (check(inptr) == 0)
        {
            count++;
            fclose(img);
        }
    }
    while (fread(buffer, 512, 1, inptr) == 512);

UPDATE: Ok my program now runs but only finds 1 image. Inside my main function is the following updated loop:

// when signature found, while fread successfully reads 512 bytes
do
{
    // create and open new img file
    char title[8];

    sprintf(title, "00%d.jpg", count);
    FILE* img = fopen(title, "w");
    if (count > 0)
    {
        // shifts file position indicator since end of loop reads 512 block of memory
        fseek(inptr, 512, SEEK_CUR);
    }

    // write 512 byte blocks while no new signature met
    do
    {

        fwrite(buffer, 512, 1, img);
        buffer = check(inptr, buffer);
    }
    while (checker == 1);

    // if found, increment counter & close old image
    count++;
    fclose(img);
}
while (fread(buffer, 512, 1, inptr) == 512);

The do while loop is supposed to repeat the process done to find the first image until fread can no longer successfully read a 512 block of memory and thus signifying the end of the file. Is my thinking wrong ?

1

There are several issues with the code. First, look at your check function. You are reading 4 bytes into an array of 4 integers. An integer is 4 bytes long each, not 1. This does work, but not the best way to do this. A byte array would be better, i.e., uint8_t instead of int.

Next, it reads in 4 bytes at a time and checks them against the two possible signatures. This has a couple of problems. It is checking against every 4 byte string in the file until it finds a signature when it should only be checking the first 4 bytes of every 512 byte block. If it happens to hit a random set of data that matches a signature in the middle of a block, it will detect it as a false positive. From that point on, everything is wrong.

Also, checking every 4 bytes instead of only the first 4 bytes of each block is going to increase processing time dramatically. (BTW, it seems like it's doing nothing because it is reading in about 7kb of garbage data that's mostly zeros before the first signature.)

Perhaps a rethinking of your code is in order. After making changes, then look at what issues remain. At least this should get you going.

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

3
  • Oh wow I must have misunderstood the spec. I didn't realise a signature could only be at the beginning of a 512 block. Thanks i'll update in a couple hours. – minniebot Dec 13 '15 at 19:18
  • You might also want to investigate fwrite() vs. write(). – Cliff B Dec 13 '15 at 22:53
  • Hey please check the updated code – minniebot Dec 15 '15 at 14:12

You must log in to answer this question.

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