1

I'm really confused. I reviewed all similar problems, but did find my case.
It seems like jpeg start bytes are detected, but wrong data inserts to the output file. Please advise!

24-Oct-2016: The code is updated

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


int main(void)
{
    // open input file 
    FILE* inptr = fopen("card.raw", "r");
    if (inptr == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }
    FILE* outptr = 0;
    uint8_t temp [512];
    char outfile [8];
    int namenumber = 0;


        for (int i = 0; feof(inptr) == 0; i++ )
        {
            //check if it isn't EOF
            if (fread(&temp, 512, 1, inptr) != 1) break;
            //read input file
            fread(&temp, 512, 1, inptr);



            //determine if there is jpeg starting bytes
            if (temp[0] == 0xff && temp[1] == 0xd8 && temp[2]==0xff && (temp[3] >= 0xe0 || temp[3] <= 0xef) )
            {

               //close previously opened outputfile
                if(namenumber != 0)
            {
                fclose(outptr);
            }

                //naming the output files 
                sprintf(outfile, "%03d.jpg", namenumber);
                namenumber++;

                //open output file and check if it opens successfully
                outptr = fopen(outfile, "w");
                if (outptr == NULL)
                {
                    fclose(inptr);
                    printf("Could not create %s.\n", outfile);
                    return 3;
                }
                fwrite(&temp, 512, 1, outptr);

            }
            //check if exists opened file
            else if (namenumber !=0 )
            {
                //write data to output file
                fwrite(&temp, 512, 1, outptr);
            }
         }
    // close infile
    fclose(inptr);

    if(namenumber != 0)
    {
     fclose(outptr);
    }

    // that's all folks
    return 0;
}
0

There are two things that I see could be causing errors:

1.- You should close the outptr before opening a new one if it is already open.

I suggest you add something like:

if(namenumber != 0)
{
    fclose(outptr);
}

somewhere before line 38, where you open outptr.

2.- You should check for all of these bytes for temp[3]:

0xe0
0xe1
0xe2
0xe3
0xe4
0xe5
0xe6
0xe7
0xe8
0xe9
0xea
0xeb
0xec
0xed
0xee
0xef

Maybe you should change your last piece of the condition on line 30 from:

(temp[3] == 0xe0 || temp[3] == 0xe1)

to something like this:

UPDATED (SEE SECOND UPDATE)

(temp[3] >= 0xe0 || temp[3] <= 0xef)

BASED ON THE UPDATED CODE 24-OCT-2016

The end part of the condition is supposed to be:

UPDATED (SEE SECOND UPDATE)

(temp[3] >= 0xe0 || temp[3] <= 0xef)

not:

(temp[3] >= 0xe0 || temp[3] <= 0xe1)

You need to add this piece of code:

        if(namenumber != 0)
        {
            fclose(outptr);
        }

inside of the condition:

        //determine if there is jpeg starting bytes
        if (temp[0] == 0xff && temp[1] == 0xd8 && temp[2]==0xff && (temp[3] >= 0xe0 || temp[3] <= 0xef) )
        {

And it is ok that you have it outside of the loop (to close the last image). But in the updated code I see that piece twice outside of the loop:

    // close infile
    // ONE TIME
    fclose(inptr);
    if (namenumber !=0 )
    {
        // close outfile
        fclose(outptr);
    }

    // TWO TIMES
    if(namenumber != 0)
    {
    fclose(outptr);
    }
    // that's all folks
    return 0;
}

I think that will cause problems if it tries to close something that is not open.

I suggest you delete the second one.

SECOND UPDATE

I apologize, the expression:

(temp[3] >= 0xe0 || temp[3] <= 0xef)

has no worth. Any number (infinite amounts of them) will fit this description.

You can take out the parenthesis and change the OR by an AND:

temp[3] >= 0xe0 && temp[3] <= 0xef

Because you want it to be greater than or equal to 0xe0 AND less than or equal to 0xef. Not just any of those but BOTH at the same time.

My apologies again.

6
  • I've done the changes to my code, that you advised, but result is the same. I don't know why incorrect data is pasted to the output file. Oct 24 '16 at 10:51
  • Can you update the code in the question so I can debug it? Oct 24 '16 at 11:28
  • I've updated the code! Oct 24 '16 at 12:38
  • Thanks, I've updated my answer. Oct 24 '16 at 13:29
  • Thanks @Ricardo for helping, but result is still: Invalid or unsupported image format. Oct 24 '16 at 14:54

You must log in to answer this question.

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