-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomp1.cpp
More file actions
306 lines (279 loc) · 9.68 KB
/
comp1.cpp
File metadata and controls
306 lines (279 loc) · 9.68 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
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
#include <sstream>
#include <bitset>
#include <chrono>
#include <unordered_set>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <math.h>
#include <exception>
#include <ctime>
using namespace std;
void attach(string bc, vector<string> &dx, vector<string> &cx, unordered_set<string> &bx)
{
string vx = "";
for (uint16_t ex : bc)
{
ex++;
if (ex == 256)
vx.append({1,1});
else
vx.push_back(ex);
}
vx.push_back(1);
bc = vx;
cx.push_back(bc);
uint32_t d = bx.size();
bx.insert(bc);
if (bx.size() > d)
dx.push_back(bc);
}
// Build individual dictionaries
void webster(string temp, vector<string> &dx, vector<string> &cx)
{
unordered_set<string> bx{};
string bc = "";
for (uint32_t i = 0; i < temp.length();)
{
bc = "";
// If the ">" or the ' ' (space) is more than 10 away
// we just take 2 characters from the string
if (temp.find(' ', i) > 6)
{
bc = temp.substr(i, 2);
i += bc.length();
attach(bc, dx, cx, bx);
}
// Elsewise, we use the ' ' (space) and go until
// there isn't one within 5 characters.
// Then we take the next char and we loop again
else if (temp.find('\0', i) < temp.find(' ', i))
{
bc = temp.substr(i, (temp.find('\0', i) - i + 1));
i += bc.length();
attach(bc, dx, cx, bx);
uint32_t t_i = i;
while (temp.find('\0', t_i) - t_i + 1 < 3)
{
string xvx = temp.substr(t_i, (temp.find('\0', t_i) - t_i + 1));
// cout << "n" << flush;
bc = xvx;
t_i += xvx.length();
attach(bc, dx, cx, bx);
}
attach(bc, dx, cx, bx);
i = t_i;
}
else if (temp.find(' ', i) < temp.find('\0', i))
{
bc = temp.substr(i, (temp.find(' ', i) - i + 1));
i += bc.length();
attach(bc, dx, cx, bx);
uint32_t t_i = i;
while (temp.find(' ', t_i) - t_i + 1 < 3)
{
string xvx = temp.substr(t_i, (temp.find(' ', t_i) - t_i + 1));
// cout << "n" << flush;
bc = xvx;
t_i += xvx.length();
attach(bc, dx, cx, bx);
}
//attach(bc, dx, cx, bx);
i = t_i;
}
else
{
cout << "?" << flush;
}
}
}
int main(int argc, char **argv)
{
locale::classic();
printf("\t╔════════════════╗");
printf("\n╟╥╫─────╢ Kcraken v1.0 ║");
printf("\n╟╜\t╟────────────────╢");
printf("\n╟╥╫─────╢David Pulse, Jr.║");
printf("\n\t╚════════════════╝\n\n");
auto start = std::chrono::system_clock::now();
// Start Timer
std::time_t start_time = std::chrono::system_clock::to_time_t(start);
cout << std::ctime(&start_time) << flush;
if (argc < 4)
{
printf("Usage: \n\t%s -c <file input> <output>", argv[0]);
printf("\n\t%s -d <file input> <output>", argv[0]);
return 0;
}
ifstream in_file(argv[2], ios::in | ios::binary);
ofstream out_file(argv[3], ios::out | ios::binary);
uint32_t sizeofall = 0;
stringstream file_in;
uint32_t i = 0, n = 100000;
// load in_file
file_in << in_file.rdbuf();
string temp = file_in.str();
cout << "\nOriginal File Size: "
<< temp.length() << ' ' << flush;
file_in.str("");
uint32_t loop_cnt = 0;
// Make sure that we're going to loop
if (strcmp("-c", argv[1]) == 0)
{
while (temp.length() > n * loop_cnt)
{
// sets up for dictionaries (dx)
// and also for list of what is there (cx)
vector<string> cx{}, dx{};
if (n * loop_cnt + n < temp.length())
{
webster(temp.substr(loop_cnt * n, n), dx, cx);
loop_cnt++;
}
else // last rollout of a dictionary
{
webster(temp.substr(loop_cnt * n), dx, cx);
temp.clear();
cout << cx.size() << flush;
loop_cnt++;
}
if (loop_cnt % 25 == 0 && temp.length() > n)
{
temp = temp.substr(loop_cnt * n + n);
loop_cnt = 0;
}
cout << "." << flush;
uint16_t cx_cnt = 0;
cout << "$" << flush;
{
{
out_file << "%;%" << flush;
sizeofall += 3;
// output the dictionary
// for this round
for (string c : dx)
{
out_file << c.c_str();
sizeofall += sizeof(c.c_str());
}
out_file << "%;%" << flush;
sizeofall += 3;
string len = "";
// go through all of the combinations
// in the file
for (string t : cx)
{
uint32_t y = 0;
for (string b : dx) // compare cx to bx to get y
{
y++;
if (t == b)
{
string vz = "";
while (y > 0) // setup to output
{
y += 2;
uint8_t v = y % 256;
vz += (char)(v);
y >>= 8;
}
if (vz.length() == 1)
vz.push_back((char)(2)); // bumpers
if (vz.length() == 3)
vz.append((char)(2)); //
len.append(vz);
break;
}
}
}
cout << "." << flush;
out_file << len.c_str(); // output
}
}
out_file << "%;%";
}
}
if (strcmp("-d", argv[1]) == 0)
{
while (3 < temp.length())
{
// read in dictionary
if (temp.substr(0, 3) == "%;%")
temp = temp.substr(3);
string dictionary = temp.substr(0, temp.find("%;%"));
string piece = "";
cout << "." << flush;
// make dictionary
vector<string> bx = {};
uint32_t len = 0;
while (len < dictionary.length())
{
if (dictionary.find(">", i) - i + 1 > 6 && dictionary.find(' ', i) - i + 1 > 6)
piece = dictionary.substr(i, 2);
else if (dictionary.find(' ', i) != string::npos)
{
piece = dictionary.substr(i, (dictionary.find(' ', i) - i + 1));
while (dictionary.find(' ', i) - i <= 3)
{
piece += dictionary[i];
i++;
}
piece += dictionary[i];
i++;
}
bx.push_back(piece);
len += piece.length();
}
temp = temp.substr(dictionary.length());
cout << "." << flush;
// read in data
len = 0;
if (temp.substr(0, 3) == "%;%")
{
temp = temp.substr(3);
cout << "TT" << flush;
}
while (temp.substr(0, 3) != "%;%")
{
uint32_t chars = 0;
if (temp.substr(0, 2) == "%&")
{
out_file << " ";
len = 2;
temp = temp.substr(len);
continue;
}
else if (temp.substr(3, 2) == "::")
{
chars = (uint32_t)temp[2] << 0;
chars += (uint32_t)temp[1] << 8;
chars += (uint32_t)temp[0] << 16;
len = 2 + 3;
}
else if (temp.find(';') > 2)
{
chars = (uint32_t)temp[1] << 0;
chars += (uint32_t)temp[0] << 8;
len = 2;
}
else
{
chars = (uint32_t)temp[0];
len = 1 + 1;
}
out_file << bx.at((chars - 1) % bx.size());
temp = temp.substr(len);
}
}
}
cout << "Output Size: " << sizeofall << flush;
// output last of file
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapscurr_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "\nfinishcurr computation at " << std::ctime(&end_time)
<< "elapscurr time: " << elapscurr_seconds.count() << "s\n";
return 0;
}