-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenigma.cpp
More file actions
160 lines (132 loc) · 3.61 KB
/
Copy pathenigma.cpp
File metadata and controls
160 lines (132 loc) · 3.61 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
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<fstream>
#include<cctype>
#include "enigma.h"
#include "helper.h"
#include "errors.h"
using namespace std;
Enigma::Enigma(int argc, char ** argv){
plugboard = nullptr;
reflector = nullptr;
leftmost_rotor = nullptr;
rightmost_rotor = nullptr;
string call_string;
Rotor* temp;
if (argc < 4){
exit_code = INSUFFICIENT_NUMBER_OF_PARAMETERS;
error_handling("usage: ", exit_code);
}
// For-loop parsing the input files and calling the appropriate constructor
for (int i = 1 ; i < argc && !exit_code; i++){
call_string = argv[i];
//Plugboard
auto found = call_string.find(".pb");
if (found != string::npos){
plugboard = new Plugboard(call_string);
exit_code = plugboard->exit_code;
}
//Reflector
found = call_string.find(".rf");
if (found != string::npos){
reflector = new Reflector(call_string);
exit_code = reflector->exit_code;
}
//Rotor Linked List
found = call_string.find(".rot");
if (found != string::npos){
//This code appends an existing Linked List of Rotors
if (rotor_count){
rightmost_rotor = new Rotor(call_string);
exit_code = rightmost_rotor->exit_code;
temp->right = rightmost_rotor;
rightmost_rotor->left = temp;
temp = rightmost_rotor;
rotor_count++;
}
//This code creates a new Rotor Linked List
if (!rotor_count){
leftmost_rotor = new Rotor(call_string);
exit_code = leftmost_rotor->exit_code;
rightmost_rotor = leftmost_rotor;
temp = leftmost_rotor;
rotor_count++;
}
}
//Rotor Positions
found = call_string.find(".pos");
if (found != string::npos){
exit_code = set_rotors(call_string, rotor_count);
}
if (exit_code){
error_handling(call_string, exit_code);
break;
}
}
}
/*END OF FUNCTION*/
int Enigma::set_rotors(string const& call_string, int rotor_count){
int count = 0, exit = NO_ERROR;
string pos_digit_string;
ifstream is_position;
int pos_configuration[rotor_count];
//Opening file
is_position.open(call_string, ios::in);
if (!is_position){
exit = ERROR_OPENING_CONFIGURATION_FILE;
}
//Inserting next digit from the file
is_position >> pos_digit_string;
while (!exit && !is_position.eof()){
is_numeric(pos_digit_string, exit);
if (!exit)
input_in_range(pos_digit_string, exit);
if (!exit)
pos_configuration[count] = stoi(pos_digit_string);
if (!exit)
is_position >> pos_digit_string;
count++;
}
if (!exit && count < rotor_count)
exit = NO_ROTOR_STARTING_POSITION;
//Setting the reference number to starting position for all rotors
if (!exit){
Rotor* here = leftmost_rotor;
for (int rotor = 0 ; here ; rotor++){
here->reference_no = pos_configuration[rotor];
here = here->right;
}
}
return exit;
}
/*END OF FUNCTION*/
int Enigma::encryption(int digit){
if (rightmost_rotor)
rightmost_rotor->rotate();
digit = plugboard->encrypt(digit);
if (rightmost_rotor)
digit = rightmost_rotor->encrypt_forward(digit);
digit = reflector->encrypt(digit);
if (leftmost_rotor)
digit = leftmost_rotor->encrypt_backwards(digit);
digit = plugboard->encrypt(digit);
return static_cast<char>(digit) + ASCII_A;
}
/*END OF FUNCTION*/
void Enigma::clear_rotor(){
Rotor* current = leftmost_rotor;
Rotor* next;
while (current){
next = current->right;
delete current;
current = next;
}
}
/*END OF FUNCTION*/
Enigma::~Enigma(){
delete plugboard;
delete reflector;
clear_rotor();
}
/*END OF FUNCTION*/