0

My recover.c program only recovers 2 images, although when I tested how many loops it did, it did 50. Both images recovered seem complete, nothing weird about the images. Check50 also confirmed the first image is correct, but my program only recover 000.jpg and 001.jpg. Where did I go wrong? Thanks!

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

int main(int argc, char *argv[])
{
if (argc != 2)
{
    fprintf(stderr, "Usage: copy infile outfile\n");
    return 1;
}
//Name of infile
char* infile = argv[1];

//Open input file
FILE *inptr = fopen(infile, "r");

if (inptr == NULL)
{
    fprintf(stderr, "File cannot be oopened\n");
    return 2;
}

//Read first 4 bytes of the 512 byte block
//New file starts at 000, +1 for each consecutive file
for (int a = 0; a < 10; a++)
{
    for (int b = 0; b < 10; b++)
    {
        for (int c = 0; c < 10; c++)
        {
            //Creating output filename
            char outfile[7];
            sprintf(outfile, "%d%d%d.jpg", a, b, c);

            FILE *outptr = fopen(outfile, "w");
            if (outptr == NULL)
            {
                fprintf(stderr, "Output file cannot be written");
                return 2;
            }


            for (int i = 0; i < 1; i++)
            {
                BYTE fb[512];
                fread(fb, 512, 1, inptr);
                if ((fb[0] == 0xff) && (fb[1] == 0xd8) && (fb[2] == 0xff) && (fb[3] >= 0xe0 && fb[3] <= 0xef))
                {
                    fwrite(fb, 512, 1, outptr);
                }
                else
                {
                    i--;
                }
            }

            for (int i = 0; i < 1; i++)
            {
                BYTE fb[512];
                if(fread(fb, 512, 1, inptr) == 1)
                {
                    if ((fb[0] == 0xff) && (fb[1] == 0xd8) && (fb[2] == 0xff) && (fb[3] >= 0xe0 && fb[3] <= 0xef))
                    {
                       fclose(outptr);
                       fseek(inptr, -512, SEEK_CUR);
                    }
                    else
                    {
                        fwrite(fb, 512, 1, outptr);
                        i--;
                    }
                }
                else
                {
                    fclose(outptr);
                    return 0;
                }
            }
        }
    }
}

}

1

Congratulations, you have made the most complex program I have seen to recover (I do not say it with irony, it has merit), only a small detail. A literal string such as "001.jpg" actually occupies eight characters and not seven as you do in your program, keep in mind that the compiler automatically adds the character end of string '\ 0', which really occupies a space, as there is no enough space we are overwriting in memory addresses that we should not touch. This fact has unexpected effects, as you see in your program your images are not recovered, we could also have a segfault in execution time, and of course it supposes a vulnerability (see attack by buffer overflow). I hope it helps

2
  • It worked :) I do tend to miss several small details, thank you! Regarding your comment about it being most complex, I understand that my program perhaps looks very caveman-like, not using malloc and such. Is this behaviour unhealthy in the long-term or is it okay? Perhaps there are things that I need to change in my style of programming? – Rob Evans Nov 23 '18 at 5:55
  • Making a program much simple is difficult, not only requires well-established theoretical knowledge, enough experience, reading a lot of code etc, do not worry too much about it. As advice that has served me (that does not mean that it serves you), is to return after a while to review old programs (when your experience increases) and try to improve them. Happy coding! – MARS Nov 23 '18 at 19:09

You must log in to answer this question.

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