-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclo.hpp
205 lines (172 loc) · 4.3 KB
/
clo.hpp
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
#ifndef H_CLOPTION
#define H_CLOPTION
#include <iostream> //std::cout
#include <cstdlib> // strtol
#include <fstream> // std::ifstream, std::ofstream
#include <sstream> // std::ostringstream
#include <string> // std::string
#include <stdexcept> // std::logic_error, std::runtime_error
//////////////////////////////////////////////////////
// Minimalistic command line options parser for this program.
//////////////////////////////////////////////////////
class CLO
{
public:
CLO(int argc, char *argv[])
: options_flags(0)
{
std::string Buf;
for(int i = 1; i < argc;)
{
Buf = argv[i];
/* Input File*/
if(Buf == "-i")
{
input = ""; // ignore any previous input
if(Flag(INPUT_FILE))
throw std::logic_error(
"The -i option has been found more than once.");
if(i+1 < argc && argv[i+1][0] != '-')
input=LoadFromFile(argv[++i]);
else
throw std::logic_error(
"Missing input file name.");
options_flags |= INPUT_FILE;
}
/* Output File */
if(Buf == "-o")
{
if(Flag(OUTPUT_FILE))
throw std::logic_error(
"The -o option has been found more than once.");
if(i+1 < argc && argv[i+1][0] != '-')
output_filename = argv[++i];
else
throw std::logic_error(
"Missing output file name.");
options_flags |= OUTPUT_FILE;
}
/* ENCIPHER */
else if(Buf == "-c")
{
if(Flag(ENCIPHER) || Flag(DECIPHER))
throw std::logic_error(
"The -c and/or -d options have been found more than once.");
if(i+1 < argc && argv[i+1][0] != '-')
key_ = argv[++i];
else
throw std::logic_error(
"Missing ciphers's key.");
options_flags |= ENCIPHER;
}
/* DECIPHER */
else if(Buf == "-d")
{
if(Flag(ENCIPHER) || Flag(DECIPHER))
throw std::logic_error(
"The -c and/or -d options have been found more than once.");
if((i+1 < argc) && (argv[i+1][0] != '-'))
{
char* endptr = 0;
key_size_ = strtol(argv[++i],&endptr,10);
if(endptr!=argv[i] && key_size_ != 0)
{
options_flags |= KEY_SIZE;
std::cout << "The key size is known : ";
std::cout << "data cryptanalysis and deduction of the key." << std::endl;
}
else
key_ = argv[i];
}
else
{
options_flags |= UNKNOWN_KEY;
std::cout << "The key size is unknown : ";
std::cout << "data cryptanalysis and deduction of the key." << std::endl;
}
options_flags |= DECIPHER;
}
else if(Buf == "-v")
{
options_flags |= VERBOSE;
}
else if(Buf == "-h")
{
options_flags |= HELP;
}
/* Append to input */
else if(!Flag(INPUT_FILE))
input += Buf;
++i;
}
/* Check */
if(input.empty())
throw std::logic_error("Empty input.");
if(((options_flags & ENCIPHER) == 0) && ((options_flags & DECIPHER) == 0))
throw std::logic_error("None of -c or -d options found.");
}
const std::string &Input(void) const
{
return input;
}
std::string key(void) const
{
return key_;
}
size_t key_size() const
{
return key_size_;
}
const std::string &OutputFilename(void) const
{
return output_filename;
}
unsigned int GetFlags(void) const
{
return options_flags;
}
bool Flag(const int f)
{
return ((options_flags & f) != 0);
}
std::string LoadFromFile(const std::string &Name)
{
std::ifstream Ifs(Name.c_str());
if(!Ifs)
throw std::runtime_error(
"Can't open input file.");
std::ostringstream Oss;
Oss << Ifs.rdbuf();
return Oss.str();
}
void SaveInFile(const std::string &Name, const std::string &String)
{
std::ofstream Ofs(Name.c_str());
if(!Ofs)
throw std::runtime_error("Can't open output file.");
Ofs << String;
}
static const int ENCIPHER ;
static const int DECIPHER;
static const int KEY_SIZE;
static const int UNKNOWN_KEY;
static const int HELP;
static const int INPUT_FILE;
static const int OUTPUT_FILE;
static const int VERBOSE;
private:
std::string input;
std::string key_;
std::string output_filename;
size_t key_size_;
unsigned int options_flags;
};
const int CLO::ENCIPHER = 1;
const int CLO::DECIPHER = 1 << 1;
const int CLO::KEY_SIZE = 1 << 2;
const int CLO::UNKNOWN_KEY = 1 << 3;
const int CLO::HELP = 1 << 4;
const int CLO::INPUT_FILE = 1 << 5;
const int CLO::OUTPUT_FILE = 1 << 6;
const int CLO::VERBOSE = 1 << 7;
#endif