:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:) recovers 000.jpg correctly
:) recovers middle images correctly
:( recovers 049.jpg correctly
recovered image does not match
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#define bs 512
typedef unsigned char byte;
byte buffer[bs];
// char outfile[17]; // = "recovered-000.jpg";
char outfile[50];
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("Usage: ./recover image");
return 1;
}
char *infile = argv[1];
// char *fn = argv[1];
FILE *inptr = fopen(infile, "r");
//FILE *outfile;
if(inptr == NULL)
{
printf("Could not open Given file");
return 2;
}
printf("Opened %s to read\n", infile);
int nof = 0;
FILE *outptr;
sprintf(outfile, "recovered-%03d.jpg", nof);
outptr = fopen(outfile, "w");
printf("Opened %s to write\n", outfile);
int block_count = 0;
while (true)
{
if (feof(inptr)) {
printf("Reached end of input file\n");
break;
}
int read_chunks = fread(buffer, 512, 1, inptr);
printf("Read %d chunks from inputfile\n", read_chunks);
// if (fread(buffer, 512, 1, inptr) < 1) break;
// bool containsjpegHeader;
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
printf("Found jpegHeader count: %d\n", nof);
fclose (outptr);
printf("Closed %s\n", outfile);
printf("outfile about to change from %s\n", outfile);
sprintf(outfile, "%03d.jpg", nof);
printf("outfile changed to %s\n", outfile);
outptr = fopen(outfile, "w");
nof++;
}
printf("Opened %s to write\n", outfile);
fwrite (buffer, 512, 1, outptr);
/* while (true) {
if (feof(inptr)) {
printf("Reached end of input file in inner loop\n");
break;
}
fread(buffer, 512, 1, inptr);
fwrite (buffer, 512, 1, outptr);
printf("%07x block read from %s and wrote to %s\n", block_count++*512, infile, outfile);
//fclose()
if (buffer[0] == (0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)) break;
} */
}
fclose(inptr);
// if (some codnditions meet) break;
}