我已经在“恢复”程序中苦苦挣扎了几个小时,但似乎找不到问题。
我以为我已经关闭了所有文件,而且我还没有使用“malloc”语法进行内存分配。
您能给我一些提示或指导吗?非常感谢!
我也尝试过使用 Valgrind,它显示如下:
==34245== Memcheck, a memory error detector
==34245== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==34245== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==34245== Command: ./recover card.raw
==34245==
==34245==
==34245== HEAP SUMMARY:
==34245== in use at exit: 944 bytes in 2 blocks
==34245== total heap usage: 104 allocs, 102 frees, 233,912 bytes allocated
==34245==
==34245== 944 bytes in 2 blocks are still reachable in loss record 1 of 1
==34245== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==34245== by 0x4A076CD: __fopen_internal (iofopen.c:65)
==34245== by 0x4A076CD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==34245== by 0x1092A9: main (recover.c:48)
==34245==
==34245== LEAK SUMMARY:
==34245== definitely lost: 0 bytes in 0 blocks
==34245== indirectly lost: 0 bytes in 0 blocks
==34245== possibly lost: 0 bytes in 0 blocks
==34245== still reachable: 944 bytes in 2 blocks
==34245== suppressed: 0 bytes in 0 blocks
==34245==
==34245== For lists of detected and suppressed errors, rerun with: -s
==34245== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// Ensure proper usage
if (argc != 2)
{
printf("Usage: ./recover card\n");
return 1;
}
//If the forensic image cannot be opened for reading, your program should inform the user as much, and main should return 1.
// Remember filenames
char *infile = argv[1];
// Open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", infile);
fclose(inptr);
return 1;
}
// block size is 512 bytes
typedef uint8_t BYTE;
int BLOCK_SIZE = sizeof(BYTE) * 512;
BYTE buffer[BLOCK_SIZE];
int index = 0;
FILE *img = NULL;
//The files you generate should each be named ###.jpg, where ### is a three-digit decimal number, starting with 000 for the first image and counting up.
while (fread(buffer, 1, BLOCK_SIZE, inptr) == BLOCK_SIZE)
{
char filename[8];
// is first pic.
if (index == 0 ) {
sprintf(filename, "%03i.jpg", index);
img = fopen(filename, "w");
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) {
if (img != NULL) {
fwrite(buffer, 1, BLOCK_SIZE, img);
}
index ++;
}
} else {
//is not first pic
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) {
//is a new img
//close prev img
if (img != NULL) {
fclose(img);
}
// open a new img
sprintf(filename, "%03i.jpg", index);
img = fopen(filename, "w");
fwrite(buffer, 1, BLOCK_SIZE, img);
index ++;
} else {
//keep writing
fwrite(buffer, 1, BLOCK_SIZE, img);
}
}
//Your program, if it uses malloc, must not leak any memory.
}
if (img != NULL) {
fclose(img);
}
fclose(inptr);
return(0);
}
根据退休忍者的评论解决了问题。
愚蠢的错误...
我找到第一个img后打开第一个img文件,然后解决问题。
感谢论坛!
// is first pic.
if (index == 0 ) {
sprintf(filename, "%03i.jpg", index); //
img = fopen(filename, "wb"); //
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) {
sprintf(filename, "%03i.jpg", index);
img = fopen(filename, "w");
if (img != NULL) {
fwrite(buffer, 1, BLOCK_SIZE, img);
}
index ++;
}
}