Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * recover.c
- *
- * Computer Science 50
- * Problem Set 5
- *
- * recovers images from .raw file. if no arg is supplied, automatically opens
- * supplied cs50 file. if user enters a filename, uses it instead.
- ***************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdint.h>
- #define BLOCKSIZE 512;
- typedef uint8_t BYTE;
- bool byte_check(BYTE bytes[]);
- //bool in(BYTE bytes[]);
- int
- main(int argc, char *argv[])
- {
- FILE *inptr = fopen("card.raw", "r");
- if (inptr == NULL){
- printf("something went wrong with the file\n 35\n\n");
- return (2);
- }
- BYTE buffer[512]; //buffer to store bytes.
- int picNum = 1;
- char fileName[3];
- sprintf(fileName, "%.2d", picNum);
- // write the bytes to the buf
- // printf("writing to buffer....");
- for (int i = 0; i < 512; i++){
- if (feof(inptr)){// && ferror(inptr)){ //if we have reached the end of the file
- break;
- printf("end of file\n");
- }
- fwrite(&buffer[i], 1, 1, inptr);
- }
- //while we have not reached the end of the file
- while (!feof(inptr) || !ferror(inptr)){
- if (byte_check(buffer)){ //if bytes are the headers
- // open output file
- FILE *outptr = fopen(fileName, "w");
- printf("validated.... opening outfile.....\n");
- if (outptr == NULL){
- fclose(inptr);
- fprintf(stderr, "Could not create file %d.\n", picNum);
- return 3;
- }
- do{
- printf("writing out picture...\n");
- for (int i = 0; i < 512; i++) //write bytes into buffer
- fwrite(&buffer, 1, 1, inptr);
- for (int i = 0; i < 512; i++){
- if (feof(inptr) < 1) //if we have reached the end of the file
- break;
- fwrite(&buffer[i], 1, 1, outptr);
- }
- } while(byte_check(buffer) == false);
- fclose(outptr);
- picNum++;
- }
- // write the bytes to the buffer
- // printf("writing to buffer....");
- for (int i = 0; i < 512; i++){
- if (feof(inptr)){// && ferror(inptr)){ //if we have reached the end of the file
- break;
- printf("end of file\n");
- }
- fwrite(&buffer[i], 1, 1, inptr);
- }
- }
- // close infile
- fclose(inptr);
- // done
- return 0;
- }
- /*
- * takes in the jpeg formatted array and checks to see if it is
- * likely the jpeg head. returns true if it is, else returns false
- */
- bool
- byte_check(BYTE bytes[]){
- // 0xff = 255, 0xd8 = 216, 0xe0 = 224, 0xe1 = 225
- if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff &&
- (bytes[3] == 0xe0 || bytes[3] == 0xe1))
- return true;
- return false;
- }
- /*// checks to see if the signature exists in any part of the buffer*/
- /*bool in(BYTE bytes[]){*/
- /* for (int i = 0; i < 508; i++){*/
- /* if (bytes[i] == 0xff && bytes[i + 1] == 0xd8 && bytes[i + 2] == 0xff &&*/
- /* (bytes[i+ 3] == 0xe0 || bytes[i+ 3] == 0xe1))*/
- /* return true; */
- /* }*/
- /* return false;*/
- /*}*/
Add Comment
Please, Sign In to add comment