-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoding.cpp
56 lines (48 loc) · 1.3 KB
/
decoding.cpp
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
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
typedef unsigned char uchar;
int main(int argc, char **argv)
{
if (argc < 3) {
printf("Usage: %s [input] [output]\n", argv[0]);
printf("Example: %s encoding.txt outfile.txt\n", argv[0]);
return 0;
}
FILE *in = fopen(argv[1], "rb");
FILE *out = fopen(argv[2], "wb");
/* decode the bitstream and make the dictionary */
vector<pii> dict;
while (true) {
uchar buf[3];
size_t rc;
rc = fread(buf, sizeof(uchar), 3, in);
if (rc < 3 * sizeof(uchar)) break;
int idx = 0;
idx += buf[0]; idx = (idx << 8);
idx += buf[1]; idx = (idx << 1);
idx += (buf[2] & 0x80) >> 7;
int ch = (buf[2] & 0x7f);
dict.push_back({idx, ch});
}
/* restore the text from the dictionary */
for (int i = 0; i < (int)dict.size(); ++i) {
string phrase;
int j = i;
while (j >= 0) {
phrase.push_back(dict[j].second);
j = dict[j].first - 1;
}
reverse(phrase.begin(), phrase.end());
fwrite(phrase.c_str(), sizeof(char), phrase.size(), out);
}
fclose(in);
fclose(out);
return 0;
}