Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * recover.c
- *
- * Computer Science 50
- * Problem Set 4
- *
- * Recovers JPEGs from a forensic image.
- */
- #include <stdlib.h>
- #include <stdint.h>
- #include <cs50.h>
- #include <stdio.h>
- int main(int argc, char* argv[])
- {
- // TODO
- FILE* infile = fopen("card.raw", "r");
- if(infile == NULL)
- {
- printf("couldn't open file!");
- fclose(infile);
- return 1;
- }
- FILE* outfile = NULL;
- //variables
- uint8_t storage[512];
- int number = 0;
- char string[50]; // why 50??
- //read bytes
- while((fread(&storage, sizeof(uint8_t), 512, infile)) != 0 )
- {
- // find beginning of jpg
- if(storage[0] == 0xff && storage[1] == 0xd8 && storage[2] == 0xff && (storage[3] == 0xe0 || storage[3] == 0xe1))
- {
- if(outfile != NULL)
- {
- fclose(outfile);
- return 1;
- }
- //open new jpg
- sprintf(string, " %.03d.jpg", number);
- outfile = fopen(string, "w");
- fwrite(&storage, sizeof(uint8_t), 512, outfile);
- number++;
- }
- else if(number != 0)
- {
- // write into this file
- fwrite(&storage, sizeof(uint8_t), 512, outfile);
- }
- }
- fclose(infile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement