Guest User

Untitled

a guest
May 21st, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdint.h>
  4. #include<string.h>
  5.  
  6. typedef struct
  7. {
  8. uint8_t one;
  9. uint8_t two;
  10. uint8_t three;
  11. uint8_t four;
  12. }header;
  13.  
  14. typedef struct
  15. {
  16. char image[512];
  17. }fat;
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. // ensure proper usage
  22. if (argc != 2)
  23. {
  24. fprintf(stderr, "Usage: ./copy infile\n");
  25. return 1;
  26. }
  27. // remember filenames
  28. char *infile = argv[1];
  29. // open input file
  30. FILE *inp = fopen(infile, "r");
  31. if (inp == NULL)
  32. {
  33. fclose(inp);
  34. fprintf(stderr, "Doesnt exis %s.\n", infile);
  35. return 3;
  36. }
  37. header a = {0xff,0xd8,0xff,0xe0};
  38. uint8_t *b = malloc(sizeof(header));
  39. uint8_t byte=0;
  40. fat img;
  41. char title[100];
  42. int i=0,c = 1, jpg_found=0;
  43. while(c==1)
  44. {
  45. if(feof(inp))
  46. {
  47. break;
  48. }
  49. fread(&byte,1,1,inp);
  50. if(!memcmp(&byte,&a,1))
  51. {
  52. fseek(inp,-1,SEEK_CUR);
  53. fread(b,sizeof(header),1,inp);
  54. *(b+3*sizeof(uint8_t)) = *(b+3*sizeof(uint8_t)) & 0xe0;
  55. if(!memcmp(&a,b,sizeof(header)))
  56. {
  57. sprintf(title,"%03d.jpg",i++);
  58. FILE *out = fopen(title, "w");
  59. if (out == NULL)
  60. {
  61. fclose(out);
  62. fprintf(stderr, "Error");
  63. return 3;
  64. }
  65. fseek(inp,-4,SEEK_CUR);
  66. memset(&img,0,sizeof(fat));
  67. while(1)
  68. {
  69. if(feof(inp))
  70. {
  71. break;
  72. }
  73. c=fread(&img,sizeof(fat),1,inp);
  74. if(memcmp(&img,&a,sizeof(header)) || jpg_found==0)
  75. {
  76. c=fwrite(&img,sizeof(fat),1,out);
  77. jpg_found++;
  78. }
  79. else
  80. {
  81. fseek(inp, -512, SEEK_CUR);
  82. break;
  83. }
  84. }
  85. fclose(out);
  86. jpg_found = 0;
  87. }
  88.  
  89. }
  90. }
  91. fclose(inp);
  92. free(b);
  93. }
Add Comment
Please, Sign In to add comment