-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (162 loc) · 6.77 KB
/
main.cpp
File metadata and controls
173 lines (162 loc) · 6.77 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
#include <QApplication>
#include <QFileInfo>
#include <QFile>
#include <QTextStream>
#include "MainWindow.h"
//Includes for the codecs
#include "encoders/Base64Codec.h"
#include "encoders/Rot13.h"
#include "encoders/CaesarCipher.h"
#include "encoders/BinaryCodec.h"
#include "encoders/HexCodec.h"
#include "encoders/PigLatin.h"
#include "encoders/Atbash.h"
#include "encoders/MorseCodec.h"
#include "encoders/AESCodec.h"
#include "encoders/RSACodec.h"
QString useCodec(const QString &inputFile, const QString &outputFile,
const QString &function, const QString &codec,
const QString &key, const QString &shift) {
QTextStream outlog(stdout);
QFile inFile(inputFile);
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream err(stderr);
err << "Error: Failed to open input file: " << inputFile << Qt::endl;
return "Fail";
}
QTextStream inStream(&inFile);
QString inputText = inStream.readAll();
inFile.close();
QString result;
if (codec == "base64") {
result = (function == "encode") ? Base64Codec::transform(inputText, false) : Base64Codec::transform(inputText, true);
} else if (codec == "rot13") {
result = Rot13::transform(inputText);
} else if (codec == "caesar") {
if (!shift.isEmpty()) {
result = (function == "encode") ? CaesarCipher::transform(inputText, shift.toInt(), false) : CaesarCipher::transform(inputText, shift.toInt(), true);
} else {
QTextStream err(stderr);
err << "Error: Missing shift amount." << Qt::endl;
return "Fail";
}
} else if (codec == "binary") {
result = (function == "encode") ? BinaryCodec::transform(inputText, false) : BinaryCodec::transform(inputText, true);
} else if (codec == "hex") {
result = (function == "encode") ? HexCodec::transform(inputText, false) : HexCodec::transform(inputText, true);
} else if (codec == "piglatin") {
result = (function == "encode") ? PigLatin::transform(inputText, false) : PigLatin::transform(inputText, true);
} else if (codec == "atbash") {
result = Atbash::transform(inputText);
} else if (codec == "morse") {
result = (function == "encode") ? MorseCodec::transform(inputText, false) : MorseCodec::transform(inputText, true);
} else if (codec == "aes") {
if (!key.isEmpty()) {
result = (function == "encode") ? AESCodec::encode(inputText, key) : AESCodec::decode(inputText, key);
} else {
QTextStream err(stderr);
err << "Error: Missing Key." << Qt::endl;
return "Fail";
}
} else if (codec == "rsa") {
if (!key.isEmpty()) {
result = (function == "encode") ? RSACodec::encode(inputText.toUtf8(), key) : RSACodec::decode(inputText.toUtf8(), key);
} else {
QTextStream err(stderr);
err << "Error: Missing Key." << Qt::endl;
return "Fail";
}
} else {
QTextStream err(stderr);
err << "Error: Unknown codec: " << codec << Qt::endl;
return "Fail";
}
if (!outputFile.isEmpty()) {
QFile outFile(outputFile);
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream err(stderr);
err << "Error: Failed to open output file: " << outputFile << Qt::endl;
return "Fail";
}
QTextStream outStream(&outFile);
outStream << result;
outFile.close();
} else {
outlog << result << Qt::endl;
}
return "Success";
}
void printUsage() {
QTextStream out(stdout);
out << "Usage:\n"
<< " decode -i <input_file> -o <output_file> -f <encode|decode> -c <codec> -k <key>\n\n"
<< "Available codecs: base64, rot13, caesar, binary, hex, piglatin, atbash, morse, aes, rsa\n\n"
<< "Arguments that may be used:"
<< "-i, --input file path for the input file.\n"
<< "-o, --output file path for the output file (optional)\n"
<< "-f, --function function to be performed, either encode or decode\n"
<< "-c, --codec codec to be used for the encoding or decoding\n"
<< "-k, --key the key to be used for encoding or decoding.\n"
<< " For AES, this is a text string like a password.\n"
<< " For RSA, this would be the path for the gpg key file.\n"
<< "-h, --help for outputting this dialog.\n"
<< "--clear-session clears the session data used by GUI.\n";
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
app.setWindowIcon(QIcon(":/decode.svg"));
MainWindow window;
QStringList args = QCoreApplication::arguments();
args.removeFirst();
QTextStream out(stdout);
QString inputFile, outputFile, function, codec, key, shift;
bool headless = false;
for (int i = 0; i < args.size(); ++i) {
QString arg = args[i];
if ((arg == "-i" || arg == "--input") && i + 1 < args.size()){
inputFile = args[++i];
headless=true;
} else if ((arg == "-o" || arg == "--output") && i + 1 < args.size()) {
outputFile = args[++i];
headless=true;
} else if ((arg == "-f" || arg == "--function") && i + 1 < args.size()) {
function = args[++i];
headless=true;
} else if ((arg == "-c" || arg == "--codec") && i + 1 < args.size()) {
codec = args[++i];
headless=true;
} else if ((arg == "-k" || arg == "--key") && i + 1 < args.size()) {
key = args[++i];
headless=true;
} else if ((arg == "-s" || arg == "--shift") && i + 1 < args.size()) {
shift = args[++i];
headless=true;
} else if (arg == "-h" || arg == "--help") {
headless=true;
printUsage();
return 0;
} else if (arg == "--clearsession") {
out << "Clear Session.\n";
window.clearSession();
return app.exec();
} else {
out << "Not a recognized flag recognized flag.: " << arg << Qt::endl;
if (QFileInfo::exists(arg) && QFileInfo(arg).isFile()) {
out << "URL recognized: " << arg << Qt::endl;
window.openFile(arg);
}
}
}
if (headless==false){
window.show();
}
if (!inputFile.isEmpty() && (function == "encode" || function == "decode") && !codec.isEmpty()) {
QString status = useCodec(inputFile, outputFile, function, codec, key, shift);
if (status != "Success") {
qCritical() << status;
return 1;
}
return 0;
}
return app.exec();
}