The recovered images are fine, and when comparing the recovered images with the images in card.raw using xxd, I see that they're not the first 8 images on the card.

Some of the recovered images start with 0xff 0xd8 0xff 0xe0 and some with 0xff 0xd8 0xff 0xe1. Here is my source code:

// Get file size.
int fileSize = 20056064;
int fileBlocks = fileSize / 512;

char jpg1[4] = {0xff, 0xd8, 0xff, 0xe0};
char jpg2[4] = {0xff, 0xd8, 0xff, 0xe1};

// Variable to keep track of number of JPEGs scanned.
int curFile = 0;

for (int i = 0; i < fileBlocks; i++)
    // Read each block of the file.
    char *BLOCK = malloc(512);
    fread(BLOCK, 512, 1, card);

    // Check if block is start of a JPEG.
    if (memcmp(BLOCK, jpg1, 4) == 0 || memcmp(BLOCK, jpg2, 4) == 0)
        // Create name for the JPEG.
        char name[8];
        if (sprintf(name, "%.03d.jpg", curFile) != 7)
            printf("Could not create %s\n", name);
            return 3;

        // Create a new file for dumping data in.
        FILE *jpeg = fopen(name, "w");
        if (jpeg == NULL)
            printf("Unable to create %s\n", name);

        // Write first block
        fwrite(BLOCK, 512, 1, jpeg);
        fread(BLOCK, 512, 1, card);

        // Start copying the JPEG from card to jpeg.
        while(!(memcmp(BLOCK, jpg1, 4) == 0) && !(memcmp(BLOCK, jpg2, 4) == 0))
            fwrite(BLOCK, 512, 1, jpeg);
            fread(BLOCK, 512, 1, card);

        // Free up the memory in the heap to prevent leaks.

Your code is recovering every other jpeg file because of a logic/structure issue. You have set up your code to do this:

  1. read blocks until it finds a jpeg signature
  2. write the signature block to a new file
  3. inside the loop, keep writing blocks until signature is found.
  4. Once signature is found, don't do anything with the block, just go back to top of outer loop and step 1.

Here's why you're writing every other file. When you find the second signature, you exit the loop without doing anything with it. You then read the next block, but since it has no signature, you just keep reading until you find the signature for the third file. Net result is that you skip every other file.

This will involve significant altering of your program to fix, so I'll leave it to you to figure out the logic. But this will get you going.

If this answers your question, please accept the answer to remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

  • Thank you very much, that was indeed the problem. I have now fixed it and it seems to work now, I should probably come up with a better system though. Jun 21 '15 at 19:49

You must log in to answer this question.

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