0

So far, I have managed to find the byte-sequence that a block in the .jpeg file starts with. Now I don't know how to tell the program to stop at the next such block.

Moreover, I don't know how I should use sprintf to name the file. I have read up on sprintf, but I don't really see how it can help me to name a file.

Here is my code:

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

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


int main(int argc, char* argv[])
{
    //Open the file
    FILE* in_p = fopen("card.raw", "r");

    //Check if the file exists and can be opened
    if (in_p == NULL)
    {
        printf("Could not open 'card.raw'.\n");
        return 1;
    }


    //Declare the buffer where the block of 512 bytes will be stored
    unsigned char buffer[512];

    //Read from the file until it ends
    while (fread(&buffer, sizeof(buffer), 1, in_p))
    {
        //const char mark_1[4] =
        if  (buffer[0] == 0xff &&
             buffer[1] == 0xd8 &&
             buffer[2] == 0xff &&
             (buffer[3] == 0xe0 || buffer[3] == 0xe1))


        {
        // If the jpeg-sequence is found write into new jpeg
        // until a new jpeg-sequence is found
            printf("found \n ");
            break;
            //FILE* out_p = fopen("title", "a");
            //fwrite(&buffer, sizeof(buffer), 1, out_p);



            //fclose(out_p);
        }

        else
        {
            printf("not found \n");

        }
        // when found, start at the beginning of the block and write it


        //when we hit it, every block has to be written to a new jpeg file (so write)

        //when we hit the next block, we close the last jpeg and start writing to a new jpeg file

        //sprintf(char title[8], "%.3d.jpg", 2)
        //title: char array to store the resultant string
        //sprintf(title[8], "%.3d.jpg", 2);

        //this prints out 2.jpg and not 002.jpg (NOT ANYMORE)
    }

    // Close "memory file"
    //fclose(in_p);
    //return 0;

}

Thanks.

3

You have two questions here:

1. Now I don't know how to tell the program to stop at the next such block.

According to the problem set specification, the program needs to read the data in blocks of 512 bytes. Your program appears to be doing this correctly. For each block read, the program must write the block to a file. Before writing the block the program must check the contents and decide what to do.

If the block has the JPEG signature, then the program knows that the block is the start of a new JPEG image, and must start a new file, then write the block to it.

If the block does not have a JPEG signature, then the block belongs to the JPEG that is being written to, and it can just append the block to the current JPEG file.

The pseudo code looks something like this:

while (read a block of 512 bytes) {

    if (the block starts with a JPEG header) {

       Increment the file number
       Generate the file name

       if (JPEG file is open) {

           Close JPEG file
       }

       file = Open a new JPEG file
    }

    Write the block to JPEG file
}

if (JPEG file is open) {

    Close JPEG file
}

2. Moreover, I don't know how I should use sprintf to name the file. I have read up on sprintf, but I don't really see how it can help me to name a file.

'sprintf' is like 'printf' in the sense that it takes arguments which are assembled into a homogenous string. The difference is that where 'printf' will always output to the display, 'sprintf' will output the string into a location/variable in memory.

This is useful because it allows you to generate a string which you can use inside your program. In this pset, the string you need to generate is the name of the file which the blocks need to be written to. In your case you are storing the file name in a variable called 'title'. This variable must be passed to 'fopen' as the name of the file to open.

2
  • How to check if JPEG file is open ? – AziCode Feb 26 '16 at 20:04
  • @AziCode if (fp != NULL) { fclose(fp); fp = NULL; };. – Luke Van In Jul 12 '16 at 9:14

You must log in to answer this question.

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