0

I've been trying to implement recovery algorithm for PSet4. As much as I understand the logic behind it, I'm stuck with the implementation.

The problems I'm having:

  • condition if(card == NULL) is somehow true, yet when running the program with GDB, there's some actual values being passed to the buffer.
  • At a second pass through the main for loop, program crashes with a seg fault, GDB returning:

    Program received signal SIGSEGV, Segmentation fault. __GI__IO_fread (buf=0xbffff0e0, size=512, count=1, fp=0x382f796c) at >iofread.c:41 41 iofread.c: No such file or directory. I imagine this has to do with the card variable returning NULL.

Here's my code at this point:

#include<stdio.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct
{
    unsigned char byte1;
    unsigned char byte2;
    unsigned char byte3;
    unsigned char byte4;
} 
BUFFER;

// struct to check file size
struct stat st;


int main(int argc, char* argv[])
{
    FILE* card = fopen("card.raw", "r");
    if (card == NULL);
    {
        printf("Could not open the file.\n");
    }
    BUFFER buffer;
    int filecount = 0;

    // check filesize in bytes
    stat("card.raw", &st);
    int fileLength = st.st_size;

    int blocksNumber;

    // set a number of blocks to search in
    if (fileLength % 512 == 0)
        blocksNumber = fileLength / 512;
    else
        blocksNumber = fileLength / 512 + 1;

    int found = 0;
    FILE* recovered;
    char filename[8];



    for (int i = 0; i < blocksNumber; i++)
    {
        fread(&buffer, 512, 1, card);

        // if that's the first file found
        if (((buffer.byte1 == 0xff && buffer.byte2 == 0xD8 && buffer.byte3 == 0xff && buffer.byte4 == 0xe0) || ((buffer.byte1 == 0xff && buffer.byte2 == 0xD8 && buffer.byte3 == 0xff && buffer.byte4 == 0xe1))) && found == 0)
        {   
            sprintf(filename, "%03d.jpg", filecount);
            recovered = fopen(filename, "w");
            found = 1;
            filecount += 1;
        }

        // if that's a consecutive file found
        if (((buffer.byte1 == 0xff && buffer.byte2 == 0xD8 && buffer.byte3 == 0xff && buffer.byte4 == 0xe0) || ((buffer.byte1 == 0xff && buffer.byte2 == 0xD8 && buffer.byte3 == 0xff && buffer.byte4 == 0xe1))) && found == 1)
        {
            fclose(recovered);
            sprintf(filename, "%03d.jpg", filecount);
            recovered  = fopen(filename, "w");
            filecount += 1; 
        }

        if (found == 1)
            fwrite(&buffer, 512, 1, recovered);
    }

    fclose(recovered);
}

I was looking for a more elegent way to state the condition to determine if the buffer matches the criteria of being a beginning of a JPEG but haven't had any other idea so far.

Thanks a lot!

0

I once knew a cowboy who tried to put 20 gallons into a 10 gallon hat. ;-)

You have created buffer as a 4 byte structure and then do an fread that tries to stuff 512 bytes into it. Want to guess what happens? ;-)

You should create a 512 byte buffer, do the fread, and then check the first 4 bytes. You don't need a struct for this.

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

1
  • Thanks a lot, used an unsigned char array of 512 instead, it worked very well!
    – Patrick
    Aug 11 '15 at 18:44

You must log in to answer this question.

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