Created
December 31, 2020 23:15
-
-
Save Edmundworks/803d8aad5fd8320bb185caa9f2813461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
//TODO | |
// Figure out how to detect the end of the file an stop | |
// Figure out what the nth piece of buffer contains when it reads into it from the final chunk | |
// ie when it runs out of infile, and tries to read that into buffer, what does buffer[i] then contain? | |
// use that as condition either | |
// a) in an if () branch on line 77 OR | |
// b) as an extra criteria in the while loop beginning line 80 | |
int main(int argc, char *argv[]) | |
{ | |
// Ensure proper usage | |
if (argc != 2) | |
{ | |
fprintf(stderr, "Usage: ./recover file-to-recover\n"); | |
return 1; | |
} | |
// Open infile | |
char *infile = argv[1]; | |
FILE *inptr = fopen(infile, "r"); | |
// Error check for blank infile | |
if (inptr == NULL) | |
{ | |
fprintf(stderr, "Infile is blank\n"); | |
return 2; | |
} | |
// Create array for storing read data | |
unsigned char buffer[512]; | |
// number of JPEGs found | |
int pic_counter = 0; | |
// Filename of new pic | |
char new_pic[8]; | |
// Read first 512 bytes of infile into a buffer | |
do | |
{ | |
fread(&buffer, 1, 512, inptr); | |
// condition for when a jpeg is found | |
} | |
while (!(buffer[0] == 0xff && | |
buffer[1] == 0xd8 && | |
buffer[2] == 0xff && | |
(buffer[3] & 0xf0) == 0xe0)); | |
do | |
{ | |
// create new jpeg of format "002.jpg" | |
sprintf(new_pic, "%03i.jpg", pic_counter); | |
FILE *img = fopen(new_pic, "w"); | |
// write to new_pic once | |
// Suspect this might be adding one chunk too many? | |
fwrite(&buffer, 1, 512, img); | |
// keep reading and writing to new pic until new JPEG is hit | |
do | |
{ | |
fread(&buffer, 1, 512, inptr); | |
if (feof(inptr)) | |
{ | |
break; | |
} | |
fwrite(&buffer, 1, 512, img); | |
} | |
while (!(buffer[0] == 0xff && | |
buffer[1] == 0xd8 && | |
buffer[2] == 0xff && | |
(buffer[3] & 0xf0) == 0xe0)); | |
// add one to pic counter | |
pic_counter++; | |
// close outfile | |
fclose(img); | |
// print current pic counter | |
printf("Pic counter is %i\n", pic_counter); | |
} | |
while (pic_counter < 100); | |
// close infile | |
fclose(inptr); | |
// Success | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment