Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <cs50.h>
  6.  
  7. bool check(FILE *fp);
  8.  
  9. typedef uint8_t BYTE;
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. int w = 000;
  14. if (argc == 2)
  15. {
  16. FILE *ptr = fopen(argv[1], "r");
  17. while(ftell(ptr) != EOF)
  18. {
  19. while(check(ptr) == 0)
  20. {
  21. char name[64];
  22. sprintf(name, "%i.jpeg", w);
  23. FILE *img = fopen(name, "w");
  24. while(check(ptr) == 0)
  25. {
  26. BYTE B[512];
  27. fread(B, 512, 1, ptr);
  28. fwrite(B, 512, 1, img);
  29. }
  30. w++;
  31. fclose(img);
  32.  
  33. }
  34. }
  35. fclose(ptr);
  36. }
  37. else
  38. {
  39. printf("Usage: ./recover image\n");
  40. return 1;
  41. }
  42.  
  43. }
  44.  
  45. bool check(FILE *fp)
  46. {
  47. BYTE b[4];
  48. fread (&b, 4, 1, fp);
  49. bool jpg = b[0] == 0xff && b[1] == 0xd8 && b[2] == 0xff && (b[3] == 0xe0 || b[3] == 0xe1 || b[3] == 0xe2 || b[3] == 0xe3 || b[3] == 0xe4 || b[3] == 0xe5 || b[3] == 0xe6 || b[3] == 0xe7 || b[3] == 0xe8 || b[3] == 0xe9 || b[3] == 0xea || b[3] == 0xeb || b[3] == 0xec || b[3] == 0xed || b[3] == 0xee || b[3] == 0xef);
  50. if (jpg == 0)
  51. {
  52. fseek(fp, -4L, SEEK_CUR);
  53. return 0;
  54. }
  55. else
  56. {
  57. fseek(fp, 508L, SEEK_CUR);
  58. return 1;
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement