0

I am using the code below for pset4 recover.c. I can see nothing wrong in this code. The code writes 16 jpgs but none of them is open able. The header of jpg files doesn't seem to have been written correctly.

Instead of starting with FF D8 FF E0 the jpg files start with 00 D8 FF E0. I have no idea how 00 comes instead of FF. Can anyone point out what's wrong?

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

typedef uint8_t  BYTE;
#define BLOCK_SIZE 512

int main(int argc, char **argv)
{
    // open raw file
    FILE* file = fopen("card.raw", "rb");
    if (file == NULL)
    {
        printf("Failed to open image!");
        fclose(file);
        return 1;
    }

    /*// get length of file
    fseek(file, 0, SEEK_END);
    long filelen = ftell(file);
    rewind(file);

    // allocate memory to store file contents
    char* buffer = (char*) malloc((filelen + 1) * sizeof(char));

    // read file contents into memory
    fread(buffer, filelen, 1, file);*/

    BYTE buffer[BLOCK_SIZE];
    int fileCount = 0;
    char ofileName[7] = "000.jpg";
    FILE* ofile = NULL;

    // go through file contents
    while (fread(buffer, BLOCK_SIZE, 1, file) != 0)
    {
        // search for jpg signature on the first 4 bytes of block
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            // close previous file if open
            if (ofile != NULL)
            {
                fclose(ofile);
                ofile = NULL;
            }

            // update output file name and increment file count
            sprintf(ofileName,"%.3d.jpg", ++fileCount);

            // open new file for writing
            ofile = fopen(ofileName, "w");

            // write block to file
            fwrite(buffer, BLOCK_SIZE, 1, ofile);
        }
        else
        {
            if (ofile != NULL)
            {
                // write block to file
                fwrite(buffer, BLOCK_SIZE, 1, ofile);
            }
        }
    }

    // close files
    fclose(ofile);
    fclose(file);
}
1

Interesting. I ran your code with different results. First, as written, it only creates one file. This is because your string var for the file name is one char too short. You didn't allow room for the string terminator '\0' on the end. It should have been 8, not 7.

Once that was fixed, it created all 16 files correctly, except that they were incorrectly named, an off-by-one problem that you should easily be able to fix.

Maybe you've been editing the code and saving it in one place but running another version?

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

2
  • File naming corrected! All 16 recovered...but the first image kinda looks weird. It shows a black silhoutte on an orange background? is this the correct image? – Hussain Noor Mohamed Sep 3 '15 at 11:27
  • Yes thats the correct first image! Kudos! – ChrisG Sep 3 '15 at 13:09

You must log in to answer this question.

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