I'm struggling with the recover problem. I'm running a while loop that's more than 7 thousand iterations long. I get a segmentation fault because (i'm pretty sure) of the last or perhaps the last but one iteration. I would like to debug that part. I do not know how to skip the non-relevant iterations, I don't even know if there is a way to do that with debug50.
I could press 'next' 7312 times, but life is short.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
FILE *pf;
FILE *img;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
}
BYTE buffer[512];
pf = fopen(argv[1], "r");
int ct = 0;
char fname[8];
while(fread(buffer, 512, 1, pf) || (!feof(pf)))
{
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
if (img != NULL)
{
fclose(img);
ct += 1;
}
sprintf(fname,"%03i.jpg", ct);
img = fopen(fname,"w");
fwrite(buffer, sizeof(buffer), 1, img);
}
else
{
if (img != NULL)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
}
}
fclose(pf);
fclose(img);
return 0;
}
I used sizeof(buffer) thinking it would handle the last buffer which is supposedly less than 512 bytes. What is strange is that "card.raw" is 3,744,768 bytes long which is exactly (512x7314).
If I delete the feof condition, the code works for the first 49 pics, so I'm missing the last one which in the last batch.
If I try (feof(pf) != 0)) I also get 49 pics with the message "File size limit exceeded", but no segmentation fault.
I also tried this and got the exact same result:
int x = 512;
while(fread(buffer, x, 1, pf) || (feof(pf) != 0))
{
if (sizeof(buffer) < 512)
x = sizeof(buffer);
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
if (img != NULL)
{
fclose(img);
ct += 1;
}
sprintf(fname,"%03i.jpg", ct);
img = fopen(fname,"w");
fwrite(buffer, x, 1, img);
}
else
{
if (img != NULL)
{
fwrite(buffer, x, 1, img);
}
}
}
fclose(pf);
fclose(img);
return 0;
}