0

Been struggling with the recover PSET for a while now, my code recovers 26 images but when i try to open them i get the following error "Invalid or Unsupported Image Format ". I believe it's because the file is too large. I have played around with the if and else if statement within the while loop, but can't to get it to work.

Any pointers would be appreciated - no pun, intended lool

\\ #include #include #include #include #include int main(int argc, char *argv[]) {

// variable declaration
typedef uint8_t BYTE;
string filename = malloc(20);
FILE *img;
BYTE storage[520];
bool found = false;
int n = 2047;
int f = 0;

if (argc == 2)
{
/// Open card.raw
FILE *source = fopen(argv[1], "r");

while(0 < 1)

{
// Reading the data from card.raw & temporarly storing the current 512 bytes being read into an array
  fread(storage, 1 , 512, source);

// Checking that the current 512 that's stored cointains the signature of a JPEG
    if(storage[0] == 0xff  && storage[1] == 0xd8 && storage[2] == 0xff && (storage[3] & 0xe0) == 0xe0)
     {


// if this is the the first JPEG, opening new file and initiating writting process
        if(f == 0 )
        {
              sprintf(filename, "%03i.jpg", f);
              img = fopen(filename, "w");
              fwrite(storage, 1, 512, img);
              found = true;
              f++;
       }

// if this is not the first JPEC, close old file and open a new file and initiating the writting process
       else if(f > 0)
       {
            fclose(img);
            sprintf(filename, "%03i.jpg", f);
            img = fopen(filename, "w");
            fwrite(storage, 1, 512, img);
            f++;
        }
     }

// if i already found a JPEG, i keep writting to the file
    if (found == true)
  {
      for(int i = 0 ; i < n ; i++)
      {
          fwrite(storage, 1, 512, img);
      }

  }

// checking if i have reached the end of the file

    if(fread(storage, 1 , 512, source) < 512)
    {
        fclose(img);
        fclose(source);
        break;
    }

}

free(filename);

}

else
{

//Error messaging for when the user's input is incorrect-
    printf("Please enter a valid input\n");
    return 1;
}

}

1 Answer 1

0

You have two freads in your loop. You don't check the first one for hitting EOF, and you ignore all the data read by the second one. I used a single fread in my loop condition.

I think you also have too many fwrites, so you write the header block twice. You could check that theory by having xxd or similar print the image to the terminal as a hex dump.

1
  • Finally worked!!! Thanks a lot
    – mo_coder
    Commented Feb 5, 2020 at 3:20

You must log in to answer this question.

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