0

Well, this is my third post regarding the same pset:recover.c, but I have made some improvements: I have recovered 50 images starting from 000.jpg until 049.jpg which were not shown at all in my previous code (having segmentation fault). But new issue comes out as below:

$ ./recover card.raw 
$ check50 cs50/2017/x/recover
:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
recovered image does not match
:( recovers middle images correctly
recovered image does not match
:( recovers 015.jpg correctly
recovered image does not match

Here is my edited code based on previous comments:

#include <stdio.h>
#include <stdlib.h> // malloc()
#include <stdint.h>

int main(int argc, char *argv[])
{

  // ensure proper usage
  if (argc != 2)
  {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
  }

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

  typedef uint8_t BYTE;//unsigned integer data type
  BYTE buffer[512];
  int i = 0;// filename sequential number
  char filename[8];
  FILE *img = NULL;

  while(fread(buffer, 512, 1, inptr)==1)
  {
     if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
     {
           if(img != NULL)
           {
                 fclose(img);
           }
           sprintf(filename, "%03i.jpg", i);
           img = fopen(filename, "w");
           i++;
     }
     else if(img != NULL)
     {
          fwrite(buffer, 512, 1, img);
     }
  }

  // close infile
  fclose(inptr);

  //close outfile .jpg
  if (img != NULL)
  {
        fclose(img);
  }

  //finish while loop
  return 0;
}

Actually, I don't understand the meaning of "recovered image does not match"? Another point, all my recovered .jpg do not show any image when preview it in CS50 IDE.

8
  • I'm guessing that you tried to look at the images in the IDE but got an unsupported image error. That means that there's something wrong with the images. The good news is that you got files now. So the next step is to figure out why the images are bad. Have you looked at the beginning of the image files with xxd?
    – Cliff B
    Sep 9 '17 at 9:30
  • @CliffB xxd? I do have a lot to learn.
    – juanli
    Sep 9 '17 at 9:35
  • You should remember xxd from pset4/resize. What should be at the beginning of each image file?
    – Cliff B
    Sep 9 '17 at 9:52
  • @CliffB $ xxd -ps -l 10 000.jpg 936340a89eb59b5427a9 I have no clue of these sequential numbers.
    – juanli
    Sep 9 '17 at 10:23
  • Where's the signature?
    – Cliff B
    Sep 9 '17 at 21:36

You must log in to answer this question.

Browse other questions tagged .