0

I am working on PSET4 Recover, and got to writing to the output file, but for some reason, it SegFaults when I try writing to a jpg. The error, as far as I can tell, occurs in the While Loop in the Recover function.

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

int findJpg(FILE *ptr, unsigned char buffer[512], int blocks);
int recover(FILE *ptr, int index, unsigned char buffer[512],int blocks, int fileNumber);

int main(int argc, char *argv[])
{
    //DONE: Open a memory card file
    FILE *ptr = fopen("card.raw", "r");
    //Store a 512 bit block in an array
    unsigned char buffer[512];
    bool writing = false;
    int blocks = 1;
    
    
    //DONE: Look for the beginning of a JPEG
    while(findJpg(ptr, buffer, blocks) == -1)
    {
        blocks++;
    }
    int index = findJpg(ptr, buffer, blocks);
    
    //TODO: Open a new JPG File To begin writing to
    int fileNumber = 0;
    recover(ptr, index, buffer, blocks, fileNumber);
    //TODO: Write 512 Bytes till a new JPEG is found
    
    //TODO: Close old JPEG
    
    //TODO: Open New one
    
    //TODO: Repeat
    
    //Close "ptr"
    fclose(ptr);
    
    return 0;
}

int findJpg(FILE *ptr, unsigned char buffer[512], int blocks)
{
    
    int offset = blocks * 512;
    
    fread(buffer, 512, 1, ptr);
    fseek(ptr, offset, SEEK_CUR);
    for (int i = 0; i < 512; i++)
    {
        if(buffer[i] == 0xff)
        {
            //printf("Test 1 Passed\n");
            if(buffer[i+1] == 0xd8)
            {
                //printf("Test 2 Passed\n");
                if(buffer[i+2] == 0xff)
                {
                    //printf("Test 3 Passed\n");
                    if((buffer[i+3] & 0xF0) == 0xe0)
                    {
                        printf("Test 4 Passed at %i block at %i index\n", blocks, i);
                        return i;
                        
                    }
                }
            }
        }
    }
    //Since there will be no JPG header in this block, the program returns -1, a value which can't exist in an array.
    return -1;
}

int recover(FILE *ptr, int index, unsigned char buffer[512],int blocks, int fileNumber)
{
    //Initialize Buffer
    
    int offset = blocks * 512;
    fseek(ptr, offset, SEEK_CUR);
    fread(buffer, 512, 1, ptr);
    int i = index;
    
    char *filename = malloc(3);
    sprintf(filename, "%03i.jpg", fileNumber);
    
    FILE *img = fopen(filename, "r");
    printf("%p\n", (void*)&buffer[i]);
    int tblocks = blocks;
    while(findJpg(ptr, buffer, blocks) == -1)
    {
        printf("SegFault in While Loop in recover function(this is to help debug)\n");
        fwrite(&buffer[i], 512, 1, img);
        offset +=512;
        fseek(ptr, offset, SEEK_CUR);
        fread(buffer, 512, 1, ptr);
        blocks++;
        
    }
    int t = findJpg(ptr, buffer, tblocks);
    
    
    return 0;
}
1

Look at the following code:

FILE *img = fopen(filename, "r");
...
while(findJpg(ptr, buffer, blocks) == -1)
{
    fwrite(&buffer[i], 512, 1, img);

Question: How is the code going to write to the output file when it is opened for READ??? ;-)

Next question, why not just use buffer in the write statement?

There are more and significant problems ahead, but this will resolve your seg fault and get you going. Wouldn't want to deprive you of the chance to debug them yourself! ;-)

Happy programming!

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

1
  • You're a lifesaver man, thank you! – Ruben Alias Aug 1 '20 at 9:03

You must log in to answer this question.

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