SHARE
TWEET

Code that can get out two images

a guest Sep 11th, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <stdint.h>
  4. #include <unistd.h>
  5.  
  6. int main (int argc, string argv[])
  7. {
  8.     if (argc != 2)
  9.     {
  10.         fprintf(stderr, "Usage: ./recover image");
  11.         return 1;
  12.     }
  13.  
  14.     char *readingfile = argv[1]; //name of file
  15.     //char *outfile; do we need this here since this is going to be changing?
  16.     FILE *memorycard = fopen (readingfile, "r"); //setting pointer for memorycard to the file we opened with the name given
  17.  
  18.     uint8_t buffer[4]; //buffer to hold first four values of the array! (for checking purposes, of course!), could this happen with char*?
  19.  
  20.      //starting off with the first four bytes of data
  21.     printf("Location of bmp after moving 4 bytes: %li\n", ftell(memorycard));
  22.  
  23.  
  24.  
  25.  
  26.     if (memorycard == NULL) //if any segmentation faults
  27.     {
  28.         fprintf(stderr, "Usage: ./recover image");
  29.         return 2;
  30.     }
  31.  
  32.     bool didnotfindjpg = true;
  33.     int blocksofspace = 0;
  34.  
  35.     do //fread has size of each element to be read before number of elements
  36.     {
  37.         fread(buffer, 4, 1, memorycard);
  38.  
  39.         if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
  40.         {
  41.             didnotfindjpg = false;
  42.             fseek(memorycard, -4, SEEK_CUR); //go to the beginning of the block we are going to read
  43.         }
  44.  
  45.         else
  46.         {
  47.             fseek(memorycard, -4, SEEK_CUR);
  48.             fseek(memorycard, 512, SEEK_CUR);
  49.              //sort of like magnifying glass to see what we are reading
  50.             printf("First value of BMP: %i\t, Second Value: %i\t,  Third Value: %i\t, Fourth value: %i\n",buffer[0], buffer[1], buffer[2], buffer[3]);
  51.  
  52.         }
  53.  
  54.  
  55.     } while (didnotfindjpg == true); //now we figured out where the file starts
  56.  
  57.  
  58.  
  59.  
  60.     uint8_t bigbuffer[512];
  61.     bool endofjpeg = false;
  62.  
  63.     while (endofjpeg == false) //will stop when we reach the end of the jpegfile
  64.     {
  65.         for (int g = 0; g < 5; g++)
  66.         {
  67.             int a = 10*g; //does this read within the parentheses coming next?
  68.             printf("This is the ten's place of the number of pictures we are saving: %i",g);
  69.             for (int x = 0 + a; x < 10 + a; x++)
  70.             {
  71.                 char nameoffile[7];
  72.                 if (x < 10) //we need to use two digits for making file names greater than 10!
  73.                 {
  74.                     sprintf(nameoffile,"00%i.jpg", x);
  75.                 }
  76.                 if (x >=10)
  77.                 {
  78.                     sprintf(nameoffile, "0%i.jpg", x);
  79.                 }
  80.  
  81.                 FILE *writehere = fopen (nameoffile, "w");
  82.                 bool startofnextfile = false;
  83.                 do
  84.                 {
  85.                     int h = fread(bigbuffer, 1, 512, memorycard);
  86.                     printf("%i\n", h);
  87.                     if (h == 512)
  88.                     {
  89.                     fwrite(bigbuffer, 1,512, writehere);
  90.                     }
  91.                     else
  92.                     {
  93.                         fclose(writehere);
  94.                         printf("HEY!");
  95.                         return 0;
  96.                     }
  97.                     //free(bigbuffer);
  98.                     //free(buffer);
  99.                     fread(buffer, 4, 1, memorycard);
  100.  
  101.                     if  (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //while not start of JPEG
  102.                     {
  103.                         startofnextfile = true;
  104.                         fclose(writehere); //do i close "writehere" or the file?
  105.                         fseek(memorycard, -4, SEEK_CUR);
  106.  
  107.                     }
  108.                     else
  109.                     {
  110.                         fseek(memorycard, -4, SEEK_CUR);
  111.  
  112.                     }
  113.                 }
  114.                     while (startofnextfile == false);
  115.             }
  116.         }
  117.  
  118.     }
  119.  
  120.  
  121.     return 0;
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top