0

Long time lurker, first time poster.

I'm working on Pset 4 - Recover. My code is below. My expected behaviour is that this should parse through the provided card.raw file and extract the JPEGs. Compiling this code does not deliver any errors. However, when I run it I get nothing back (not a segmentation fault, warning, or any files created). Debug50 just shows it looping through the while loop continuously, never 'discovering' a first JPEG file.

Kind of going crazy trying to work this out.

Here is my code:

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

// Creating data type BYTE to represent one byte of data
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: recover datanamehere\n");
        return 1;
    }
    // Open input file
    FILE *file = fopen(argv[1], "r");
    // Check for a valid file
    if (file == NULL)
    {
        printf("Could not read file\n");
        return 1;
    }
    // Memory allocation for buffer
    int* buffer = malloc(sizeof(BYTE) * 512);

    // Initialised target file
    FILE *target_file = NULL;

    // Counter to keep track of how many files have been extracted, and what the current JPEG file name should be
    int jpeg_count = 0;

    // Read 512 bytes of file data into a buffer
    while (fread(buffer, sizeof(BYTE), 512, file) == 512)
    {

        // String to hold the label of the jpeg file (like '004.jpg')
        char jpeg_label[8];
        sprintf(jpeg_label, "%03i.jpg", jpeg_count);

        // If this 512 bytes represents a new JPEG...
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // If this is the very first JPEG
            if (jpeg_count == 0)
            {
                // Create a new JPEG file
                target_file = fopen(jpeg_label, "w");
                // Write the data to the new JPEG file
                fwrite(buffer, sizeof(BYTE), 512, target_file);
                // Increase the JPEG count by one
                jpeg_count++;
                printf("JPEG_COUNT: %i\n", jpeg_count);
            }
            // If this is not the very first JPEG
            else
            {
                // Close the file currently being worked on
                fclose(target_file);
                // Increase the JPEG count
                jpeg_count++;
                // Create a new JPEG label
                sprintf(jpeg_label, "%03i.jpg", jpeg_count);
                // Create a new JPEG file to write to
                target_file = fopen(jpeg_label, "w");
                // Write the data to the new JPEG file
                fwrite(buffer, sizeof(BYTE), 512, target_file);
                printf("JPEG_COUNT: %i\n", jpeg_count);
            }
        }
        // The current 512 slice of data is an extension of the prior JPEG
        else
        {
            // If there is a prior JPEG
            if(jpeg_count > 0)
            {
                // Continue to write to that JPEG
                fwrite(buffer, sizeof(BYTE), 512, target_file);
            }
        }
    }
    if (target_file != NULL)
    {
        fclose(target_file);
    }
        if (target_file != NULL)
    {
        fclose(file);
    }
    free(buffer);
}
0

First the good news. No infinite loop here. There's just a lot of data and the while loop has to run through it all. It's just not finding a signature block.

The problem lies here:

// Memory allocation for buffer
int* buffer = malloc(sizeof(BYTE) * 512);

By declaring buffer as an int pointer, it's affecting how the data is being stored, and wreaking havoc on the data. (Try looking at the first line of raw data in the input file and compare it to what's stored in buffer on the first read to see what happens.)

If you change it to something more appropriate, like uint8_t, it works. Seems like you already defined uint8_t as BYTE in the program.

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

| improve this answer | |
  • Thanks so much! I never would have noticed that myself. – Jason A Aug 17 at 4:15

You must log in to answer this question.

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