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

What am I missing? What is this stuff I am reading out? Where are the jpeg's?


I figured it out. By using fread(bytes, 512, 1, file); as the while loop condition, I was able to move further into creating this problem, and eventually solve it.

You must log in to answer this question.

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