/** * recover.c * * Computer Science 50 * Problem Set 4 * * Recovers JPEGs from a forensic image. */ #include #include #include #include
int main(void) {
FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
fclose(card);
printf("Could not open %s\n", "card.raw");
return 1;
}
FILE* images = NULL;
uint8_t buffer[512];
char title[8];
int counter = 0;
while(fread(buffer, 512, 1, card) == 1)
{
if(buffer[0] == 0xff && buffer[1] == 0xd8 &&
buffer[2] == 0xff)
{
if(buffer[3] <= 0xef || buffer[3] >= 0xe0)
{
counter++;
sprintf(title,"%03d.jpg",counter);
if(images != NULL)
{
fclose(images);
}
images = fopen(title,"w");
if(images == NULL)
{
printf("error opening imagefile\n");
return 1;
}
fwrite(&buffer, 512, 1, images);
}
}
}
fclose(images);
fclose(card);
return 0;
}