0

Why isn't check50 clearing even though the files are created and the images are visible?

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

typedef uint8_t BYTE;
typedef struct
{
    BYTE data[512];
} My512Bytes;

int main(int argc, char *argv[])
{
    // Proper Usage
    if (argc != 2)
    {
        printf("Error only 1 argument command required\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[1];

    // open input File
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("Could not open file %s. \n", infile);
        return 1;
    }

    // Read input File
    char jpgfile[50];
    My512Bytes bf;
    int filenum = 0;

    while (fread(&bf, sizeof(BYTE), 512, inptr) == 512)
    {
        if (bf.data[0] == 0xff && bf.data[1] == 0xd8 && bf.data[2] == 0xff)
        {
            if (bf.data[3] >= 0xe0 && bf.data[3] <= 0xef)
            {

                sprintf(jpgfile, "%03i.jpg", filenum);


                FILE *outfile = fopen(jpgfile, "w");
                if (outfile == NULL)
                {
                    printf("Error opening file for writing\n");
                    return 1;
                }
                fwrite(&bf, sizeof(My512Bytes), 1, outfile);

                fclose(outfile);
                filenum++;
            }
        }

        if (bf.data[0] != 0xff && bf.data[1] != 0xd8 && bf.data[2] != 0xff)
        {
            FILE *outfile = fopen(jpgfile, "a");
            if (outfile == NULL)
            {
                continue;
            }
            fwrite(&bf, sizeof(My512Bytes), 1, outfile);

            fclose(outfile);
        }
    }

    fclose(inptr);
}

This is the fail check50


:) recover.c exists.
Log
checking that recover.c exists...
:) recover.c compiles.
Log
running clang recover.c -o recover -std=c11 -ggdb -lm -lcs50...
:) handles lack of forensic image
Log
running ./recover...
checking that program exited with status 1...
:( recovers 000.jpg correctly
Cause
recovered image does not match
Log
running ./recover card.raw...
checking that program exited with status 0...
checking that 000.jpg exists...
hashing 000.jpg...
:( recovers middle images correctly
Cause
recovered image does not match
Log
running ./recover card.raw...
checking that program exited with status 0...
checking that 001.jpg exists...
hashing 001.jpg...
:( recovers 049.jpg correctly
Cause
recovered image does not match
Log
running ./recover card.raw...
checking that program exited with status 0...
checking that 049.jpg exists...
hashing 049.jpg...
:| program is free of memory errors
Cause
can't check until a frown turns upside down
3
  • 1
    what does check50 have to say? What tests are failing? Please post the link to the check50 detailed result.
    – Cliff B
    Aug 11, 2023 at 13:12
  • Sorry I have added it.
    – Don Tan
    Aug 11, 2023 at 16:10
  • Have you managed to fix your problem? I'm running into the same problem. Code outputs visible images 000.jpg to 049.jpg, even did a hex editor check. Valgrind shows no memory leaks, yet Check50 throws "no match". Sep 15, 2023 at 12:47

0

You must log in to answer this question.

Browse other questions tagged .