My recover successfully recovers 24 images that are all good; nothing scrambled or anything. But I thought there were supposed to be 50 images. I imagine that I'm skipping over every other image or something. Moreover, check50 says 015.jpg does not exist when there is a 015.jpg.
:) recover.c exists :) recover.c compiles :( recovers 000.jpg correctly :( recovers middle files correctly :( recovers last file correctly \ expected 015.jpg to exist
EDIT: I realized the issue may have to do with limiting the 4th JPEG byte to only two possibilities. Since the 4th byte has to be between 0xe0 and 0xef, I tried to do a greater than/lesser than comparison. But it didn't extract any files at all this way. I'm still not even sure this is the problem.
if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2]== 0xff) && ((buffer[3] >= 0xe0 && buffer[3] <= 0xef)))
code starts here:
#include <stdio.h>
#include <stdlib.h>
//blocks are 512 bytes
int const block = 512;
//define byte
typedef unsigned char BYTE;
int main()
{
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL) {
printf("Could not open file.\n");
return 1;
}
//calculate size of input file in blocks
fseek (inptr, 0L ,SEEK_END); //Go to end of file
int length = ftell (inptr); //return cursor position, which is size of file in bytes
fseek (inptr, 0L, SEEK_SET); //go back to the beginning of file
int blocknum = (length/block); // how many blocks
int counter = 0;
//do this for each block of the input file
for (int i = 0; i < blocknum; i++)
{
//make buffer array that has 512 capacity
BYTE buffer[512];
//read input file to buffer 512 bytes at a time
fread(&buffer, sizeof(buffer), 1, inptr);
//compare first 4 bytes for jpg sig
if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2]== 0xff) && ((buffer[3] & 0xf0) == 0xe0))
{
char name[9];
sprintf(name, "%03i.jpg", counter);
//make output file
FILE* outfile;
outfile = fopen(name, "w");
fwrite(&buffer , 512, 1, outfile);
//going on to next block
fread(&buffer, 512, 1, inptr);
//keep reading blocks until you find another sig
while (!(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1)))
{
fwrite(&buffer, 512, 1, outfile);
fread(&buffer, 512, 1, inptr);
}
//close file you just made
fclose(outfile);
counter++;
}
}
}
buffer
not&buffer
in fread and fwrite,buffer
itself is a pointer to the chunk of 512 bytes. Otherwise, you risk segfaults. – Blauelf Mar 2 '17 at 20:02buffer
is an array, not a pointer, so&buffer
is correct. – DinoCoderSaurus Mar 3 '17 at 11:29malloc
, it's on the heap. But C seems to do some magic,buffer
and&buffer
are both the same. Did not know that. – Blauelf Mar 3 '17 at 11:39