我遵循pset 3 restore的伪代码,我的代码仅输出一个图像,并且在number = fread(buffer, 1, 512, file);
行中第4次循环后,调试器(debug50)退出。
我检查了输出jpeg的十六进制值,发现与原始文件相比,它也是不正确的。我可以辨认出图像,但有些十六进制数偏离很小。I
// open card file
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
fprintf(stderr, "Couldn't open %s", argv[1]);
return 2;
}
// buffer
unsigned char *buffer = malloc(513);
buffer[512] = 0x00;
// number
int number = 512;
// bool
int jpegFound = 0;
// jpeg number
int jpegNum = 0;
// jpeg name
char *jpegName = malloc(8);
jpegName[7] = 0x00;
// jpeg file pointer
FILE *jpegPtr;
// iterate through all the blocks of the card
while (1)
{
// read 512 bytes into a buffer
number = fread(buffer, 1, 512, file);
// break out of the loop if its the EOF
if (number != 512)
{
break;
}
// check whether its the start of a new jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// have we already found a jpeg?
if (jpegFound == 1)
{
// close previous jpeg file
fclose(jpegPtr);
// store the jpegName in a string
sprintf(jpegName, "%03i.jpg", jpegNum);
// open a new file with the stored jpeg name
jpegPtr = fopen(jpegName, "w");
}
else
{
// we found a jpeg finally
jpegFound = 1;
// store the jpeg Name in a string
sprintf(jpegName, "%03i.jpg", jpegNum);
// open a new file with the stored jpeg name
jpegPtr = fopen(jpegName, "w");
}
// write buffer to the jpeg
fwrite(buffer, 1, 512, jpegPtr);
}
else
{
if (jpegFound == 1)
{
// these bytes are part of the previous jpeg
fwrite(buffer, 1, 512, jpegPtr);
}
else
{
// this means that these bytes can be discarded
}
}
}
// close any remaining files
fclose(jpegPtr);
// free the memory we used in malloc
free(buffer);
free(jpegName);
// return 0
return 0;
./recover card.raw
的输出预计会是很多图像,但只能得到一张看起来还不错的图像,但其十六进制值有些偏离。
答案 0 :(得分:0)
我修复了它。我也没有增加jpegNum
,就debug50
而言,我仍然不知道是什么导致它退出。