forked from erichVK5/BXL2text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceBuffer.cc
335 lines (291 loc) · 8.19 KB
/
SourceBuffer.cc
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SourceBuffer.cc - C++ code for converting Huffman encoded files
// into text, ported to C++ from the vala code by Geert Jordaens
//
// NodeTree - a tree used for the Huffman decoding
// Node - node in the tree used for the Huffman decoding
// SourceBuffer.cc v1.0
// Copyright (C) 2016 Erich S. Heinzle, [email protected]
// see LICENSE-gpl-v2.txt for software license
// see README.txt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// SourceBuffer.cc Copyright (C) 2016 Erich S. Heinzle [email protected]
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class Node {
public:
int level = 0;
Node *parent = 0;
Node *left = 0;
Node *right = 0;
int symbol = -1;
int weight = 0;
~Node () {
}
void deleteChildren(Node *target) {
if (target->left != 0) {
target->deleteChildren(target->left);
}
if (target->right != 0) {
target->deleteChildren(target->right);
}
delete target;
}
Node () {
//this.level = 0;
}
Node (Node *parent, int symbol) {
if (parent != 0) {
this->parent = parent;
this->level = parent->level+1;
} else {
this->level = 0;
}
if (level > 7) {
this->symbol = symbol;
}
}
Node* add_child(int symbol){
Node *ret = 0;
if (level < 7) {
if (right != 0) {
ret = right->add_child(symbol);
if (ret != 0) return ret;
}
if (left != 0) {
ret = left->add_child(symbol);
if (ret != 0) return ret;
}
if (right == 0) { // first fill right branch
right = new Node (this, -1);
return right;
}
if (left == 0) {
left = new Node (this, -1);
if (left !=0) return left;
}
return 0;
} else {
if (right == 0) {
right = new Node (this, symbol);
return right;
} else if (left == 0) {
left = new Node (this, symbol);
return left;
} else {
return 0; // Leaves are filled
}
}
}
bool is_leaf() {
return (level > 7);
}
Node* sibling(Node *node){
if (node != right) {
return right;
} else {
return left;
}
}
void incrementWeight() {
this->weight++;
}
bool need_swapping() {
if (parent != 0 &&
parent->parent != 0 && // root node
this->weight > parent->weight) {
return true;
}
return false;
}
};
class NodeTree {
public:
Node *root = 0;
~NodeTree() {
if (root != 0) {
root->deleteChildren(root->left);
root->deleteChildren(root->right);
delete root;
root = 0;
}
}
NodeTree() {
// create root node
Node *node = new Node(0, 0);
root = node;
int leaf_count = 0;
// fill levels
while(node != 0) {
node = root->add_child(leaf_count);
if(node != 0 && node->is_leaf()) { leaf_count++; }
}
}
Node* getRoot() {
return root;
}
void cleanUp() {
if (root != 0) {
root->deleteChildren(root->left);
root->deleteChildren(root->right);
delete root;
root = 0;
}
}
void swap (Node *n1, Node *n2, Node *n3) {
if (n3 != 0) { n3->parent = n1;}
if (n1->right == n2) { n1->right = n3; return; }
if (n1->left == n2) { n1->left = n3; return; }
}
void update_tree(Node *current) {
if (current != 0 && current->need_swapping()) {
Node *parent = current->parent;
Node *grand_parent = parent->parent;
Node *parent_sibling = grand_parent->sibling(parent);
swap(grand_parent, parent, current);
swap(grand_parent, parent_sibling, parent);
swap(parent, current, parent_sibling);
parent->weight = parent->right->weight + parent->left->weight;
grand_parent->weight = current->weight + parent->weight;
update_tree(parent);
update_tree(grand_parent);
update_tree(current);
}
}
};
typedef unsigned char BYTE;
// Get the size of a file
long getFileSize(FILE *file) {
long lCurPos, lEndPos;
lCurPos = ftell(file);
fseek(file, 0, 2);
lEndPos = ftell(file);
fseek(file, lCurPos, 0);
return lEndPos;
}
class SourceBuffer {
private:
BYTE *source_buffer = 0;
int source_index = 4;
int source_char = 0;
long fileSize = 0;
int bit = 0;
public:
~SourceBuffer() {
if (source_buffer != 0) {
delete [] source_buffer;
source_buffer = 0;
}
}
SourceBuffer(char *filename) {
FILE *file = 0; // File pointer
// Open the file in binary mode using the "rb" format string
// checks if file exists and/or can be opened correctly
if ((file = fopen(filename, "rb")) == 0)
cout << "Couldn't open file" << filename << endl;
else
cout << filename << " opened successfully" << endl;
// Get size of file in bytes
fileSize = getFileSize(file);
// Allocate space in the buffer for the whole file
source_buffer = new BYTE[fileSize];
// Read the file in to the buffer
fread(source_buffer, fileSize, 1, file);
fclose(file);
}
int read_next_bit() {
int result = 0;
if (bit < 0) {
// Fetch next byte from source_buffer
bit = 7;
source_char = (int)source_buffer[source_index];
result = source_char & (1 << bit);
source_index++;
} else {
result = source_char & (1 << bit);
}
bit--;
return result;
}
int uncompressed_size() {
/* Uncompressed size =
B0b7 * 1<<0 + B0b6 * 1<<1 + ... + B0b0 * 1<<7 +
B1b7 * 1<<0 + B1b6 * 1<<1 + ... + B2b0 * 1<<7 +
B2b7 * 1<<0 + B2b6 * 1<<1 + ... + B3b0 * 1<<7 +
B3b7 * 1<<0 + B3b6 * 1<<1 + ... + B4b0 * 1<<7
*/
int size = 0;
int mask = 0;
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[0] & (1 << i)) != 0) {
size |= (1 << mask);
}
mask++;
}
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[1] & (1 << i)) != 0) {
size |= (1<<mask);
}
mask++;
}
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[2] & (1 << i)) != 0) {
size |= (1<<mask);
}
mask++;
}
for (int i = 7 ; i >=0 ; i--) {
if ((source_buffer[3] & (1 << i)) != 0) {
size |= (1<<mask);
}
mask++;
}
return size;
}
string decode() {
NodeTree *tree = new NodeTree();
int out_file_length = uncompressed_size();
string sb = "";
while (source_index < fileSize) {
Node *node = tree->getRoot();
while (!node->is_leaf()) {
// find leaf node
if (read_next_bit() != 0) {
node = node->left;
} else {
node = node->right;
}
}
sb = sb + (char)(node->symbol);
node->incrementWeight();
tree->update_tree(node);
}
tree->cleanUp();
delete tree;
tree = 0;
return sb;
}
};
int main() {
string filename = "LM339_N_14.bxl"; //"MKL27Z256VFM4.bxl";
char* fn = (char*)filename.c_str();
SourceBuffer *sb = new SourceBuffer(fn);
cout << sb->decode() << endl;
delete sb;
return 0;
}