0

Sorry for asking such a general question but I cannot understand why my code does not recover the images fully. In most of the images one can see a fraction of the image. check50 tells me that there is a segmentation fault, when trying to open the images.

typedef uint8_t BYTE;

bool jpeg(BYTE *pt);

int main(int argc, char *argv[]){
    // If your program is not executed with exactly one command-line argument, it should remind the user.
    if (argc != 2)
    {
        printf("Usage: ./recover [filename]\n");
        return 1;
    }

    // Open the forensic image given for reading.
    FILE *fp = fopen(argv[1], "r");

    // If the forensic image cannot be opened for reading, your program should inform the user.
    if (fp == NULL)
    {
        printf("unable to open the file %s for reading\n", argv[1]);
        return 1;
    }

    // Determine size of file.
    fseek(fp, 0, SEEK_END);    /* file pointer at the end of file */
    const int size = ftell(fp);   /* take a position of file pointer */
    printf("The size of given file is : %d\n", size);

    // Reset stream to start.
    rewind(fp);

    // Establish a pointer to the beginning of an array of #size bytes.
    BYTE *buffer = malloc(512 * size);

    // Read the complete image to the buffer. 512 bytes at a time.
    fread(buffer, 512, size, fp);

    int jpg_counter = 0;
    // Check for jpegs!
    for (int i = 0; i <= size; i++)
    {
        if (jpeg(&buffer[i]) == true)
        {
            // Name the file ###.jpg
            char *filename = malloc(3);
            sprintf(filename, "%03i.jpg", jpg_counter);
            printf("created %s\n", filename);

            // Open new file with that name.
            FILE *new_jpg = fopen(filename, "w");

            // Detect end of jpg file aka beginning of new jpg.
            int j = 0;
            do
            {
                fwrite(&buffer[i+j], 512, 1, new_jpg);
                j++;

                if (i+j == size)
                {
                    break;
                }
            }
            while (jpeg(&buffer[i+j]) == false);

            // Close file.
            fclose(new_jpg);

            jpg_counter++;

        }
    }
    printf("%i jpgs found.\n", jpg_counter);
}

// Detect the begin of a jpeg
bool jpeg (BYTE *pt)
{
    if (pt[0] == 0xff && pt[1] == 0xd8 && pt[2] == 0xff && (pt[3] & 0xf0) == 0xe0)
    {
        return true;            // returns true, if jpeg
    }
    else
    {
        return false;           // returns false, if not a jpeg
    }
}

OUTPUT:

The size of given file is : 3744768
created 000.jpg
created 001.jpg
created 002.jpg
created 003.jpg
created 004.jpg
created 005.jpg
created 006.jpg
created 007.jpg
created 008.jpg
created 009.jpg
created 010.jpg
created 011.jpg
created 012.jpg
created 013.jpg
created 014.jpg
created 015.jpg
created 016.jpg
created 017.jpg
created 018.jpg
created 019.jpg
created 020.jpg
created 021.jpg
created 022.jpg
created 023.jpg
created 024.jpg
created 025.jpg
created 026.jpg
created 027.jpg
created 028.jpg
created 029.jpg
created 030.jpg
created 031.jpg
created 032.jpg
created 033.jpg
created 034.jpg
created 035.jpg
created 036.jpg
created 037.jpg
created 038.jpg
created 039.jpg
created 040.jpg
created 041.jpg
created 042.jpg
created 043.jpg
created 044.jpg
created 045.jpg
created 046.jpg
created 047.jpg
created 048.jpg
created 049.jpg
50 jpgs found.

You must log in to answer this question.

Browse other questions tagged .