0

enter image description here

Here is my code:

include

include

include

include

int main(int argc, string argv[]) { // ensure proper usage if (argc != 2) { fprintf(stderr, "Usage: ./recover infile\n"); return 1; }

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

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

//initialize buffer and other values
int count = 0;
uint8_t buffer[512];
uint8_t test[4];
bool jpeg_start = false;
FILE* img = NULL;

while(fread(buffer,512,1,inptr) == 1)
{
    for(int i = 0; i<4; i++)
    {
        test[i] = buffer[i];
    }

    // tests if start of jpeg
    if(test[0] == 0xff &&
       test[1] == 0xd8 &&
       test[2] == 0xff &&
       test[3] >= 0xe0 && 
       test[3] <= 0xef)
     {
        //declare files
        char filename[8];
        sprintf(filename, "%03i.jpg", count);

        // jpeg already started but not at the start
        if(jpeg_start == false)
        {
            img = fopen(filename,"w");
            fwrite(buffer,512,1,img);
            jpeg_start = true;
        }
        // jpeg must be ending
        // close are start writing a new one
        else
        {
            fclose(img);
            img = fopen(filename,"w");
            fwrite(buffer,512,1,img);
            count++;
        }
     }
     // continue writing if we already found JPEG
     else
     {
        if(jpeg_start == true)
        {
            fwrite(buffer,512,1,img);    
        }
    }
}
// close infile
fclose(inptr);
return 0;

}

0

Run your code with card.raw and look how many .jpg you recover. One is missing.

Two hints:

  1. It is the first .jpg

  2. Look closely at count and filenaming.

| improve this answer | |
  • Wow. Thanks for the hint works perfect now. ...its always the simple stuff :(. – noobcoder Jul 11 '17 at 18:53

You must log in to answer this question.

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