Created
October 3, 2019 01:48
-
-
Save eandrewgolden/3fa2eadc87e3d1d15e17fbcbe73d2d3d to your computer and use it in GitHub Desktop.
recover.c
This file contains 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> | |
int main(int argc, char *argv[]) | |
{ | |
// take in one command-line argument only | |
if (argc != 2) | |
{ | |
fprintf(stderr, "Wrong!\n"); | |
return 1; | |
} | |
// remember filenames | |
char *raw_file = argv[1]; | |
// open input file | |
FILE *rawptr = fopen(raw_file, "r"); | |
if (rawptr == NULL) | |
{ | |
fprintf(stderr, "Could not open %s.\n", raw_file); | |
return 2; | |
} | |
//title number | |
int t = 0; | |
// create a charcter holder for title of file | |
char jpegFile[9]; | |
// create place holder for 512 bytes, one jpeg block | |
char jpegBlock[512];// = (char *)malloc(512 * sizeof(char)); | |
do | |
{ | |
fread(&jpegBlock, sizeof(jpegBlock), 1, rawptr); | |
} | |
while (jpegBlock[0] != (char)0xff && jpegBlock[1] != (char)0xd8 && jpegBlock[2] | |
!= (char)0xff && (jpegBlock[3] & (char)0xf0) != (char)0xe0); | |
// scan for the total number of jpegs | |
for (int i = 0; i < 3; i++) | |
{ | |
t++; | |
// create jpeg file with titled name | |
sprintf(jpegFile, "%03i.jpeg", t); | |
// open jpeg file to write on | |
FILE *jpegptr = fopen(jpegFile, "w"); | |
// check if jpeg file exists | |
if (jpegptr == NULL) | |
{ | |
fclose(rawptr); | |
fprintf(stderr, "Could not create %s.\n", jpegFile); | |
return 3; | |
} | |
do | |
{ | |
fwrite(&jpegBlock, sizeof(jpegBlock), 1, jpegptr); | |
fread(&jpegBlock, sizeof(jpegBlock), 1, rawptr); | |
} | |
while (jpegBlock[0] != (char)0xff && jpegBlock[1] != (char)0xd8 && jpegBlock[2] | |
!= (char)0xff && (jpegBlock[3] & (char)0xf0) != (char)0xe0); | |
// close current jpeg file | |
fclose(jpegptr); | |
} | |
// close infile | |
fclose(rawptr); | |
// success | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment