Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- int main(int argc, char *argv[])
- { int n=0;
- // ensure proper usage
- if (argc != 2)
- {
- fprintf(stderr, "Usage: ./copy infile outfile\n");
- return 1;
- }
- char *infile = argv[1];
- // open input file
- FILE *inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- fprintf(stderr, "Could not open %s.\n", infile);
- return 2;
- }
- int flag =0;
- uint8_t *buffer = malloc(512*sizeof(uint8_t));
- FILE *img;
- while (fread(buffer,1,512,inptr)==512)
- {
- if(((*buffer)==0xff&&(*buffer+1)==0xd8&&(*buffer+2)==0xff&&((*buffer+3) & 0xf0)==0xe0)&&flag==0)
- {
- char *a = malloc(sizeof(uint8_t));
- sprintf(a,"%03i.jpg",n);
- n++;
- flag =1;
- img = fopen(a,"w");
- for(int j=0;j<512;j++)
- {
- fwrite(buffer,1,1,img);
- }
- }
- else if(((*buffer)==0xff&&(*buffer+1)==0xd8&&(*buffer+2)==0xff&&((*buffer+3) & 0xf0)==0xe0)&&flag==1)
- {
- fclose(img);
- char *a= malloc(sizeof(uint8_t));
- sprintf(a,"%03i.jpg",n);
- n++;
- flag =1;
- img = fopen(a,"w");
- for(int j=0;j<512;j++)
- {
- fwrite(buffer,1,1,img);
- }
- }
- else if(flag==1)
- {
- fwrite(buffer,512,1,img);
- }
- }
- fclose(inptr);
- }
- OUTPUT:
- ./recover card.raw
- ~/workspace/recover/ $ check50 2016.recover recover.c
- :) recover.c exists
- :) recover.c compiles
- :) handles lack of forensic image
- :( recovers 000.jpg correctly
- \ expected 000.jpg to exist
- :( recovers middle files correctly
- \ expected 001.jpg to exist
- :( recovers last file correctly
- \ expected 015.jpg to exist
- https://sandbox.cs50.net/checks/8fa72dca95bd4da492ad1dd76c425181
Add Comment
Please, Sign In to add comment