0

Having a problem with Recover. The debugger shows that "buffer" is always 0000. Tried everything I could think of here. Any ideas?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    //check argc to ensure only 1 (technically 2) command line arguments are present
        //if more or less, return Usage: ./recover image
    if (argc != 2)
    {
        printf("Error: Must include file name to recover from only.");
        return 1;
    }

    //open memory card in read mode
    FILE *card = fopen(argv[1], "r");

    //if it can't find the file, return error
    if (card == NULL)
    {
        printf("Error: Cannot read file.");
        return 2;
    }

    // Open our first file.
    int counter = 0;

    FILE *output = NULL;

    // Create a 512 byte buffer.
    BYTE buffer[512];

    // while we're not at the end of the card.raw file, read 512 bytes {
    while (fread(buffer, sizeof(BYTE), 512, card))
    {
      // at this point we have 512 bytes of the file.
      // check the first 3 to see is a JPG AND.. if we convert hex to binary, then check the high 4 bit and it's 1110  {
      if (buffer[0] == 0xff
          && buffer[1] == 0xd8
          && buffer[2] == 0xff
          && (buffer[3] >= 0xe0 && buffer[3] < 0xf0)) {

        // YES
        // are we working on another jpg?
        if (output) {
          fclose(output);
        }

        // YES: close the other file.
        // start a new jpg file outout
        counter++;
        char filename[8];
        sprintf(filename, "output%d.jpg", counter);
        output = fopen(filename, "w");

      //  char filename[8] = ("output%d.jpg", counter); // output0.jpg, output1.jpg...
      //  FILE *output = fopen(filename, "w");

        // copy the 512 to the new jpg output
        fwrite(buffer, 512, 1, output);
      } else {
        // NO
        // continue copying
        fwrite(buffer, 512, 1, output);
      }

    }

    // Free up the buffer.
    free(buffer);

    // also close the very last file.
    fclose(output);

    // close the card.raw file
    fclose(card);

}
0

Problematic code, but we are on the right track, two things regarding your question:

 else {
            // NO
            // continue copying
            fwrite(buffer, 512, 1, output);
          }

what happens if there is no output, that is if the output file is NULL, I would change this else clause for:

if(output != NULL){
    // NO
    // continue copying
    fwrite(buffer, 512, 1, output);
}

Second thing, why do you free an array? It is not necessary if you do not reserve memory with malloc or calloc, arrays are released automatically and cause memory leaks.

There are other problems, but I hope you will be able to solve them.

1
  • I will take a look at this for sure! The free() code was actually a remnant from when I used malloc, I removed that at some point and forgot to remove free, so thanks for catching that. – Brandon Zemel May 30 '20 at 1:05

You must log in to answer this question.

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