我一次扫描512字节的原始位。 如果前 4 个字节与 jpeg 文件后缀匹配,我会创建一个新的 jpeg 并将该块和所有后续块写入其中,直到遇到新的 jpeg 后缀。
出于某种原因,它会在第一次写入调用之后的读取调用上出现段错误。
/** * 恢复.c * * 计算机科学 50 * 问题集 4 * * 从取证图像中恢复 JPEG。 */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef uint8_t BYTE;
int main(int argc, char* argv[])
{
//load in file
FILE* file = fopen("card.raw", "r");
FILE* newfile = NULL;
//error opening file
if (file==NULL){
return 1;
}
//create buffer and outfile index
BYTE buffer[512]; //
int filecount = 0;
char newfilename[3];
//iterate through file, break on eof
while (1){//(!feof(file)){//(fgetc(file) != EOF){
//seek back to 512 block start
//fseek(file,-1,SEEK_CUR);
//fread(&buffer,1,512,file);
if (fread(buffer,512,1,file) <= 0){
break;
}
//Check if first 4 bytes are jpeg signature
if ((buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
buffer[3] == 0xe0)||
(buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
buffer[3] == 0xe1 )){
//close out previously created jpeg
if (newfile != NULL){
//file close error
if (fclose(newfile) == EOF){
return 2;
}
newfile = NULL;
}
//create new jpeg
sprintf(newfilename,"%03d.jpeg",filecount++);
newfile = fopen(newfilename,"w");
}
//Write blocks to newfile
if (newfile != NULL){
if (fwrite(buffer,512,1,newfile) <= 0){
return 4;
}
}
}
//close out last newfile
if (newfile != NULL){
//file close error
if (fclose(newfile) == EOF){
return 3;
}
newfile = NULL;
}
//close infile
fclose(file);
//we should find 16 images
}
没有足够的
数组来存储文件名
char newfilename[3];
当你这样做时
sprintf(newfilename,"%03d.jpeg",filecount++);
这将需要
char newfilename[9];