Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Make a program to copy all JPEG images of memory card to recover all thoses deleted images
- by looking for JPEG signature.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- int main(int argc, char *argv[])
- {
- // one command-line argument
- if (argc != 2)
- {
- fprintf(stderr, "Usage: enter name of forensic image\n");
- return 1;
- }
- //name of the memroy card file
- char *card_raw = argv[1];
- // open memory card file that is the input // cardfile is a file and card_raw is the name of the file
- FILE *cardfile = fopen(card_raw, "r");
- if (cardfile == NULL)
- {
- fprintf(stderr, "Could not open %s\n", card_raw);
- return 2;
- }
- //define data structure for having exactly 1 byte
- typedef uint8_t BYTE;
- //to store bytes of the size of an array = 512 =block size long (size of an array of 512 which is 1 byte long) => store memory which bunch of 512 bytes called buffer
- BYTE buffer[512];
- //count for number of JPEG file - should get total of 50 with raw
- int count = 0;
- FILE *image = NULL;
- //read the memory card until end of file (end of memory card) so no more 512 block left, where size of block is 512 from FAT
- while (fread(buffer, 512, 1, cardfile) == 1)
- {
- //check the first 4 bytes pattern to find the JPEG signature (fourth byte 0Xe (0 à 7 ou a à f))
- if (buffer [0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] == 0xe0 || buffer[3] == 0xe1))
- {
- //if an outfile is open, close it to than start a new file
- if (count > 0)
- {
- fclose(image);
- }
- //if JPEG signature start a new file so create the file to be opened to be able to copy the JPEG image from memory card
- //create name of the file with char for jpeg filames with 3 digits => 000.jpg (7 caracters) + "\ 0" (+1 caracter) = total of 8caracters
- char filename[8];
- //create a name for each file
- sprintf(filename,"%03i.jpg", count);
- //increase number of filename for next jpg found => 000.jpg, 001.jpg. 002.jpg etc...
- //open the file
- image = fopen(filename, "w");
- count ++;
- if (image == NULL);
- {
- fprintf(stderr, "Could not open new file\n");
- return 3;
- }
- //write the new file so copy the memory card ofr one image at a time
- fwrite(buffer, 512, 1, image);
- }
- // write image by image // write the 0 as 0 if blank space
- //if ( size != 1) //stop reading because it s
- // fin c'est le début d'un autre JPEG
- //if no JPEG signature, just copy on the file until closed or end of file (when found a new signature file will be closed)
- if (count > 0)
- {
- //write all the blocks after the JPEG signature
- fwrite(buffer, 512, 1, image);
- }
- }
- // close outfile each one at the time ( when encounter another signature)
- fclose(image);
- // close memory file
- fclose(cardfile);
- // success
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement