0

I am struggling along with recover.c of Pset4. I have pieced together - from the instructions and inspiration from other users - a program that I think should work. However, it does not create any jpgs - none at all, zero, nada, niente, nichts.

I would be immensely grateful for suggestions where I have gone wrong and maybe also some suggestions on how to improve on my code.

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




    int main(void)
    {




    //open the infile to inptr
    FILE* inptr = fopen("card.raw", "r");
    if(inptr == NULL)
    {
        printf("Cannot open card.raw!");
        return 2;
    }  

    //open outfile, setting it NULL to avoid Magic Number
    FILE* outptr = NULL;

    //make an array of 512 bytes/chars
    unsigned char buffer[512];

    //a numbering tracker for outfile naming
    int number_jpg = 0;

    //storage for the filename: array with size eight to store "000.jpg\0"
    char name[8];

    //start the loop reading in 512 bytes at a time which will stop when fread hits the end of the file
    while(fread(buffer,sizeof(BYTE)*512, 1, inptr) == 1)
    {

        //check if the first bytes indicate a jpeg file
        if(buffer[0]==0xff && buffer[1]==0xd8 && buffer[2]==0xff && (buffer[3]==0xe1 || buffer[3]==0xe2 || buffer[3]==0xe3 || buffer[3]==0xe4 || buffer[3]==0xe5 || buffer[3]==0xe6 || buffer[3]==0xe7 || buffer[3]==0xe8 || buffer[3]==0xe9 || buffer[3]==0xea || buffer[3]==0xeb || buffer[3]==0xec || buffer[3]==0xed || buffer[3]==0xee || buffer[3]==0xef))
        {
            //close old file (if there was one)
            if(outptr!=NULL)
            {
                fclose(outptr);

            }

            //sprint the title to variable name in format "000.jpg"
            sprintf(name, "%03d.jpg", number_jpg);
            number_jpg++;

            //open a new jpg file with write flag
            outptr = fopen(name, "w");

            //check if file opened
            if(outptr==NULL)
            {
                printf("Cannot create %03d.jpg!\n", number_jpg);
                return 2;
            }

            //write the first block of the new jpeg file
            fwrite(buffer, sizeof(BYTE)*512,1,outptr);
        }

        //write the rest of the buffer until the if condition triggers the loop above
        else if(outptr!=NULL)
        {
            fwrite(buffer, sizeof(BYTE)*512,1,outptr);
        }



    }

    //close remaining files
    fclose(inptr);
    if(outptr!=0)
    {
        fclose(outptr);
    }
    return 0;        
}
1

The code is almost correct. In fact, using the data set used by check50, it does find some of the files. (check50 uses a different data set. ;-) ) You should recheck what constitutes a valid signature.

Also, instead of such a long check for 16 sequential values, you could simplify the test condition. For instance, if you were checking to see if something was, say, 20, 21, 22, 23 or 24, you could if(x>=20 && x <= 24). I'll leave it to you to translate that into something useful. ;-)

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

3
  • thank you, very helpful. I changed the condition to buffer[3] >= 0xe0 && buffer[3]<=0xef works like a charm now
    – mor3dr3ad
    Aug 19 '16 at 9:59
  • But did you figure out why it didn't work before? ;-)
    – Cliff B
    Aug 19 '16 at 10:25
  • oh yeah, sorry. forgot to answer that part. yeah, I forgot the 0 signature in the 4th condition - once I added that and changed the condition to something cleverer, it all worked.
    – mor3dr3ad
    Aug 22 '16 at 7:41

You must log in to answer this question.

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