0

could you please help me to understand why my program is not passing the check - the error says "timed out while program is trying to exit". It's compiling well, but seems to not even create any jpg's... I understand that it might be because the FILE *img pointer could be pointing somewhere wrong or be null, but can't figure out how to create it in a more correct way :(

Thanks in advance for your suggestions!

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t  BYTE;

int main(int argc, char *argv[])
{
    BYTE buffer[512];
    if (argc !=2) //checking if there's exactly one command-line argument
    {
        printf("Missing argument!");
        return 1;
    }
    char *input_file = argv[1];
    FILE *input_ptr = fopen(input_file, "r");
    if (input_ptr == NULL) // checking if the forensic image can be opened for reading
    {
        printf("Error: cannot open %s\n", input_file);
        return 2;
    }
    int n = 0;
    FILE *img;
    while (fread(buffer, sizeof(BYTE), 512, input_ptr) != 0) // loop to be repeated until the end of file
    {
        if (buffer[0] == 0xff && buffer[1] == 0xdf && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) // check if it's the start of jpg
        {
            char filename [8];
            if (n == 0)// if it's the 1st jpg
            {
                sprintf(filename, "%03i.jpg", n);
                img = fopen(filename, "w");
                fwrite(buffer, sizeof(BYTE), 512, img);
                n++;
            }
            else // if it's not the 1st jpg
            {
                fclose(img);
                sprintf(filename, "%03i.jpg", n);
                img = fopen(filename, "w");
                fwrite(buffer, sizeof(BYTE), 512, img);
                n++;
            }
        }
        else if (img != NULL)// if it's not a start of jpg, so need to keep copying bytes into a jpg that's already been discovered
        {
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
    }
    fclose(img);
    fclose(input_ptr);
}
0

Looks like a typo on the buffer[1] comparison. It never finds a jpeg signature. I would have expected it to fail at fclose(img);; it likely doesn't because img is never initialized; it just hangs.

4
  • Same typo as the last question. – Cliff B Dec 30 '20 at 20:37
  • Thanks, now I understood which typo! :) – J_K Dec 30 '20 at 20:44
  • Still, after correcting the typo, the program is still not passing the check: again "timed out while waiting for program to exit" – J_K Dec 30 '20 at 20:45
  • THANK YOU - I've also changed the code a bit to initialise img before the loop starts - and now it works! Thanks for your suggestions! – J_K Dec 30 '20 at 20:50

You must log in to answer this question.

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