-
Notifications
You must be signed in to change notification settings - Fork 0
/
progtest1.cpp
367 lines (309 loc) · 9.11 KB
/
progtest1.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#ifndef __PROGTEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <climits>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <memory>
#include <functional>
#include <stdexcept>
using namespace std;
#endif /* __PROGTEST__ */
#define DEBUG_LEVEL 0
#if DEBUG_LEVEL >= 1
#define DEBUG_PRINT(...) printf (__VA_ARGS__)
#else
#define DEBUG_PRINT(...)
#endif
#define INVALID_VALUE 255
class CIO {
public:
static bool getBit (bool & bit);
static bool getByte (uint8_t & byte);
static bool getUTF8 (uint32_t & byte, int & size);
static bool init (const char * inFileName);
static bool close ();
private:
static uint8_t m_Byte;
static int m_Position;
static ifstream m_Fin;
};
uint8_t CIO::m_Byte;
int CIO::m_Position;
ifstream CIO::m_Fin;
class CNode {
public:
CNode * m_Right;
CNode * m_Left;
uint32_t m_Value;
int m_Size;
CNode ()
: m_Right (nullptr),
m_Left (nullptr),
m_Value (INVALID_VALUE)
{}
~CNode (){
delete m_Right;
delete m_Left;
}
};
bool CIO::init (const char * inFileName)
{
m_Fin.open (inFileName, ios::binary | ios::in);
if (!(m_Fin.good () && m_Fin.is_open ())){
return false;
}
m_Position = 0;
DEBUG_PRINT ("all is good\n");
return true;
}
bool CIO::close ()
{
m_Fin.get ();
m_Fin.close ();
return m_Fin.eof ();
}
bool CIO::getBit (bool & bit)
{
if (m_Position == 0){
m_Byte = m_Fin.get ();
if (m_Fin.eof ()){
return false;
}
m_Position = 8;
}
bit = (m_Byte & (1 << --m_Position ));
return true;
}
bool CIO::getByte (uint8_t & byte)
{
bool bit;
uint8_t tmp = 0;
for (int j = 7; j >= 0 ; j--){
if (getBit (bit)){
tmp |= (bit << j);
}
else{
return false;
}
}
DEBUG_PRINT ("BYTE\n");
byte = tmp;
return true;
}
bool CIO::getUTF8 (uint32_t & symbol, int & size)
{
uint8_t byte;
uint32_t tmp_symbol;
int tmp_size;
if(! CIO::getByte (byte)){
return false;
}
int counter = 0;
for( int i = 7; i >= 3; i-- ){
bool bit = byte & (1<<i);
if(!bit){
break;
}
++counter;
}
if( counter == 5 || counter == 1){
return false;
}
tmp_symbol = ((uint32_t) byte);
tmp_size = counter == 0 ? 1: counter;
for(int bleb = 0; bleb < (counter-1); bleb++){
uint8_t finger;
if(! CIO::getByte (finger) ){
DEBUG_PRINT("FAILED CHECK finger ===> %02x \n", finger);
return false;
}
if(((0b11000000 & finger) ^ 0b10000000)){
return false;
}
tmp_symbol |= finger << (8 + bleb * 8);
}
DEBUG_PRINT("TMP SYMBOL ===> %02x \n", tmp_symbol);
symbol = tmp_symbol;
size = tmp_size;
return true;
}
bool getSymbol (CNode * root, uint32_t & symbol, int & size)
{
bool bit;
if (!root){
return false;
}
DEBUG_PRINT ("> ROOT VALUE = %02x\n", root->m_Value);
if (root->m_Value != INVALID_VALUE){
symbol = root->m_Value;
size = root->m_Size;
DEBUG_PRINT (" => %c\n", symbol);
return true;
}
if (!(CIO::getBit (bit))){
return false;
}
DEBUG_PRINT (" + %d\n", (int)bit);
if (bit == 0){
return getSymbol (root->m_Left, symbol, size);
}
else{
return getSymbol (root->m_Right, symbol, size);
}
}
bool parseTree (CNode ** n)
{
bool bit;
if (!(CIO::getBit (bit))){
return false;
}
DEBUG_PRINT("BIT\n");
* n = new CNode ();
if (bit == 0){
return (parseTree (& ( * n )->m_Left ) && parseTree (& ( * n )->m_Right ));
}
return CIO::getUTF8 ( ( * n )->m_Value, ( * n )->m_Size);
}
bool decompressFile (const char * inFileName, const char * outFileName)
{
ofstream fout (outFileName, ios::binary);
if (!(fout.is_open ())){
DEBUG_PRINT ("SAAAD COULDNT OPEN %s\n", outFileName);
return false;
}
CIO::init (inFileName);
CNode * root = nullptr;
if (!(parseTree ( & root )) ){
DEBUG_PRINT ("FUK TREE\n");
delete root;
CIO::close ();
return false;
}
DEBUG_PRINT ("PODTIM\n");
bool flag, bit;
uint32_t symbol;
int size;
int remaining;
int times;
while (CIO::getBit (flag)){
remaining = 0;
if (flag == 0 ){
for (int j = 11; j >= 0 ; j--){
if (!(CIO::getBit (bit))){
DEBUG_PRINT ("DID NOT READ 12\n");
delete root;
fout.close();
CIO::close ();
return false;
}
remaining |= (bit << j);
}
}
times = flag * 4096 + (!flag) * remaining;
DEBUG_PRINT ("times %d \n", times);
for (int i = 0; i < times ; i++){
DEBUG_PRINT ( "===========\n" );
if (!(getSymbol (root, symbol, size ))){
DEBUG_PRINT ("WHERE IS CHAR\n");
delete root;
CIO::close ();
fout.close();
return false;
}
DEBUG_PRINT ("COUT WRITE __________\n");
DEBUG_PRINT ("symbol %02x\n", symbol);
DEBUG_PRINT ("size %02x\n", size);
DEBUG_PRINT ("Somehitng %02x\n",(char *) & symbol);
fout.write ((char *) & symbol, size);
if (fout.bad() && !fout.eof()){
delete root;
fout.close();
CIO::close ();
return false;
}
}
if (flag==0){
break;
}
}
if( flag != 0){
delete root;
fout.close();
CIO::close ();
return false;
}
delete root;
fout.close();
return CIO::close ();
}
bool compressFile (const char * inFileName, const char * outFileName)
{
// keep this dummy implementation (no bonus) or implement the compression (bonus)
return false;
}
#ifndef __PROGTEST__
bool identicalFiles (const char * fileName1, const char * fileName2)
{
ifstream fin1 (fileName1, ios::binary );
ifstream fin2 (fileName2, ios::binary );
if(!(fin1.is_open() && fin2.is_open())){
return false;
}
while (fin1.get() == fin2.get()){
if (fin1.eof() || fin1.eof()){
break;
}
}
fin1.close();
fin2.close();
return fin2.eof() && fin1.eof();
}
int main ( void )
{
assert ( identicalFiles ( "tests/test0.orig", "tests/test0.orig" ) );
assert ( decompressFile ( "tests/test0.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/test0.orig", "tempfile" ) );
assert ( decompressFile ( "tests/test1.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/test1.orig", "tempfile" ) );
assert ( decompressFile ( "tests/test2.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/test2.orig", "tempfile" ) );
assert ( decompressFile ( "tests/test3.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/test3.orig", "tempfile" ) );
assert ( decompressFile ( "tests/test4.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/test4.orig", "tempfile" ) );
assert ( ! decompressFile ( "tests/test5.huf", "tempfile" ) );
assert ( decompressFile ( "tests/extra0.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra0.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra1.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra1.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra2.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra2.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra3.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra3.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra4.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra4.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra5.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra5.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra6.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra6.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra7.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra7.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra8.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra8.orig", "tempfile" ) );
assert ( decompressFile ( "tests/extra9.huf", "tempfile" ) );
assert ( identicalFiles ( "tests/extra9.orig", "tempfile" ) );
return 0;
}
#endif /* __PROGTEST__ */