-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.cpp
More file actions
219 lines (183 loc) · 5.17 KB
/
decoder.cpp
File metadata and controls
219 lines (183 loc) · 5.17 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#define DSIZE 3122 //데이터 파일 크기
#define NSIZE 182 //일반 호프만 테이블 크기
#define CSIZE 1724 //Adaptive 호프만 테이블 크기
using namespace std;
unsigned char binaryToChar(string binary); //2진수를 문자로 변환하는 함수
void processData(string& data, int size); //데이터 전처리 함수
void readData(string& nTable, string& cTable, string& data); //데이터를 읽어오는 함수
void processTable(string& nTable, vector<string>& table, string& cTable, vector<vector<string>>& adaptiveTable); //테이블 불러오는 함수
void decode(string& data, vector<string>& table, vector<vector<string>>& adaptiveTable); // 압축 해제
int main() {
string data; //데이터
string nTable; //테이블 데이터
string cTable; //Adaptive 테이블 데이터
vector<string> table(128); //테이블
vector<vector<string>> adaptiveTable(128); //Adaptive 테이블
readData(nTable, cTable, data);
processData(data, DSIZE);
processData(nTable, NSIZE);
processData(cTable, CSIZE);
processTable(nTable, table, cTable, adaptiveTable);
decode(data, table, adaptiveTable);
return 0;
}
void readData(string& nTable, string& cTable, string& data) {
ifstream dataFile("encoded.hbs", ios::in | ios::binary); //데이터 불러오기
ifstream nTableFile("HuffmanTable.hbs", ios::in | ios::binary); //테이블 데이터
ifstream cTableFile("AdaptiveHuffmanTable.hbs", ios::in | ios::binary); //Adaptive 테이블 데이터
if (nTableFile.is_open() && cTableFile.is_open() && dataFile.is_open()) {
data.resize(DSIZE);
nTable.resize(NSIZE);
cTable.resize(CSIZE);
dataFile.read(&data[0], DSIZE);
nTableFile.read(&nTable[0], NSIZE);
cTableFile.read(&cTable[0], CSIZE);
}
dataFile.close();
nTableFile.close();
cTableFile.close();
return;
}
void processData(string& data ,int size) { //비트 단위로 파일 표시
string tmp;
for (int i = 0; i < size; i++) {
for (int j = 7; j >= 0; j--) {
if ((data[i] >> j) & 1) tmp += '1';
else tmp += '0';
}
}
data = tmp;
return;
}
void decode(string& data, vector<string>& table, vector<vector<string>>& adaptiveTable) {
ofstream decodeFile("output.txt", ios::out);
string originalText;
string code;
code.resize(0);
int eod = 0;
int tmp = 0;
int idx = 0;
int flag = 0;
for (int i = 0; i < 13; i++) {
code += data[i];
idx++;
for (int j = 0; j < 128; j++) {// 일반 호프만 부호로 되어있는 첫번째 변환
if (table[j] == code) {
tmp = j;
break;
}
}
if (table[tmp] == code) {
break;
}
}
code.clear();
originalText += (char)tmp;
while (true) {
for (int i = 0; i < 13; i++, idx++) {
if(idx < data.size()) code.push_back(data.at(idx)); //code 를 통해 Adaptive 테이블로 변환
for (int j = 0; j < 128; j++) {
if (code == adaptiveTable[tmp][j]) {
flag = 1;
if (j == 3) { // EOD 판별
eod = 1;
break;
}
idx++;
tmp = j;
originalText += (char)tmp;
break;
}
}
if (flag || eod) break;
}
if (eod) break;
flag = 0;
code.clear();
}
decodeFile << originalText;
decodeFile.close();
}
void processTable( string& nTable, vector<string>& table, string& cTable, vector<vector<string>>& adaptiveTable) {
for (int i = 0; i < nTable.length();) {//일반 테이블
int len, Ascii;
string ascii;
string clen;
string code;
for (int j = 0; j < 8; i++, j++) { // ascii 데이터 read
if (i < nTable.length())
ascii += nTable[i];
}
for (int j = 0; j < 8; i++, j++) { // 코드 길이 데이터 read
if (i < nTable.length())
clen += nTable[i];
}
Ascii = binaryToChar(ascii);
len = binaryToChar(clen);
for (int j = 0; j < len; i++, j++) { // 코드 데이터 read
if (i < nTable.length()) code += nTable[i];
}
if (i < nTable.length()) table[Ascii] = code;
else break;
}
int num;
int count = 0;
string cont;
for (int i = 0; i < 8; i++) cont += cTable[i], count++; // Adaptive 테이블
num = binaryToChar(cont);
for (int i = 0; i < num; i++) {
string cnt;
for (int j = count; j < count + 8 && j < cTable.length(); j++) { //테이블 크기 판별
cnt += cTable[j];
}
count += 8;
int tmp = binaryToChar(cnt);
adaptiveTable[tmp].resize(128);
for (int j = count; j < cTable.length();) {
int len, Ascii;
string ascii;
string clen;
string code;
string EOD;
for (int k = 0; k < 13; k++) { // EOD 판별
if (j < cTable.length()) {
EOD += cTable[j + k];
}
}
if (EOD == table[3]) {
count += 13;
break;
}
for (int k = 0; k < 8; j++, k++) { // ascii 데이터 read
if (j < cTable.length())
ascii += cTable[j];
count++;
}
for (int k = 0; k < 8; j++, k++) { // 코드 길이 데이터 read
if (j < cTable.length())
clen += cTable[j];
count++;
}
Ascii = binaryToChar(ascii);
len = binaryToChar(clen);
for (int k = 0; k < len; j++, k++) { // 코드 데이터 read
if (j < cTable.length()) code += cTable[j];
count++;
}
if (j < cTable.length()) adaptiveTable[tmp][Ascii] = code;
else break;
}
}
return;
}
unsigned char binaryToChar(string binary) { // 2진수를 문자열로 변환
unsigned char result = 0;
for (int i = 0; i < binary.length(); i++) {
if (binary[i] == '1') result += (int)pow(2, binary.length() - i - 1);
}
return result;
}