ketan18710

RECOVER.C

Jun 14th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. int main(int argc, char *argv[])
  5. { int n=0;
  6. // ensure proper usage
  7. if (argc != 2)
  8. {
  9. fprintf(stderr, "Usage: ./copy infile outfile\n");
  10. return 1;
  11. }
  12. char *infile = argv[1];
  13. // open input file
  14. FILE *inptr = fopen(infile, "r");
  15. if (inptr == NULL)
  16. {
  17. fprintf(stderr, "Could not open %s.\n", infile);
  18. return 2;
  19. }
  20. int flag =0;
  21. uint8_t *buffer = malloc(512*sizeof(uint8_t));
  22. FILE *img;
  23. char *a = malloc(sizeof(uint8_t)); ;
  24. while (fread(buffer,1,512,inptr)==512)
  25. {
  26. if(((*buffer)==0xff&&(*buffer+1)==0xd8&&(*buffer+2)==0xff&&((*buffer+3) & 0xf0)==0xe0)&&flag==0)
  27. {
  28.  
  29. sprintf(a,"%03i.jpg",n);
  30. n++;
  31. flag =1;
  32. img = fopen(a,"w");
  33. for(int x=0;x<512;x++)
  34. {
  35. fwrite(buffer,1,1,img);
  36. }
  37.  
  38. if(((*buffer)==0xff&&(*buffer+1)==0xd8&&(*buffer+2)==0xff&&((*buffer+3) & 0xf0)==0xe0)&&flag==1)
  39. {
  40. fclose(img);
  41. sprintf(a,"%03i.jpg",n);
  42. n++;
  43. flag =1;
  44. FILE *img = fopen(a,"w");
  45. fwrite(buffer,512,1,img);
  46. }
  47. }
  48. else if(flag==1)
  49. {
  50. fwrite(buffer,512,1,img);
  51. }
  52. else
  53. break;
  54.  
  55.  
  56.  
  57. }
  58. fclose(inptr);
  59. }
  60. OUTPUT
  61. $ make recover
  62. clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow recover.c -lcrypt -lcs50 -lm -o recover
  63. recover.c:44:23: error: declaration shadows a local variable [-Werror,-Wshadow]
  64. FILE *img = fopen(a,"w");
  65. ^
  66. recover.c:22:12: note: previous declaration is here
  67. FILE *img;
  68. ^
  69. 1 error generated.
  70. make: *** [recover] Error 1
  71. ~/workspace/recover/ $
Add Comment
Please, Sign In to add comment