Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * recover.c
- *
- * (rl777)
- * 5/25/2016
- *
- * Computer Science 50
- * Problem Set 4 - Forensics/Recover
- *
- * Recovers JPEGs from a forensic image.
- */
- // https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output
- // https://www.branah.com/ascii-converter
- // Library includes
- //==================================
- #include <stdio.h>
- #include <string.h>
- #include <cs50.h>
- //Declarations
- //==================================
- int count;
- unsigned char buf[512]; //holds the 512 bytes of a jpg block
- int jpgCnt=-1; //counts the number of jpgs detected
- int blockCnt=0; //number of blocks processed, both empty and data.
- int picSize=0; //individual image size in blocks.
- bool processing=0; //used to determine if we are in the data section of the card
- char name[3];
- char prefix[11];
- FILE* fp; // this is the input file ptr. Pointing to card.raw
- /*
- long GetFileSize(const char* filename)
- {
- long size;
- FILE *f;
- f = fopen(filename, "r"); //open the file
- if (f == NULL) return -1; //
- fseek(f, 0, SEEK_END); //point to the end of the file
- size = ftell(f); //get the relative offset
- fclose(f); //close the file
- return size; //return the size of the file
- }
- */
- int main(int argc, char* argv[])
- {
- FILE* cardFile=fopen("card.raw","r"); //open the card file
- while (fread(buf, 512,1, cardFile) ==1)
- {
- if ( //see if this is the start of a jpg
- ((int)buf[0]==0xff) &&
- ((int)buf[1]==216) &&
- ((int)buf[2]==255) &&
- ( (int)buf[3]==224 || (int)buf[3]==225 )
- )
- {
- jpgCnt++;
- if (processing==0) //This is the first jpg encountered. No file to close.
- {processing=1;}
- else //This is not the first jpg encountered. Need to close open file
- {fclose(fp);
- }
- //in either case, a new jpg file is opened
- sprintf(name, "%03d", jpgCnt); // %i or %d?
- strcat(name,".jpg");
- strcpy(prefix,"../"); //compiles ok, but checker doesn't like this.
- //strcpy(prefix,""); //checker likes this.
- strcat(prefix,name);
- fp = fopen(prefix,"a"); //open the new jpg file
- fwrite(buf,512,1,fp); //fp is an "append" file. Pointer will change
- //automatically after write.
- picSize++;
- }
- else //Not first block of current jpg or no jpg yet
- {if (processing==1) //Append data to open jpg file
- {
- fwrite(buf,512,1,fp);
- picSize++;
- }
- }
- } //end of while loop, looping block-at-a-time through card file.
- // We have now exited the while loop. No more file processing.
- fclose(fp); // close the current (the last generated) image file.
- fclose(cardFile); // close the input file (card.raw))
- exit(0); // is this needed?
- } /* End main */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement