Guest User

Untitled

a guest
Feb 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: recover.c
  5. *
  6. * Description: Program to recover jpegs from a memory card
  7. *
  8. * Version: 1.0
  9. * Created: 07/16/2011 07:47:12 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: me
  14. * Company:
  15. *
  16. * =====================================================================================
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdbool.h>
  22. #include "bmp.h"
  23. /*
  24. * === FUNCTION ======================================================================
  25. * Name: main
  26. * Description: gets all the pictures but has to be killed with Ctrl-C, dont know why
  27. * =====================================================================================
  28. */
  29. int
  30. main ( int argc, char *argv[] )
  31. {
  32. // prototype for our function
  33. bool isJpeg(BYTE buffer[]);
  34. FILE *make_file(int n);
  35.  
  36. // open file card.raw to read data
  37. FILE *fp = fopen("card.raw", "r");
  38. if(fp == NULL)
  39. {
  40. printf("Could not open file.\n");
  41. return 1;
  42. }
  43.  
  44.  
  45.  
  46. //int to hold filename value and string to
  47. int num = 0;
  48.  
  49. // keep track of whether we found a jpeg or not 0 for false 1 for true
  50. // int foundpic = 0;
  51. //allocate space for buffer to hold 512 byte blocks and next block
  52. BYTE buffer[512];
  53.  
  54.  
  55. //sequence through filenames and open files for writing
  56. while(!feof(fp) && !ferror(fp))
  57. {
  58.  
  59. // read into buffer
  60. fread(buffer, sizeof(BYTE), 512, fp);
  61.  
  62.  
  63. while(isJpeg(buffer))
  64. {
  65. // create outfile
  66. FILE *outfile = make_file(num);
  67.  
  68. // write out jpeg to outfile
  69. fwrite(buffer, sizeof(BYTE), 512, outfile);
  70. fread(buffer, sizeof(BYTE), 512, fp);
  71.  
  72. // while next block isnt the start of new jpeg keep writing
  73. while(!isJpeg(buffer))
  74. {
  75. fwrite(buffer, sizeof(BYTE), 512, outfile);
  76. fread(buffer, sizeof(BYTE), 512, fp);
  77. }
  78. fclose(outfile);
  79. num++;
  80. }
  81.  
  82. }
  83. fclose(fp);
  84. return EXIT_SUCCESS;
  85. } /* ---------- end of function main ---------- */
  86.  
  87. bool isJpeg(BYTE buffer[])
  88. {
  89. if((buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255 && buffer[3] == 224)||
  90. (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe1))
  91. {
  92. return true;
  93. }
  94. return false;
  95. }
  96. FILE *make_file(int n)
  97. {
  98. char filename[60];
  99. sprintf(filename, "0%i.jpg",n);
  100. FILE *out = fopen(filename, "w");
  101. if(out == NULL)
  102. {
  103. printf("Could not open %s for writing", filename);
  104. return NULL;
  105. }
  106. return out;
  107. }
Add Comment
Please, Sign In to add comment