0
#include <stdio.h>

include

include

int main(int argc, char *argv[]) { //ensure proper usage if (argc != 2) { fprintf(stderr, "Usage: /recover infile\n"); return 1; }

char *card = argv[1];

// open input file (card)
FILE *inptr = fopen(card, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", card);
    return 2;
}

//temporary storages
typedef uint8_t  BYTE;
BYTE block[512];
char filename[10];
int count = 0;

//iterate over blocks of the sd card (512 byte blocks)
while (fread(&block, 1, 512, inptr) == 512)
{

    //check if the header of the block is a jpeg
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
    {

        //create a file.jpg and store it in an array called filename, and keep track of the num. of the jpg
        sprintf(filename, "%03i.jpg", count);
        count++;

        // open filename for writing and write the jpeg header into it
        FILE *img = fopen(filename, "w");
        fwrite(block, 1, 512, img);

        //write in the jpeg until new jpeg is found, and then exit the loop
        while (1)
        {

            if ((fread(&block, 1, 512, inptr) == 512) && block[0] != 0xff && block[1] != 0xd8 && block[2] != 0xff && (block[3] & 0xf0) != 0xe0)
            {
                fseek(inptr, -512, SEEK_CUR);
                break;
            }

            fwrite(block, 1, 512, img);
        }

        //close the jpeg
        fclose(img);
    }
}

//close any remaining files
fclose(inptr);

}

my code creates 50 jpg, they all starts with the jpg signature but they are all 512 bytes. I don't understand why the following blocks aren't written in each jpg until a new one is found.

1

Simple. ALL the code to process data blocks is contained inside the if code block to detect signatures. The code to process data blocks without signatures needs to be outside and after the if code block.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • I found the problem: //write in the jpeg until new jpeg is found, and then exit the loop while (1) { // when EOF or a new jpeg is detected exit the loop if ((fread(&block, 1, 512, inptr) != 512) || (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)) { fseek(inptr, -512, SEEK_CUR); break; } // write a block to the jpeg fwrite(block, 1, 512, img); } – TheSaxo Dec 5 '19 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .