This is my humble, little code. The comments shows my though process:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
return 2;
}
unsigned char bytes[512]; // since we're writing 512 bytes at the time
char ch;
while((ch = fgetc(file)) != EOF) // Loop through file as long as we dont meet EOF
{
fread(bytes, 512, 1, file); // read chunks of 512bytes, place it in array bytes
printf("%s\n", bytes); // Get rick rolled
if(bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (buffer[3] & 0xf0) == 0xe0;)
{
// nothing here, because apparently, by looking through the file
// this way, all you get is a bit of a url to a Rick Astley song
}
}
fclose(file); // close file after failing miserably.
}
Output: 50.ly/surprise
If I put cs in front of 50, and go to that URL, i get rick rolled.
If I change my ch to an int, and run it, it seems like the output is more like machine code / printed noise.
I tried changing the while loop with a for loop:
for (int c = fgetc(file); c != EOF; c = fgetc(file))
{
fread(bytes, 512, 1, file);
printf("%s", bytes);
if(bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff)
{
printf("Nothing gets printed here\n");
}
}
Doing it this way, all I get is noise. I am not able to get that URL by using a for loop.
The noise looks kind of like this:
iB.sȖG#=g߉7|իXmhvaY!t(q' Qj5TQ@[_K3oHKzd[J
<thyƎbt\T`B\KEF
ڇi/a1f{>smdak$#yPI=M`P|{q#Jb1#b4qEa!H^PFG$9_nl8ԻgKsOr
%n8T
@).\X
*MNsp*E
What am I missing? What is this stuff I am reading out? Where are the jpeg's?