I am currently working with recover and when I first compiled my program with debug50, my program was able to return 6 corrupted files without a propre name. When I wanted to get a closer look as to why this happened, it turns out buffer keeps the same first 4 bytes throughout the whole program. After wanting to test my program 1 more time, thevalues ofthe first 4 bytes changed and stayed that way no matter how many times I recompiled and ran my program. Any ideas as to why this happens?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
//checking if a file is given
if(argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
char *image = argv[1];
FILE *JPG = fopen(image, "r");
//checking if file could be opened
if (JPG == NULL)
{
printf("Could not open %s.\n", image);
return 2;
}
//declaring the variables
int n = 000;
FILE* img = NULL;
int x = 512;
BYTE buffer[512];
while (1 == 1)
{
//detecting the start of a new JPEG
fread(buffer, 1, 512, JPG);
int a = buffer[0];
int b = buffer[1];
int c = buffer[2];
int d = buffer[3];
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
char filename[8];
sprintf(filename, "%03i.jpg", n);
img = fopen(image, "w");
while (1 == 1)
{
//writing until new file is found
fread(buffer, 1, 512, JPG);
x = fread(buffer, 1, 512, JPG);
a = buffer[0];
b = buffer[1];
c = buffer[2];
d = buffer[3];
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
break;
}
//ending when end of file reached
if (x != 512)
{
return 0;
}
fwrite(buffer, 1, 512, img);
}
//closing the file
fclose(img);
n++;
}
}
}