-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
280 lines (234 loc) · 8.06 KB
/
main.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
#include <experimental/filesystem> // std::experimental::filesystem, kas leidžia gauti programos vardą be kelio ir plėtinio
#include <Windows.h>
#include "compression.h"
#include <cstdio>
#include <fstream>
#include <string>
#include <chrono> // skaiciuoti laika
#include <cstdlib> // exit, EXIT_FAILURE
#include <random>
using std::cout;
using std::endl;
using std::cin;
using std::string;
static void naudojimo_instrukcija(string ProgramName)
{
cout << "Failo suspaudimas, naudojimas:\n\n";
cout << " " << ProgramName << " compress failo_pav suspausto_failo_pav [/fast | /full] [/encrypt]\n";
cout << " " << ProgramName << " decompress suspausto_failo_pav išskleisto_failo_pav\n";
cout << " \n";
cout << " Daugiau info: " << ProgramName << " /?\n\n";
}
static void issami_instrukcija(string ProgramName)
{
cout << "Naudojimas:\n\n";
cout << " " << ProgramName << " compress failo_pav suspausto_failo_pav [/fast | /full] [/encrypt]\n";
cout << " " << ProgramName << " decompress suspausto_failo_pav išskleisto_failo_pav\n";
cout << " \n";
cout << " Failo suspaudimo programa su galimybe spaudžiamą failą užšifruoti: /encrypt\n";
cout << " Papildomai galima nurodyti suspaudimo lygį - /full arba /fast:\n";
cout << " /full (numatytasis) suspaudimo lygis suspaudžia failą geriau nei /fast, bet\n";
cout << " spaudimas/išskleidimas vyksta atitinkamai lėčiau\n";
cout << " Išskleidžiant failą nereikia nurodyti failo suspaudimo lygį\n";
cout << " \n";
cout << " Vietoj compress/decompress galima atitinkamai naudoti -/+, pvz:\n";
cout << " " << ProgramName << " - pavyzdys.txt pavyzdys.cmp\n";
cout << " " << ProgramName << " + pavyzdys.cmp pavyzdys.out\n";
cout << " pavyzdys.txt failas bus suspaustas /full lygiu į pavyzdys.cmp failą\n";
cout << " ir vėl išskleistas į pavyzdys.out failą\n";
cout << " pavyzdys.txt failas bus identiškas pavyzdys.out failui\n";
cout << " \n";
}
struct fileContents
{
s64 size;
u8* memory;
};
static fileContents readEntireFileToMemory(const char* fileName)
{
std::ifstream file(fileName, std::fstream::binary | std::fstream::in);
if (!file.is_open())
{
cout << "Duotas failas " << fileName << " nerastas arba nėra privilegijų jo atidaryti.";
exit(EXIT_FAILURE);
}
file.seekg(0, file.end);
std::streampos file_size = file.tellg();
file.seekg(0, file.beg);
fileContents result;
result.memory = (u8*) malloc((std::size_t)file_size);
file.read((char*)result.memory, file_size);
result.size = file_size;
file.close();
return result;
}
// kad nesimatytu kai vedamas password
void disableConsoleOutput(bool disable = true)
{
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
if (disable) mode &= ~ENABLE_ECHO_INPUT;
else mode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hStdin, mode);
}
const u8 BOM = 0b01010100;
static void xor_buffer(u8* inBuffer, s64 size, std::mt19937_64 & cipher)
{
u64* buffer = (u64*)inBuffer;
for (s64 i = 0; i < size / 8; ++i)
{
*buffer++ ^= cipher();
}
u64 last_cipher = cipher();
inBuffer = (u8*)buffer;
size = size % 8;
for (s64 i = 0; i < size; ++i)
{
*inBuffer++ ^= *(((u8*)(&last_cipher)) + i);
}
}
static std::mt19937_64 requestPassword()
{
string password;
cout << "Įveskite slaptažodį: ";
disableConsoleOutput(true);
std::getline(cin, password);
disableConsoleOutput(false);
cout << "\n";
std::seed_seq seed(password.begin(), password.end());
std::mt19937_64 cipher(seed);
return cipher;
}
int main(int argCount, char** args)
{
setlocale(LC_ALL, "Lithuanian");
namespace fs = std::experimental::filesystem;
fs::path Program = fs::path(args[0]);
Program.replace_extension(); // pasalina .exe extension
string ProgramName = Program.filename().string();
if (argCount == 2)
{
if (strcmp(args[1], "/?") == 0)
{
issami_instrukcija(ProgramName);
exit(EXIT_FAILURE);
}
else naudojimo_instrukcija(ProgramName);
}
else if (argCount == 4)
{
char* command = args[1];
char* inFileName = args[2];
char* outFileName = args[3];
filenames files = { inFileName, outFileName };
fileContents inFile = readEntireFileToMemory(inFileName);
fileContents outFile;
if (strcmp(command, "compress") == 0 || strcmp(command, "-") == 0)
{
outFile.size = inFile.size * 2;
outFile.memory = (u8*)malloc((std::size_t)outFile.size);
s64 byte_pos = 0;
writeByte(BOM, outFile.memory, byte_pos, 0);
writeFourBytes((u32)inFile.size, outFile.memory, byte_pos, 0);
getTimeElapsed();
s64 compressed_size = compress(inFile.memory, inFile.size, outFile.memory + 5, outFile.size - 5, files);
s64 outFile_final_size = compressed_size + 5;
std::ofstream file(outFileName, std::fstream::binary | std::fstream::out);
file.write((char*)outFile.memory, outFile_final_size);
file.close();
auto end_time = getTimeElapsed();
// spausdinti kiek laiko praejo
cout << "Užtruko " << std::setprecision(2) << end_time / 1000.0f << " sekundes" << endl;
}
else if (strcmp(command, "decompress") == 0 || strcmp(command, "+") == 0)
{
s64 byte_pos = 0;
u8 first_byte = readByte(inFile.memory, byte_pos, 0);
if (first_byte != BOM && first_byte != BOM + 1)
{
cout << "Duotas failas " << inFileName << " nebuvo suspaustas su šia programa\nNeįmanoma jo išskleisti";
exit(EXIT_FAILURE);
}
bool encrypted = false;
if (first_byte == BOM + 1)
{
std::mt19937_64 cipher = requestPassword();
xor_buffer(inFile.memory + 1, inFile.size - 1, cipher);
u8 second_byte = readByte(inFile.memory, byte_pos, 0);
if (second_byte != BOM)
{
cout << "Neteisingas slaptažodis";
exit(EXIT_FAILURE);
}
encrypted = true;
}
outFile.size = readFourBytes(inFile.memory, byte_pos, 0);
outFile.memory = (u8*)malloc((std::size_t)outFile.size);
getTimeElapsed();
s64 decompressed_size = decompress(inFile.memory +(encrypted ? 6: 5), inFile.size - (encrypted ? 6 : 5), outFile.memory, outFile.size, files);
std::ofstream file(outFileName, std::fstream::binary | std::fstream::out);
file.write((char*)outFile.memory, decompressed_size);
file.close();
auto end_time = getTimeElapsed();
// spausdinti kiek laiko praejo
cout << "Užtruko " << std::setprecision(2) << end_time / 1000.0f << " sekundes" << endl;
}
else
{
naudojimo_instrukcija(ProgramName);
exit(EXIT_FAILURE);
}
}
else if (argCount == 5)
{
char* command = args[1];
char* encrypt = args[4];
char* inFileName = args[2];
char* outFileName = args[3];
if (strcmp(encrypt, "/encrypt") != 0)
{
naudojimo_instrukcija(ProgramName);
exit(EXIT_FAILURE);
}
filenames files = { inFileName, outFileName };
fileContents inFile = readEntireFileToMemory(inFileName);
fileContents outFile;
if (strcmp(command, "compress") == 0 || strcmp(command, "-") == 0)
{
outFile.size = inFile.size * 2;
outFile.memory = (u8*)memset(malloc((std::size_t)outFile.size), 0, (std::size_t)outFile.size);
s64 byte_pos = 0;
writeByte(BOM + 1, outFile.memory, byte_pos, 0);
writeByte(BOM, outFile.memory, byte_pos, 0);
writeFourBytes((u32)inFile.size, outFile.memory, byte_pos, 0);
std::mt19937_64 cipher = requestPassword();
getTimeElapsed();
s64 compressed_size = compress(inFile.memory, inFile.size, outFile.memory + 6, outFile.size - 6, files);
s64 outFile_final_size = compressed_size + 6;
xor_buffer(outFile.memory + 1, outFile_final_size - 1, cipher);
std::ofstream file(outFileName, std::fstream::binary | std::fstream::out);
file.write((char*)outFile.memory, outFile_final_size);
file.close();
auto end_time = getTimeElapsed();
// spausdinti kiek laiko praejo
cout << "Užtruko " << std::setprecision(2) << end_time / 1000.0f << " sekundes" << endl;
}
else if (strcmp(command, "decompress") == 0 || strcmp(command, "+") == 0)
{
cout << "Ar norėjot suspausti " << inFileName << " failą ? " << " su /encrypt funkcija failo išskleisti negalima";
exit(EXIT_FAILURE);
}
else
{
naudojimo_instrukcija(ProgramName);
exit(EXIT_FAILURE);
}
}
else
{
naudojimo_instrukcija(ProgramName);
exit(EXIT_FAILURE);
}
system("pause");
}