Guest User

Untitled

a guest
May 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. /****************************************************************************
  2.  * recover.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * recovers images from .raw file. if no arg is supplied, automatically opens
  8.  * supplied cs50 file. if user enters a filename, uses it instead.
  9.  ***************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15.  
  16. #define BLOCKSIZE 512;
  17.  
  18. typedef uint8_t BYTE;
  19.  
  20. bool byte_check(BYTE bytes[]);
  21. //bool in(BYTE bytes[]);
  22.            
  23.  
  24. int
  25. main(int argc, char *argv[])
  26. {
  27.     FILE *inptr = fopen("card.raw", "r");
  28.    
  29.     if (inptr == NULL){
  30.         printf("something went wrong with the file\n 35\n\n");
  31.         return (2);
  32.     }
  33.     BYTE buffer[512]; //buffer to store bytes.
  34.    
  35.     int picNum = 1;
  36.     char fileName[3];
  37.     sprintf(fileName, "%.2d", picNum);    
  38.  
  39.     // write the bytes to the buf
  40. //    printf("writing to buffer....");
  41.     for (int i = 0; i < 512; i++){    
  42.         if (feof(inptr)){// && ferror(inptr)){ //if we have reached the end of the file
  43.             break;
  44.             printf("end of file\n");
  45.         }
  46.         fwrite(&buffer[i], 1, 1, inptr);
  47.     }
  48.  
  49.     //while we have not reached the end of the file
  50.     while (!feof(inptr) || !ferror(inptr)){
  51.    
  52.         if (byte_check(buffer)){ //if bytes are the headers
  53.             // open output file
  54.             FILE *outptr = fopen(fileName, "w");    
  55.             printf("validated.... opening outfile.....\n");
  56.            
  57.             if (outptr == NULL){
  58.                 fclose(inptr);
  59.                 fprintf(stderr, "Could not create file %d.\n", picNum);
  60.                 return 3;
  61.             }
  62.             do{
  63.                 printf("writing out picture...\n");
  64.                 for (int i = 0; i < 512; i++) //write bytes into buffer
  65.                     fwrite(&buffer, 1, 1, inptr);
  66.                
  67.                 for (int i = 0; i < 512; i++){
  68.                     if (feof(inptr) < 1) //if we have reached the end of the file
  69.                         break;
  70.                     fwrite(&buffer[i], 1, 1, outptr);
  71.                 }
  72.             } while(byte_check(buffer) == false);
  73.            
  74.             fclose(outptr);
  75.             picNum++;
  76.         }    
  77.         // write the bytes to the buffer
  78. //        printf("writing to buffer....");
  79.         for (int i = 0; i < 512; i++){
  80.             if (feof(inptr)){// && ferror(inptr)){ //if we have reached the end of the file
  81.                 break;
  82.                 printf("end of file\n");
  83.             }            
  84.             fwrite(&buffer[i], 1, 1, inptr);
  85.         }
  86.     }
  87.     // close infile
  88.     fclose(inptr);
  89.  
  90.     // done
  91.     return 0;
  92. }
  93.  
  94.  
  95.  
  96. /*
  97.  * takes in the jpeg formatted array and checks to see if it is
  98.  * likely the jpeg head. returns true if it is, else returns false
  99.  */
  100.  
  101. bool
  102. byte_check(BYTE bytes[]){
  103.     // 0xff = 255, 0xd8 = 216, 0xe0 = 224, 0xe1 = 225
  104.     if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff &&
  105.         (bytes[3] == 0xe0 || bytes[3] == 0xe1))
  106.         return true;
  107.     return false;
  108. }
  109.  
  110. /*// checks to see if the signature exists in any part of the buffer*/
  111. /*bool in(BYTE bytes[]){*/
  112. /*    for (int i = 0; i < 508; i++){*/
  113. /*        if (bytes[i] == 0xff && bytes[i + 1] == 0xd8 && bytes[i + 2] == 0xff &&*/
  114. /*            (bytes[i+ 3] == 0xe0 || bytes[i+ 3] == 0xe1))*/
  115. /*            return true;        */
  116. /*    }*/
  117. /*    return false;*/
  118. /*}*/
Add Comment
Please, Sign In to add comment