Advertisement
rl777

my code

May 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. /**
  2.  * recover.c
  3.  *
  4.  * (rl777)
  5.  * 5/25/2016
  6.  *
  7.  * Computer Science 50
  8.  * Problem Set 4 - Forensics/Recover
  9.  *
  10.  * Recovers JPEGs from a forensic image.
  11.  */
  12.  
  13. // https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output
  14. // https://www.branah.com/ascii-converter
  15.  
  16.  
  17.  
  18.  
  19. // Library includes
  20. //==================================
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <cs50.h>
  24.  
  25.  
  26. //Declarations
  27. //==================================
  28.  
  29. int count;
  30. unsigned char buf[512]; //holds the 512 bytes of a jpg block
  31. int jpgCnt=-1;          //counts the number of jpgs detected
  32. int blockCnt=0;         //number of blocks processed, both empty and data.
  33. int picSize=0;          //individual image size in blocks.
  34. bool processing=0; //used to determine if we are in the data section of the card
  35. char name[3];
  36. char prefix[11];
  37. FILE* fp;          // this is the input file ptr. Pointing to card.raw
  38.  
  39.  
  40. /*
  41.  
  42. long GetFileSize(const char* filename)
  43. {
  44.     long size;
  45.     FILE *f;
  46.  
  47.     f = fopen(filename, "r");  //open the file
  48.     if (f == NULL) return -1;  //
  49.     fseek(f, 0, SEEK_END);     //point to the end of the file
  50.     size = ftell(f);           //get the relative offset
  51.     fclose(f);                 //close the file
  52.  
  53.     return size;               //return the size of the file
  54. }
  55.  
  56. */
  57.  
  58. int main(int argc, char* argv[])
  59. {
  60.     FILE* cardFile=fopen("card.raw","r");  //open the card file
  61.  
  62.     while (fread(buf, 512,1, cardFile) ==1)
  63.     {
  64.         if (  //see if this is the start of a jpg  
  65.             ((int)buf[0]==0xff) &&
  66.             ((int)buf[1]==216) &&
  67.             ((int)buf[2]==255) &&
  68.             (  (int)buf[3]==224 || (int)buf[3]==225   )
  69.             )
  70.         {
  71.             jpgCnt++;
  72.    
  73.             if (processing==0)   //This is the first jpg encountered. No file to close.
  74.                 {processing=1;}
  75.             else  //This is not the first jpg encountered. Need to close open file
  76.                 {fclose(fp);
  77.                 }
  78.            
  79.             //in either case, a new jpg file is opened 
  80.             sprintf(name, "%03d", jpgCnt);   // %i or %d?
  81.             strcat(name,".jpg");
  82.             strcpy(prefix,"../");   //compiles ok, but checker doesn't like this.
  83.             //strcpy(prefix,"");  //checker likes this.
  84.             strcat(prefix,name);
  85.             fp = fopen(prefix,"a"); //open the new jpg file
  86.             fwrite(buf,512,1,fp);  //fp is an "append" file. Pointer will change
  87.                                    //automatically after write.
  88.             picSize++;
  89.         }
  90.    
  91.         else  //Not first block of current jpg or no jpg yet      
  92.             {if (processing==1) //Append data to open jpg file
  93.                 {
  94.                    fwrite(buf,512,1,fp);
  95.                    picSize++;
  96.                 }            
  97.             }
  98.     } //end of while loop, looping block-at-a-time through card file.
  99.    
  100.     // We have now exited the while loop. No more file processing.
  101.  
  102.     fclose(fp);           // close the current (the last generated) image file.
  103.     fclose(cardFile);     // close the input file (card.raw))
  104.     exit(0);              // is this needed?
  105.  
  106. }   /* End main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement