Guest User

Untitled

a guest
Jul 26th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 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 <cs50.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15. // TODO
  16.  
  17. // Open file
  18. FILE* inptr = fopen("card.raw", "r");
  19.  
  20. // Check if the file exists and can be opened
  21. if (inptr == NULL)
  22. {
  23. printf("Could not open 'card.raw'.\n");
  24. return 1;
  25. }
  26.  
  27. // Declare the buffer where the block of 512 bytes will be stored
  28. unsigned char buffer[512];
  29.  
  30. // Declare and intialize counter for file
  31. int fcount = 0;
  32.  
  33. // Initialize a variable for file name
  34. char fname[8];
  35.  
  36. // Declare file name temporary variable
  37. FILE* temp = NULL;
  38.  
  39.  
  40.  
  41. while (fread(&buffer, sizeof(buffer), 1, inptr)!= 0) // fread to check end of file
  42. {
  43. fread(&buffer, sizeof(buffer), 1, inptr);
  44.  
  45. while (fread(&buffer, sizeof(buffer), 1, inptr)) // read 512 bytes into buffer
  46. {
  47.  
  48.  
  49. if ( buffer[0] == 0xff &&
  50. buffer[1] == 0xd8 &&
  51. buffer[2] == 0xff &&
  52. (buffer[3] >= 0xe0 || buffer[3] <= 0xef) ) /*block starts with jpeg header*/ //
  53.  
  54. {
  55.  
  56.  
  57.  
  58. // generate file name
  59. sprintf( fname, "%03d.jpg",fcount );
  60. fcount++; // increment file number
  61.  
  62. if (temp != NULL)
  63. {
  64. fclose(temp); // close file
  65. }
  66.  
  67. // file = Open a new JPEG file
  68. temp = fopen(fname, "w");
  69.  
  70.  
  71. }
  72. if (fcount != 0)
  73. {
  74. fwrite(buffer, sizeof(buffer), 1, temp); // now write that block to file , I assume it will keep writing till next jpg
  75.  
  76. }
  77.  
  78.  
  79.  
  80. if (temp != NULL)
  81. {
  82. fwrite(buffer, sizeof(buffer), 1, temp);
  83. }
  84.  
  85.  
  86. }
  87.  
  88.  
  89.  
  90. } // 1st while loop
  91.  
  92. if (inptr != 0) /*card file is open*/
  93. {
  94. fclose(inptr); // Close it
  95. }
  96.  
  97.  
  98. }
Add Comment
Please, Sign In to add comment