1

I know pointers don't have to be used in this pset, but my first attempt relies on them.

The program has other bugs, but I'm wondering why my pointer variable doesn't get values (keep staying empty) thus not returning any JPEG (attaching a screenshot of the debugger below)?

Attaching a screenshot of the GNU debugger

Including the code:

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

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("Incorrect input. Correct usage: ./recover image_to_recover.raw");
    }

    // starts index to name recovered files
    int jpeg_file_index = 0;
    char jpeg_file_index_string[3];
    char jpeg_file_base[] = "JPEG_recovered_";

    // opens image where to recover files
    FILE *raw_data = fopen(argv[1],"r"); // update to take argv
    if (raw_data == NULL) // checks opening of image was successful
    {
        printf("%s wasn't found or wasn't successfully opened", argv[1]);
        return 1;
    }

// convert all variables used in fread / fwrite to pointers
    // jpeg file recovery logic
    // unsigned char *read_trial = (unsigned char *) malloc(1); // not going to use this variable, apart from using it in the below line to check if read is successful
    unsigned char *read_byte = (unsigned char *) malloc(4); // temporary holder for first 4 bytes of FAT section

    while (fread(read_byte, 1, 1, raw_data) == 1) // check if read is successful
    {
        fread(read_byte, 1, 3, raw_data);

        int read_bit[4]; // will store read bits of 4th byte
        unsigned char jpeg_bytes[3] = {0xff, 0xd8, 0xff};
        int jpeg_bits_4th_byte[4] = {1, 1, 1, 0};

        int trial_1 = read_byte[3];
        int trial_2 = read_byte[4];
        int trial_3 = read_byte[2];

        // read bits of 4th byte
        for (int i = 0; i < 4; i++)
        {
            if (read_byte[3] & (1 << (7 - i)))
            {
                read_bit[i] = 1;
            }
            else
            {
                read_bit[i] = 0;
            }
        }

        // check if the beginninf of the FAT section is the start of a jpeg file
        if (read_byte == jpeg_bytes && memcmp(read_bit, jpeg_bits_4th_byte, 1) == 0)
        // if (read_trial[0] == 0xff && read_trial[1] == 0xd8 && read_trial[2] == 0xff && )
        {
            sprintf(jpeg_file_index_string, "%d", jpeg_file_index);
            char *filename = (char *) malloc(strlen(jpeg_file_base) + strlen(jpeg_file_index_string));
            filename = strcat(jpeg_file_base, jpeg_file_index_string);
            FILE *output_file = fopen(filename,"w");

            // write FAT section into the ouptput file
            fwrite(read_byte, 1, 4, output_file);
            unsigned char *read_rest_of_bytes = (unsigned char *) malloc(508); // temporary holder for rest of FAT section
            fread(read_rest_of_bytes, 1, 508, raw_data);
            fwrite(read_rest_of_bytes, 1, 508, output_file);

            // checks if JPEG file is completed already at end of FAT section
            if (read_rest_of_bytes[507] == 0x00)
            {
                ;
            }
            else
            {
                unsigned char *read_fat_bytes = (unsigned char *) malloc(512); // temporary holder for next FAT section
                read_fat_bytes[511] = 0x00; // initialise variable with correct condition to ensure first run of below while loop

                // write next FAT section into the ouptput file, until JPEG file is completed
                while (read_rest_of_bytes[511] != 0x00)
                {
                    fread(read_fat_bytes, 1, 512, raw_data);
                    fwrite(read_fat_bytes, 1, 512, output_file);
                }
            }

            // increases the index used for naming JPEG files
            jpeg_file_index++;
        }
    }

    printf("JPEG recovery of %s has been completed.\n", argv[1]);
    return 1;
}

You must log in to answer this question.

Browse other questions tagged .