Advertisement
tkamiten

recover.c

Mar 17th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /**
  2. * recover.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 4
  6. *
  7. * Recovers JPEGs from a forensic image.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12.  
  13. typedef uint8_t BYTE;
  14.  
  15. int main(void)
  16. {
  17. // open CF card
  18. FILE* inptr = fopen("card.raw", "r");
  19. if (inptr == NULL)
  20. {
  21. printf("Could not open \n");
  22. return 1;
  23. }
  24.  
  25.  
  26.  
  27. int j = 0; // counter for jpeg file
  28. char title[8]; // file name for jpeg file
  29. BYTE block[512]; // 512byte chunk of memory
  30.  
  31.  
  32. while(!feof(inptr))
  33. {
  34.  
  35. fread(block, sizeof(block), 1, inptr);
  36.  
  37. if(block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] >= 0xe0 && block[3] <= 0xef))
  38. {
  39.  
  40. sprintf(title, "%03d.jpg", j);
  41. j++;
  42.  
  43. FILE* outptr = fopen(title,"a");
  44.  
  45. do
  46. {
  47. fwrite(block, sizeof(block), 1, outptr);
  48. fread(block, sizeof(block), 1, inptr);
  49. } while(block[0] != 0xff || block[1] != 0xd8 || block[2] != 0xff || (block[3] < 0xe0 || block[3] > 0xef));
  50.  
  51. fclose(outptr);
  52.  
  53. }
  54.  
  55. }
  56. fclose(inptr);
  57.  
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement