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];

    //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);
                FILE* 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(title);
                fnumber++;

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

Errors I'm getting, mostly dealing with filenaming

I am not sure if this code works correctly but I can't seem to get around to compiling

I'm not sure how I am supposed to pass the string of "title" over to the sprintf function. (*title, &title, or title?)

Also, I have file "img" declared in a if-else nested statement that isn't declared originally should that condition be met, not sure how to do that

Also, if you use while(fread(&buffer, 512, 1, inptr)==1) does in a logic statement does this actually read the buffer or just create a logic statement only?

1
  • You used the following: 'BYTE buffer[512]' but as far as I know, BYTE is not something like int, char etc. So how can you define something with BYTE without defining what BYTE is? – Tejesh Atr Jul 16 '17 at 10:07
0

title is a string var used to build a file name, which is passed to the img file pointer using fopen. From there on, any time the code writes to the output file, or if the file needs to be closed, the code should use img, not title.

Later, the error with img happens because img is out of scope and no longer exists. It was declared inside of an if code block. Once that block ends, it no longer exists. It should have been declared (and initialized) outside of any curly braces that define a code block, but still inside main.

To answer your final question, while(fread...) does both. It performs the fread, and does a logic compare between the return value from fread and 1.

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

1

You must log in to answer this question.

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