Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. typedef uint8_t BYTE;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. FILE *mem = NULL;
  10. FILE *file;
  11.  
  12. if (argc != 2)
  13. {
  14. printf("Usage: ./recover image\n");
  15. return 1;
  16. }
  17.  
  18. file = fopen(argv[1] , "r");
  19. if (!file)
  20. {
  21. return 1;
  22. }
  23. int count = 0;
  24. char img[8];
  25. int i = 1;
  26. BYTE bytes[512];
  27. // This reads the file front to back
  28. while (fread(bytes, 512, 1, file))
  29. {
  30. // we check for a Jpeg
  31. if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
  32. {
  33. // we check what number has the jpeg file
  34. if(count == 0)
  35. {
  36. // we print in a string
  37. sprintf(img, "%03i.jpg", count);
  38. // create a file
  39. mem = fopen(img , "w");
  40. //we write the bytes from bytes to the actual file
  41. fwrite(bytes, 512, 1, mem);
  42. //we start counting the jpg files
  43. count++;
  44. }
  45. // If this is another jpeg file
  46. else
  47. {
  48. fclose(mem);
  49. sprintf(img, "%03i.jpg", count);
  50. mem = fopen(img , "w");
  51. fwrite(bytes, 512, 1, mem);
  52. count++;
  53. }
  54. i = 1;
  55. }
  56. else
  57. {
  58. // if we already found a jpeg we continue to write to it.
  59. if(mem != NULL)
  60. {
  61. fwrite(bytes, 512, 1, mem);
  62. }
  63. }
  64. }
  65. fclose(mem);
  66. fclose(file);
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement