0
/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */

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

typedef uint8_t  BYTE; 



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

    //check if file is null
    if (inptr ==NULL)
    {
        fclose(inptr);
        fprintf(stderr, "card.raw file not working \n");
        return 1;
    }

    char title[8];

    FILE* img = NULL;

    //Initialize variables
    BYTE buffer[512];
    int fnumber = 0; 
    int searchjpeg = 1; //1 if true, 0 if false

    //Cycle through each 512 MB block
    while(fread(&buffer, 512, 1, inptr)==1){


        if(searchjpeg==1){ //Start of new jpeg?
            if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1)) //4 of 16 possible
            { //yes
                searchjpeg = 0;

                sprintf(title, "%03d.jpg", fnumber);
                img = fopen(title, "w");
                fwrite(&buffer, 512, 1, img);
            }
            else
            { //no
                printf("nothing\n");
            }
        }



        else{ //already found a jpeg?
            if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
            { //yes
                fclose(img);
                fnumber++;

                sprintf(title, "%03d.jpg", fnumber);
                img = fopen(title, "w");
                fwrite(&buffer, 512, 1, img);
            }
            else
            { //no
                fwrite(&buffer, 512, 1, img);
            }
        }
    }
    fclose(img);
    free(buffer);
}

code works fine, produces all 50 images as intended

ran check50 2015.fall.pset4.recover recover.c

but got this error

Not sure what the problems are. Is it because I am not going a return statement at the end of the main argument?

EDIT Also I seem to have a memory leak as well from valgrind

0

Well, I see two issues, both easily fixed. First, your program prints out "nothing\n" repeatedly. It isn't supposed to print anything other than the error messages. check50 doesn't like this, even if your program is running correctly.

Second, I see a single 568 byte memory leak - the size of a file pointer. Did you forget to close an open file somewhere? ;-)

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

| improve this answer | |
  • I'm not sure where my memory leak is occuring. I thought i put fclose(inptr) for my original card.raw file, but that file would've been larger than 568 bytes. Valgrind isn't really giving me a more in-def answer either. I really don't know which file I have open. – Vincent Tang Nov 9 '16 at 20:14
  • I read your comments below. Adding the fclose(inptr) was correct, but I never said I found all of the problems. The last issue is that the code has a call to free(). The problem is that you can't call free() to any stack variable, like buffer. free() is only called when memory is allocated using malloc or one of its cousins. Interestingly, when I ran it with the call to free(), it generated errors. I'm surprised it ran for you at all. – Cliff B Nov 11 '16 at 20:27
0

It doesn't seem like you're doing closing the card.raw file anywhere. You should do fclose(inptr) after the while-loop as well.

| improve this answer | |

You must log in to answer this question.

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