Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 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 <stdlib.h>
  10. #include <stdint.h>
  11. #include <cs50.h>
  12. #include <stdio.h>
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16. // TODO
  17.  
  18.  
  19. FILE* infile = fopen("card.raw", "r");
  20.  
  21. if(infile == NULL)
  22. {
  23. printf("couldn't open file!");
  24. fclose(infile);
  25. return 1;
  26. }
  27. FILE* outfile = NULL;
  28. //variables
  29. uint8_t storage[512];
  30. int number = 0;
  31. char string[50]; // why 50??
  32.  
  33. //read bytes
  34.  
  35. while((fread(&storage, sizeof(uint8_t), 512, infile)) != 0 )
  36. {
  37.  
  38.  
  39. // find beginning of jpg
  40. if(storage[0] == 0xff && storage[1] == 0xd8 && storage[2] == 0xff && (storage[3] == 0xe0 || storage[3] == 0xe1))
  41. {
  42. if(outfile != NULL)
  43. {
  44. fclose(outfile);
  45. return 1;
  46. }
  47. //open new jpg
  48. sprintf(string, " %.03d.jpg", number);
  49. outfile = fopen(string, "w");
  50.  
  51. fwrite(&storage, sizeof(uint8_t), 512, outfile);
  52.  
  53. number++;
  54. }
  55. else if(number != 0)
  56. {
  57.  
  58. // write into this file
  59. fwrite(&storage, sizeof(uint8_t), 512, outfile);
  60.  
  61.  
  62. }
  63.  
  64. }
  65.  
  66. fclose(infile);
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement