So far, I have managed to find the byte-sequence that a block in the .jpeg file starts with. Now I don't know how to tell the program to stop at the next such block.
Moreover, I don't know how I should use sprintf to name the file. I have read up on sprintf, but I don't really see how it can help me to name a file.
Here is my code:
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
//Open the file
FILE* in_p = fopen("card.raw", "r");
//Check if the file exists and can be opened
if (in_p == NULL)
{
printf("Could not open 'card.raw'.\n");
return 1;
}
//Declare the buffer where the block of 512 bytes will be stored
unsigned char buffer[512];
//Read from the file until it ends
while (fread(&buffer, sizeof(buffer), 1, in_p))
{
//const char mark_1[4] =
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
// If the jpeg-sequence is found write into new jpeg
// until a new jpeg-sequence is found
printf("found \n ");
break;
//FILE* out_p = fopen("title", "a");
//fwrite(&buffer, sizeof(buffer), 1, out_p);
//fclose(out_p);
}
else
{
printf("not found \n");
}
// when found, start at the beginning of the block and write it
//when we hit it, every block has to be written to a new jpeg file (so write)
//when we hit the next block, we close the last jpeg and start writing to a new jpeg file
//sprintf(char title[8], "%.3d.jpg", 2)
//title: char array to store the resultant string
//sprintf(title[8], "%.3d.jpg", 2);
//this prints out 2.jpg and not 002.jpg (NOT ANYMORE)
}
// Close "memory file"
//fclose(in_p);
//return 0;
}
Thanks.