Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * =====================================================================================
- *
- * Filename: recover.c
- *
- * Description: Program to recover jpegs from a memory card
- *
- * Version: 1.0
- * Created: 07/16/2011 07:47:12 PM
- * Revision: none
- * Compiler: gcc
- *
- * Author: me
- * Company:
- *
- * =====================================================================================
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include "bmp.h"
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description: gets all the pictures but has to be killed with Ctrl-C, dont know why
- * =====================================================================================
- */
- int
- main ( int argc, char *argv[] )
- {
- // prototype for our function
- bool isJpeg(BYTE buffer[]);
- FILE *make_file(int n);
- // open file card.raw to read data
- FILE *fp = fopen("card.raw", "r");
- if(fp == NULL)
- {
- printf("Could not open file.\n");
- return 1;
- }
- //int to hold filename value and string to
- int num = 0;
- // keep track of whether we found a jpeg or not 0 for false 1 for true
- // int foundpic = 0;
- //allocate space for buffer to hold 512 byte blocks and next block
- BYTE buffer[512];
- //sequence through filenames and open files for writing
- while(!feof(fp) && !ferror(fp))
- {
- // read into buffer
- fread(buffer, sizeof(BYTE), 512, fp);
- while(isJpeg(buffer))
- {
- // create outfile
- FILE *outfile = make_file(num);
- // write out jpeg to outfile
- fwrite(buffer, sizeof(BYTE), 512, outfile);
- fread(buffer, sizeof(BYTE), 512, fp);
- // while next block isnt the start of new jpeg keep writing
- while(!isJpeg(buffer))
- {
- fwrite(buffer, sizeof(BYTE), 512, outfile);
- fread(buffer, sizeof(BYTE), 512, fp);
- }
- fclose(outfile);
- num++;
- }
- }
- fclose(fp);
- return EXIT_SUCCESS;
- } /* ---------- end of function main ---------- */
- bool isJpeg(BYTE buffer[])
- {
- if((buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255 && buffer[3] == 224)||
- (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe1))
- {
- return true;
- }
- return false;
- }
- FILE *make_file(int n)
- {
- char filename[60];
- sprintf(filename, "0%i.jpg",n);
- FILE *out = fopen(filename, "w");
- if(out == NULL)
- {
- printf("Could not open %s for writing", filename);
- return NULL;
- }
- return out;
- }
Add Comment
Please, Sign In to add comment