This repository has been archived by the owner on Jul 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
174 lines (149 loc) · 5.68 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
#include <iostream>
#include <cstdint>
#include <string>
#include "base64.h"
#include "md5.h"
#include <cstring>
#include <fstream>
#include <sys/stat.h>
#include "tea.h"
#include <thread>
#include <mutex>
#define MAX(x,y) (((x)>(y)) ? (x) : (y))
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
#define LOG_FILENAME "encoder.log"
std::mutex mu;
std::string getFileName(const std::string& s, std::string* pathwithoutname) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
if (i != std::string::npos) {
pathwithoutname->clear();
pathwithoutname->append(s.substr(0, i+1));
return s.substr(i+1, s.length() - i);
}
return s;
}
void stringToUpper(std::string* s) {
for (size_t i=0; i<s->length(); ++i) {
char& c = (*s)[i];
if (c>='a' && c<='z') {
c = c-'a'+'A';
}
}
}
bool log(std::ofstream* ofile, const std::string filefolder, const std::string new_fname, const std::string md5_new_fname, const std::string old_fname) {
*ofile << "Encoded from '" << old_fname << "' to '" << new_fname << "'('" << md5_new_fname << "') in '" << filefolder << "'" << std::endl;
}
bool encodeFile(const std::string fpath, const std::string key, std::ofstream* log_file) {
//file reader
struct stat results;
if (stat(fpath.c_str(), &results) != 0) {
mu.lock(); std::cout<< "[Thread: " << std::this_thread::get_id() << "][Error] File not found"<<std::endl; mu.unlock();
return false;
}
std::ifstream ifile(fpath, std::ios::in | std::ios::binary);
char* fbuffer = new char[results.st_size];
ifile.read(fbuffer, results.st_size);
ifile.close();
unsigned char* fb64buffer = new unsigned char[results.st_size];
memcpy(fb64buffer, fbuffer, (size_t)results.st_size);
std::string fb64 = base64_encode(fb64buffer, results.st_size);
delete[] fb64buffer;
delete[] fbuffer;
fbuffer = new char[fb64.size()];
memcpy(fbuffer, fb64.c_str(), (size_t)fb64.size());
mu.lock(); std::cout << "[Thread: " << std::this_thread::get_id() << "][Output] The file has been read" << std::endl; mu.unlock();
//init variables
unsigned int k[4];
unsigned int kbuffer[4];
memset(k, 0, sizeof(k));
memset(kbuffer, 0, sizeof(kbuffer));
memcpy(kbuffer, key.c_str(), MIN(key.length(), 16));
for (int i=0; i<4; i++) k[i] = kbuffer[i];
size_t vbuffer_size = (size_t)fb64.size();
if (vbuffer_size%4 > 0) vbuffer_size += 4 - (vbuffer_size%4);
unsigned char* vbuffer = new unsigned char [vbuffer_size];
memset(vbuffer, 0, vbuffer_size);
memcpy(vbuffer, fbuffer, (size_t)fb64.size());
//преобразования
size_t obuffer_size = vbuffer_size+4;
char* obuffer = new char[obuffer_size];
unsigned int v[2];
memset(v, 0, sizeof(v));
for (int i=0; i<fb64.size(); i+=4) {
v[0] = *(unsigned int*)&vbuffer[i];
encrypt(&v[0], &k[0]);
memcpy(&obuffer[i], &v[0], 4);
memcpy(vbuffer, fbuffer, (size_t)fb64.size());
}
memcpy(&obuffer[obuffer_size-4], &v[1], 4);
//file writer
std::string filefolder;
std::string old_filename = getFileName(fpath, &filefolder);
std::string filename = key + ":" + old_filename; //новое имя файла
mu.lock(); std::cout << "[Thread: " << std::this_thread::get_id() << "][Output] File non-hased name: " << filename << std::endl; mu.unlock();
std::string md5_filename = md5(filename);
mu.lock(); std::cout << "[Thread: " << std::this_thread::get_id() << "][Output] File hased name: " << md5_filename << std::endl; mu.unlock();
stringToUpper(&md5_filename);
std::string fullpath = filefolder + md5_filename;
std::ofstream ofile(fullpath.c_str(), std::ios::out | std::ios::binary);
if (ofile.good()) {
ofile.clear();
}
ofile.write(obuffer, obuffer_size);
ofile.close();
mu.lock(); std::cout << "[Thread: " << std::this_thread::get_id() << "][Output] File has been encoded into: " << fullpath << std::endl; mu.unlock();
log(log_file, filefolder, filename, md5_filename, old_filename);
delete[] fbuffer;
delete[] vbuffer;
delete[] obuffer;
return true;
}
int main(const int argc, const char* argv[]) {
//path
std::string fpath;
if (argc>1) {
fpath.assign(argv[1]);
} else {
mu.lock(); std::cout << "[Output] You can open file(s) with this program" << std::endl; mu.unlock();
mu.lock(); std::cout << "[Output] Or drag'n'drop on it" << std::endl; mu.unlock();
mu.lock(); std::cout << "[Input] Enter filename(without spaces): "; mu.unlock();
std::cin >> fpath;
}
//key reader
std::string key;
std::cout << "[Input] Enter key(max 16 symbols): ";
std::cin >> key;
std::cout << std::endl;
std::string f, main_folder;
getFileName(argv[0], &main_folder);
std::ofstream ofile(main_folder+LOG_FILENAME, std::ios::out | std::ios::app);
std::cout << "[Output] Logging into: " << LOG_FILENAME << std::endl;
std::thread** threads;
if (argc>1) {
threads = new std::thread*[argc-1];
for (int i=0; i<argc-1; i++) {
std::string tfpath(argv[i+1]);
std::thread* t = new std::thread(encodeFile, tfpath, key, &ofile);
threads[i] = t;
}
for (int i=0; i<argc-1; i++) {
threads[i]->join();
}
} else {
threads = new std::thread*[1];
std::thread* t = new std::thread(encodeFile, fpath, key, &ofile);
threads[0] = t;
t->join();
}
for (size_t i=0; i<argc-1; i++) {
delete threads[i];
}
delete[] threads;
ofile.close();
char _C; std::cin >> _C; //breakpoint
return 0;
}