The recover program seems to be working almost perfectly, but I think I must be missing the first file, somehow. I get images 000.jpg to 049.jpg, and all of them are openable, but with a few weird things happening:
-The second to last picture, 048.jpg (Hulk homage, I think), has tons of white space after it. This makes me think this should actually be the last picture. I have an idea for how I can get it to not add so much white space, but that's not the big problem.
-049.jpg opens, but it is not really a picture:
-So, this makes me think that the picture I have as 000.jpg (A woman in a black parka in the snow) is actually the second picture in the file, not the first one.
Here's the code:
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
#include<cs50.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
int main(int argc, char* argv[])
{
FILE* input = fopen("card.raw", "r");
FILE* output;
//Ok, gonna need some variables that persist:
int filecount = 0;
uint8_t block1Check = 0xff;
uint8_t block2Check = 0xd8;
uint8_t block3Check = 0xff;
uint8_t block4CheckLow = 0xe0;
uint8_t block4CheckHigh = 0xef;
uint8_t wholeBlock[512];
bool recovering = false;
while (filecount < 49) //keep looking through the file until I've found all 50 jpegs
{
//fetch the beginning of the block
fread(&wholeBlock, sizeof(wholeBlock), 1, input);
uint8_t block1 = wholeBlock[0];
uint8_t block2 = wholeBlock[1];
uint8_t block3 = wholeBlock[2];
uint8_t block4 = wholeBlock[3];
//If it's a jpeg signature, get the jpeg, and up the filecount by one
if (block1==block1Check && block2==block2Check && block3==block3Check && ((block4>=block4CheckLow)&&(block4<=block4CheckHigh)))
{
if(!recovering)
{
char filename[3];
sprintf(filename, "%03i.jpg", filecount);
output = fopen(filename, "w");
fwrite(wholeBlock, sizeof(wholeBlock), 1, output);
recovering = true;
}
else
{
fclose(output);
filecount++;
char filename[3];
sprintf(filename, "%03i.jpg", filecount);
output = fopen(filename, "w");
fwrite(wholeBlock, sizeof(wholeBlock), 1, output);
recovering = true;
}
}
else if(recovering)
{
fwrite(wholeBlock, sizeof(wholeBlock), 1, output);
}
}
//make sure everything is closed
if(output)
{
fclose(output);
}
fclose(input);
return 0;
}
Any help would be greatly appreciated.
Thanks,
-B