ketan18710

RECOVER

Apr 3rd, 2017
202
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. int main(int argc, char *argv[])
  5. { int n=0;
  6. // ensure proper usage
  7. if (argc != 2)
  8. {
  9. fprintf(stderr, "Usage: ./copy infile outfile\n");
  10. return 1;
  11. }
  12. char *infile = argv[1];
  13. // open input file
  14. FILE *inptr = fopen(infile, "r");
  15. if (inptr == NULL)
  16. {
  17. fprintf(stderr, "Could not open %s.\n", infile);
  18. return 2;
  19. }
  20. int flag =0;
  21. uint8_t *buffer = malloc(512*sizeof(uint8_t));
  22. FILE *img;
  23.  
  24. while (fread(buffer,1,512,inptr)==512)
  25. {
  26. if(((*buffer)==0xff&&(*buffer+1)==0xd8&&(*buffer+2)==0xff&&((*buffer+3) & 0xf0)==0xe0)&&flag==0)
  27. {
  28. char *a = malloc(sizeof(uint8_t));
  29. sprintf(a,"%03i.jpg",n);
  30. n++;
  31. flag =1;
  32. img = fopen(a,"w");
  33. for(int j=0;j<512;j++)
  34. {
  35. fwrite(buffer,1,1,img);
  36. }
  37. }
  38. else if(((*buffer)==0xff&&(*buffer+1)==0xd8&&(*buffer+2)==0xff&&((*buffer+3) & 0xf0)==0xe0)&&flag==1)
  39. {
  40. fclose(img);
  41. char *a= malloc(sizeof(uint8_t));
  42. sprintf(a,"%03i.jpg",n);
  43. n++;
  44. flag =1;
  45. img = fopen(a,"w");
  46. for(int j=0;j<512;j++)
  47. {
  48. fwrite(buffer,1,1,img);
  49. }
  50. }
  51. else if(flag==1)
  52. {
  53. fwrite(buffer,512,1,img);
  54. }
  55.  
  56.  
  57.  
  58. }
  59. fclose(inptr);
  60. }
  61.  
  62. OUTPUT:
  63. ./recover card.raw
  64. ~/workspace/recover/ $ check50 2016.recover recover.c
  65. :) recover.c exists
  66. :) recover.c compiles
  67. :) handles lack of forensic image
  68. :( recovers 000.jpg correctly
  69. \ expected 000.jpg to exist
  70. :( recovers middle files correctly
  71. \ expected 001.jpg to exist
  72. :( recovers last file correctly
  73. \ expected 015.jpg to exist
  74. https://sandbox.cs50.net/checks/8fa72dca95bd4da492ad1dd76c425181
RAW Paste Data