summaryrefslogtreecommitdiff
path: root/wk4/pset/recover/recover.c
blob: 13917669b7dc38c06e66130c620bd423ab845883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("Usage: ./recover infile\n");
        return 1;
    }

    // Open input file
    FILE *card = fopen(argv[1], "r");
    if (card == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 1;
    }

    // create a buffer to store a block of data
    uint8_t buffer[512];
    int files = -1;
    // int blocks = (int)ceil(sizeof(infile)/512.0);
    while(fread(buffer, 1, 512, card) == 512)
    {
        // create new files from data
        if (
            buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            buffer[3] >= 0xe0 &&
            buffer[3] <= 0xef
        )
        {
            // this is a jpeg, make a new file from it
            files++;
            char name[4];
            sprintf(name, "%i%i%i", files / 100, (files / 10) % 10, (files % 10) % 10);
            fopen()
        }
    }

    /*
    files = 0
    for (file length)
    {
        if (first 3 bytes are 0xff 0xd8 0xff AND 4th byte is 0xe* (* denotes anything))
        fread(ptr, )
        {
            this is a photo; remember the pointer (call it point)
            int photolength = 0;
            // look through the blocks ahead of this one for a new photo
            for (file length left)
            {
                photolength++;
                if (first 3 bytes are 0xff 0xd8 0xff AND 4th byte is 0xe* (* denotes anything))
                {
                    // this is the photo length
                    break
                }
            }
            filename = files.jpg
            files++
            Open output file with name (filename)

            // make buffer space to store the data
            uint32_t data[photolength][16];
            // read the photo to data in blocks of 32 bits
            fread(&data, sizeof(uint8_t), 16 * photolength, card);
            // write the photo from data
            fwrite(&data, sizeof(uint8_t), 16 * photolength, point);
            // Close the photo file
            // repeat this process for other photos
        }
    }





    */

   fclose(card);
}