-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.cpp
More file actions
200 lines (176 loc) · 5.62 KB
/
decoder.cpp
File metadata and controls
200 lines (176 loc) · 5.62 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <stdexcept>
// Number of bits in a code value
#define code_value_bits 16
// Input a bit
int input_bit(unsigned char* read_bit, unsigned int* bit_len,
FILE* input_file, unsigned int* garbage_bit) {
if ( (*bit_len) == 0 ) {
(*read_bit) = fgetc(input_file);
if (feof(input_file)) {
(*garbage_bit)++;
if ( (*garbage_bit) > 14 ) {
throw std::invalid_argument("Can't decompress");
}
}
(*bit_len) = 8;
}
int t = (*read_bit) & 1;
(*read_bit) >>= 1;
(*bit_len)--;
return t;
}
// Decoding function
void decoder(const char* input_name = "encoded.txt",
const char* output_name = "output.txt") {
unsigned int * alfabet = new unsigned int[256];
for (int i = 0; i < 256; i++) {
alfabet[i] = 0;
}
FILE* input_file = fopen(input_name, "rb"); // Open input file
if (input_file == nullptr) {
throw std::invalid_argument("File not found.");
}
unsigned char col = 0;
unsigned int col_letters = 0;
col = fgetc(input_file);
if (!feof(input_file)) {
col_letters = static_cast<unsigned int>(col);
}
unsigned char character = 0;
// Reading the letters used and their number
for (int i = 0; i < col_letters; i++) {
character = fgetc(input_file);
if (!feof(input_file)) {
fread(reinterpret_cast<char*>(&alfabet[character]),
sizeof(uint16_t), 1, input_file);
} else {
throw std::invalid_argument("Can't decompress file.");
}
}
std::vector<std::pair<char, unsigned int>> arr;
for (int i = 0; i < 256; i++) {
if (alfabet[i] != 0) {
arr.push_back(std::make_pair(static_cast<char>(i), alfabet[i]));
}
}
sort(arr.begin(), arr.end(),
[](const std::pair<char, unsigned int> &l,
const std::pair<char, unsigned int> &r) {
if (l.second != r.second) {
return l.second >= r.second;
}
return l.first < r.first;
});
uint16_t* table = new uint16_t[arr.size() + 2];
table[0] = 0;
table[1] = 1;
for (int i = 0; i < arr.size(); i++) {
unsigned int b = arr[i].second;
for (int j = 0; j < i; j++) {
b += arr[j].second;
}
table[i+2] = b;
}
if (table[arr.size()] > (1 << ((code_value_bits - 2)) - 1)) {
throw std::invalid_argument("Error, too long count.");
}
unsigned int low_v = 0;
unsigned int high_v = ((static_cast<unsigned int>(1)
<< code_value_bits) - 1);
unsigned int delitel = table[arr.size()+1];
unsigned int first_qtr = (high_v + 1) / 4;
unsigned int half = first_qtr * 2;
unsigned int third_qtr = first_qtr * 3;
FILE* output_file = fopen(output_name, "wb +");
unsigned int bit_len = 0;
unsigned char read_bit = 0;
unsigned int garbage_bit = 0;
uint16_t value = 0;
int k = 0;
// Input bits to fill the code value
for (int i = 1; i <= 16; i++) {
k = input_bit(&read_bit, &bit_len, input_file, &garbage_bit);
value = 2 * value + k;
}
unsigned int diff = high_v - low_v + 1;
for (;;) { // Read from input file
unsigned int freq = static_cast<unsigned int>((
(static_cast<unsigned int>(value) - low_v + 1)
* delitel - 1) / diff);
int j;
// Find symbol
for (j = 1; table[j] <= freq; j++) {}
high_v = low_v + table[j] * diff / delitel - 1;
low_v = low_v + table[j - 1] * diff / delitel;
for (;;) {
if (high_v < half) {
} else if (low_v >= half) {
low_v -= half;
high_v -= half;
value -= half;
} else if ((low_v >= first_qtr) && (high_v < third_qtr)) {
low_v -= first_qtr;
high_v -= first_qtr;
value -= first_qtr;
} else {
break;
}
low_v += low_v;
high_v += high_v + 1;
k = 0;
k = input_bit(&read_bit, &bit_len,
input_file, &garbage_bit);
value += value + k;
}
if (j == 1) {
break;
}
// Write this character
fputc(arr[j-2].first, output_file);
diff = high_v - low_v + 1;
}
fclose(input_file);
fclose(output_file);
}
// Checking for file matches
unsigned int checker(const char* before_name = "input.txt",
const char* after_name = "output.txt") {
unsigned int same = 0;
FILE* before_file = fopen(before_name, "r");
FILE* after_file = fopen(after_name, "r");
unsigned char after_l = 0;
unsigned char before_l = 0;
while (!feof(after_file) && !feof(before_file)) {
after_l = fgetc(after_file);
before_l = fgetc(before_file);
if (!feof(after_file) && !feof(before_file)) {
if (after_l != before_l) {
same++;
}
}
}
while (!feof(after_file)) {
after_l = fgetc(after_file);
if (!feof(after_file)) {
same++;
}
}
while (!feof(before_file)) {
before_l = fgetc(before_file);
if (!feof(before_file)) {
same++;
}
}
fclose(after_file);
fclose(before_file);
return same;
}
int main() {
decoder();
std::cout << checker() << std::endl;
}