I am stuck on pset4/recover. My program appears to recover all 50 images, but I am unable to pass the checker. Any tips or pointers (ha) are much appreciated. -Thad
/** * recover.c * * Computer Science 50 * Problem Set 4 * * Recovers JPEGs from a forensic image. */
include
include
include
include
int main(int argc, char* argv[])
{
//inport the card.raw file;
FILE* cardin = fopen("card.raw", "r");
if (cardin == NULL)
{
printf("file format not supported\n");
return 1;
}
//define a two hexademical variable type
typedef uint8_t BYTE;
BYTE jbyte1, jbyte2, jbyte3, jbyte4;
//loop through the card.raw file to find the jpg start sites
int location[51];
int counter = 0;
for (int i = 0; i < 1000000000/512; i++)
{
fread(&jbyte1, sizeof(jbyte1), 1, cardin);
fread(&jbyte2, sizeof(jbyte2), 1, cardin);
fread(&jbyte3, sizeof(jbyte3), 1, cardin);
fread(&jbyte4, sizeof(jbyte4), 1, cardin);
if (jbyte1 == 0xff && jbyte2 == 0xd8 && jbyte3 == 0xff && (jbyte4 == 0xe0 || jbyte4 == 0xe1 || jbyte4 == 0xe2 || jbyte4 == 0xe3 || jbyte4 == 0xe4 || jbyte4 == 0xe5 || jbyte4 == 0xe6 || jbyte4 == 0xe7 || jbyte4 == 0xe8 || jbyte4 == 0xe9 || jbyte4 == 0xea || jbyte4 == 0xeb || jbyte4 == 0xec || jbyte4 == 0xed || jbyte4 == 0xee || jbyte4 == 0xef))
{
location[counter] = i;
counter++;
if (counter == 50)
{
break;
}
}
fseek(cardin, (i+1)*512, SEEK_SET);
}
//find the end of the last file
fseek(cardin, location[49]*512, SEEK_SET);
for (int i = 0; i< (1000000000/512-location[49]); i++)
{
BYTE zero = 0;
BYTE sum = 0;
for (int j = 0; j < 200; j++)
{
fread(&zero, sizeof(zero), 1, cardin);
sum = sum + zero;
}
if (sum == 0)
{
location[50] = i + location[49];
break;
}
else
fseek(cardin, (location[49] + (i+1))*512, SEEK_SET);
}
//printf("start: %i\n end: %i\n", location[49], location[50]);
//write the jpg data to output files
char filename[8];
char jpg[5] = ".jpg\0";
BYTE jbyte;
for (int i= 0; i < 50; i++)
{
sprintf(filename, "%03d%s", i, jpg);
FILE* cardout = fopen(filename, "w");
if (cardout == NULL)
{
fprintf(stderr, "Could not create outfile");
return 2;
}
fseek(cardin, location[i]*512, SEEK_SET);
for (int j = 0; j < (location[i+1] - location[i])*512; j++)
{
fread(&jbyte, sizeof(jbyte), 1, cardin);
fwrite(&jbyte, sizeof(jbyte), 1, cardout);
}
fclose(cardout);
}
//goodbye return 0; }