Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 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. * open memory card file
  10. * find beginning of jpg
  11. * open a new jpg
  12. * write 512 bytes until new jpg is found
  13. * detect end of file
  14. */
  15. #include <stdio.h>
  16. #include <stdint.h>
  17.  
  18. typedef uint8_t BYTE;
  19.  
  20. int main(void)
  21. {
  22. // open card file
  23. FILE* inptr = fopen("card.raw", "r");
  24.  
  25. // check if file can be opened, if not, show error message
  26. if (inptr == NULL)
  27. {
  28. printf("Could not open card.raw.\n");
  29. return 1;
  30. }
  31.  
  32. // counter for number of jpg files recovered
  33. int counter = 0;
  34.  
  35. // create a buffer of 512 bytes
  36. BYTE buffer[512];
  37.  
  38. // declare jpg file name, 3 digit number, starting from 000
  39. char filename[8];
  40.  
  41. // temporary file output
  42. FILE* outptr = NULL;
  43.  
  44. // repeat until end of card
  45. while ((fread(buffer, 512, 1, inptr)) == 1)
  46. {
  47. // detect jpg file by checking first four bytes
  48. if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] >= 0xe0 && buffer[3] <= 0xef))
  49. {
  50. // if temp already exists, fclose temp (start of new jpg file and end of previous jpg file, hence close old file and open new file)
  51. if (outptr != NULL)
  52. {
  53. fclose(outptr);
  54. }
  55. // title of jpg files
  56. sprintf(filename, "%03d.jpg", counter);
  57.  
  58. // open temp file
  59. outptr = fopen(filename, "w");
  60.  
  61. counter++;
  62.  
  63. // write data to temp file
  64. fwrite(buffer, 512, 1, outptr);
  65. }
  66.  
  67. // else if buffer conditions above not met and counter > 0
  68. else if (counter > 0)
  69. {
  70. // continue to fwrite to temp file (start of jpg file already written, continue to write to file currently opened)
  71. fwrite(buffer, 512, 1, outptr);
  72. }
  73.  
  74. // continue to fread (cos if buffer condition not met + counter = 0, means havent hit jpg file yet, so keep reading)
  75. else
  76. fread(buffer, 512, 1, inptr);
  77. }
  78.  
  79. // close remaining files
  80. fclose(inptr);
  81. fclose(outptr);
  82.  
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement