Advertisement
rupek1995

Untitled

Mar 7th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 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 header_is_jpeg(FILE *memcard);
  8. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter);
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. // check proper usage
  13. if (argc != 2)
  14. {
  15. fprintf(stderr, "usage: ./recover filename\n");
  16. return 1;
  17. }
  18.  
  19. // keep filenames
  20. char *infile = argv[1],
  21. *outfile = NULL;
  22. FILE *inptr = fopen(infile, "r"),
  23. *outptr = NULL;
  24. BYTE *byte_array = malloc(512 * sizeof(BYTE));
  25. int filename_counter = 0;
  26.  
  27. // check if files open properly
  28. if (inptr == NULL)
  29. {
  30. free(byte_array);
  31. free(outfile);
  32. fprintf(stderr, "Could not open %s\n", infile);
  33. return 2;
  34. }
  35.  
  36. // skip memory blocks until jpeg header is found
  37. while (!header_is_jpeg(inptr))
  38. {
  39. fseek(inptr, 512, SEEK_CUR);
  40. }
  41.  
  42. if (header_is_jpeg(inptr))
  43. {
  44. create_new_outfile(&outfile, &outptr, &filename_counter);
  45. }
  46.  
  47. printf("new file created : %s\n", outfile);
  48.  
  49. if (outptr == NULL)
  50. {
  51. free(byte_array);
  52. free(outfile);
  53. fprintf(stderr, "Could not write in %s file\n", outfile);
  54. return 3;
  55. }
  56.  
  57. // TODO fix fwrite() to an array
  58. // only keep reading if block is proper size (512)
  59. while (fread(outptr, 1, 512, inptr) == 512)
  60. {
  61. // rewind seek
  62. fseek(inptr, -512, SEEK_CUR);
  63.  
  64. // read and write 512 bytes, 1 byte at a time
  65. for(int i = 0, arr_size = sizeof(byte_array); i < arr_size; i++)
  66. {
  67. fread(&byte_array[i], 1, 1, inptr);
  68. fwrite(&byte_array[i], 1, 1, outptr);
  69. }
  70.  
  71. // if next block has a new jpeg header - create new file
  72. if (header_is_jpeg(inptr))
  73. {
  74. create_new_outfile(&outfile, &outptr, &filename_counter);
  75. }
  76. }
  77.  
  78. // close files
  79. free(byte_array);
  80. free(outfile);
  81. fclose(inptr);
  82. fclose(outptr);
  83. return 0;
  84. }
  85.  
  86.  
  87. int header_is_jpeg(FILE *memcard)
  88. {
  89. BYTE *BLOCKHEADER = malloc(4 * sizeof(BYTE));
  90. // read first 4 bytes of current memory block
  91. for (int i = 0; i < 4; i++)
  92. {
  93. fread(&BLOCKHEADER[i], 1, 1, memcard);
  94. }
  95.  
  96. // rewind seek back to beginning of block
  97. fseek(memcard, -4, SEEK_CUR);
  98.  
  99. // check if header is the same as JPEG header, return true if it is
  100. if (BLOCKHEADER[0] == 0xff &&
  101. BLOCKHEADER[1] == 0xd8 &&
  102. BLOCKHEADER[2] == 0xff &&
  103. (BLOCKHEADER[3] & 0xf0) == 0xe0)
  104. {
  105. printf("JPEG Header found!\n");
  106. free(BLOCKHEADER);
  107. return 1;
  108. }
  109. else
  110. {
  111. free(BLOCKHEADER);
  112. return 0;
  113. }
  114. }
  115.  
  116. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter)
  117. {
  118. // if a file is open - close the old one before creating new one
  119. if (*outptr != NULL)
  120. {
  121. printf("successfuly recovered image: %s\n", *outfile);
  122. fclose(*outptr);
  123. free(*outfile);
  124. }
  125.  
  126. // create new output file
  127. *outfile = malloc(7 * sizeof(char)+ 1);
  128. sprintf(*outfile, "%03i.jpg", *filename_counter);
  129. *outptr = fopen(*outfile, "w");
  130. (*filename_counter)++;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement