0
/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char* argv[])
{
    FILE* card = fopen("card.raw", "r");

    //check for error in card reading
    if(card == NULL){
    printf("Card reading problemo");
    return 1;
    }

    BYTE buffer[512];
    int jpgno = 0;

    FILE* img = NULL;

    while(!feof(card)){

    //read 512 bytes of card into the buffer
    if(jpgno != 0)
    {
    fread(&buffer,sizeof(buffer),1,card);
    }

    if(jpgno == 0){

    do{

    fread(&buffer,sizeof(buffer),1,card);

    //check to find jpg first time
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1)){

    printf("File Found!");

    }
    }while(buffer[0]!=0xff || buffer[1]!=0xd8 || buffer[2]!=0xff || (buffer[3]!=0xe0 && buffer[3]!=0xe1));
    }

    printf("Found it!");

    //if new jpg is found
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))  //!!!!!!!111!1
    {

    //close previous file if not the first time
    if(jpgno != 0){
    fclose(img);
    }

    jpgno++;

    char* title = "000.jpg";

    if(jpgno > 9)
    sprintf(title, "%d%d.jpg", 0,jpgno);

    if(jpgno < 10)
    sprintf(title, "%d%d%d.jpg", 0,0,jpgno);

    img = fopen(title, "a");

    //error check for img file
    if(img == NULL){
    printf("Img problemo");
    return 2;
    }

    }

    //write 512 byttes of buffer to jpg
    fwrite(buffer,512,1,img);     

   }

    free(card);
}

//while(!(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 && buffer[3] == 0xe1)));

I'm really sorry to bother guys for the third time again but it's not working. I thought I understood it but turns out I dindt, following Cliff's advice I did this and even tried

while(!buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))

But it isnt working.It just keeps looping endlessly.

1

You need to use malloc() here:

char* title = "000.jpg";

so that your sprintf() can write to title safely.

Here's an explanation:

So how come that you get segmentation fault? - The main point is that char* title = "000.jpg"; makes title point to the read-only memory where your string literal "000.jpg" is stored. So when you try to access this memory: sprintf(title, "%3d.jpg", 0, 0, jpgno);, it is a memory access violation.

https://stackoverflow.com/questions/12795850/string-literals-pointer-vs-char-array

PS: Using "%3d" instead of "%d" for your conversion specifier will ensure it's always three digits, adding 0's to the front as necessary.

1
  • Thanks a lot! Finally did it. – Jedi18 Oct 4 '15 at 13:12
0

The advice was to negate the whole test, not the first element. !a && b is not the same as !(a & b) Look at the code:

while(!buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && 
              (buffer[3] == 0xe0 || buffer[3] == 0xe1))

It is checking that buffer[0] isn't 0xff while the remaining elements are equal to the elements indicated. Enclose the whole test inside parentheses and put the ! before the opening parenthesis.

2
  • Sorry that was a typo, I'd actually done that and it still keeps looping. – Jedi18 Oct 1 '15 at 10:24
  • 1
    Wasn't style50 in the advice, also? – Sam Gerber Oct 1 '15 at 17:58
0

I updated my code, removing the while loop, but it still shows a seg fault.

/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
**/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char* argv[])
{
    FILE* card = fopen("card.raw", "r");

    //check for error in card reading
    if(card == NULL)
    {
        printf("Card reading problemo");
        return 1;
    }

    BYTE buffer[512];
    int jpgno = 0;

    FILE* img = NULL;

    while(fread(&buffer,sizeof(buffer),1,card) == 1)
    {
        //if new jpg is found
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))  //!!!!!!!111!1
        {
            //close previous file if not the first time
            if(jpgno != 0)
            {
                fclose(img);
            }

            jpgno++;
            char* title = "000.jpg";

            if(jpgno > 9)
                sprintf(title, "%d%d.jpg", 0,jpgno);

            if(jpgno < 10)
                sprintf(title, "%d%d%d.jpg", 0,0,jpgno);

            img = fopen(title, "a");

            //error check for img file
            if(img == NULL)
            {
                printf("Img problemo");
                return 2;
            }
        }

        if(jpgno > 0)
        {
            //write 512 byttes of buffer to jpg
            fwrite(buffer,512,1,img);
        }
    }
    free(card);
}

You must log in to answer this question.

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