0

This is what I've created to solve recover. When I run it, the IDE gives an error that File size limit exceeded. Please check this code and tell me what part of it is wrong. What I am exactly doing: 1.Finding the starting point of the jpg in the raw file because I know after that the jpg will be contiguous. 2.Set the file pointer to four bytes back so that the jpg header gets written in the next file. 3. Keep writing blocks of 512 B until another jpg header is encountered. 4.If header is encountered, the loops starts again.

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

static char check_pri[] = {0xff, 0xd8, 0xff};
static char check_sec[] = {0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,  0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef};


bool check(char* dat);

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

    long count = 0;

    while(!feof(data)){

        char* temp = malloc(4);
        fread(temp, 1, 4, data);

        if(check(temp)){
            count = ftell(data);
            free(temp); 
            break;
        }
    }

    for (int i = 0; !feof(data); i++){

        fseek(data, count - 4, SEEK_SET);

        char* init = malloc(4);
        fread(init, 1, 4, data);
        char name[10];
        sprintf(name, "%03d.jpg", i+ 1);
        FILE* pic = fopen(name, "w");


        while (check(init)){
            char* temp = malloc(512);
            fread(temp, 512, 1, data);
            fwrite(temp, 512, 1, pic);
            fread(init, 1, 4, data);
            free(temp);
        } 

        while (!check(init)){
            char* temp = malloc(512);
            fread(temp, 512, 1, data);
            fwrite(temp, 512, 1, pic);
            fread(init, 1, 4, data);
            free(temp);
        }

        fclose(pic);

        count = ftell(data);
    }

    fclose(data);

    return 0;
}

bool check(char* dat){  //check whether the chunk of data is the same jpg or a new one

int check = 1;

for(int i = 0; i < 3; i++){

    if((dat[i] = check_pri[i])){
        check = 0;
    }
    else{
        check = 1;
        break;
    }
}

if(check == 0 ){
    for(int j = 0; j < 16; j++){
        if((dat[3] = check_sec[j])){
            return true;
        }
    }
}


return false;

} 
2

There are a lot of problems in this code. If you're getting a file size limit exceeded error, you almost certainly have an infinite loop. The reason is that you have while loops that do the reads but never check for EOF. Next, the code reads 4 bytes and then 512 bytes from the input file. In other words, it is reading 516 bytes on each pass, which gets everything out of synch. It needs to read 512 bytes and check the first 4 bytes.

All of the malloc and free calls are essentially unnecessary. Instead, a single 512 byte buffer can be allocated on the stack or on the heap at the beginning of the program and reused over and over.

There may well be other issues, but this will get you going. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I tried the same feof logic over some .txt files; it works fine with them. I also tried another version of code in which I am only reading 4 bytes at a time and writing them accordingly but the error persists.
    – hotessy
    May 6 '16 at 12:19

You must log in to answer this question.

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