libenigma
Enigma machine utility library
Loading...
Searching...
No Matches
rotor.h
Go to the documentation of this file.
1
6#ifndef ENIGMA_ROTOR_H
7#define ENIGMA_ROTOR_H
8
9#include "common.h"
10
14#define ENIGMA_ROTOR_COUNT 8
15
23typedef struct {
24 const char* name;
25 int fwd_indices
27 int rev_indices
29 int notches[2];
32
33// clang-format off
34
35// Getting formatting errors on EnigmaRotor definitions for some reason, even though everything's
36// properly formatted.
37
44static const EnigmaRotor enigma_rotor_I = {
45 .name = "I",
46 .fwd_indices = { 4, 10, 12, 5, 11, 6, 3, 16, 21, 25, 13, 19, 14,
47 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9 },
48 .rev_indices = { 20, 22, 24, 6, 0, 3, 5, 15, 21, 25, 1, 4, 2,
49 10, 12, 19, 7, 23, 18, 11, 17, 8, 13, 16, 14, 9 },
50 .notches = { 16 },
51 .notches_count = 1,
52};
53
60static const EnigmaRotor enigma_rotor_II = {
61 .name = "II",
62 .fwd_indices = { 0, 9, 3, 10, 18, 8, 17, 20, 23, 1, 11, 7, 22,
63 19, 12, 2, 16, 6, 25, 13, 15, 24, 5, 21, 14, 4 },
64 .rev_indices = { 0, 9, 15, 2, 25, 22, 17, 11, 5, 1, 3, 10, 14,
65 19, 24, 20, 16, 6, 4, 13, 7, 23, 12, 8, 21, 18 },
66 .notches = { 4 },
67 .notches_count = 1,
68};
69
76static const EnigmaRotor enigma_rotor_III = {
77 .name = "III",
78 .fwd_indices = { 1, 3, 5, 7, 9, 11, 2, 15, 17, 19, 23, 21, 25,
79 13, 24, 4, 8, 22, 6, 0, 10, 12, 20, 18, 16, 14 },
80 .rev_indices = { 19, 0, 6, 1, 15, 2, 18, 3, 16, 4, 20, 5, 21,
81 13, 25, 7, 24, 8, 23, 9, 22, 11, 17, 10, 14, 12 },
82 .notches = { 21 },
83 .notches_count = 1,
84};
85
92static const EnigmaRotor enigma_rotor_IV = {
93 .name = "IV",
94 .fwd_indices = { 4, 18, 14, 21, 15, 25, 9, 0, 24, 16, 20, 8, 17,
95 7, 23, 11, 13, 5, 19, 6, 10, 3, 2, 12, 22, 1 },
96 .rev_indices = { 7, 25, 22, 21, 0, 17, 19, 13, 11, 6, 20, 15, 23,
97 16, 2, 4, 9, 12, 1, 18, 10, 3, 24, 14, 8, 5 },
98 .notches = { 17 },
99 .notches_count = 1,
100};
101
108static const EnigmaRotor enigma_rotor_V = {
109 .name = "V",
110 .fwd_indices = { 21, 25, 1, 17, 6, 8, 19, 24, 20, 15, 18, 3, 13,
111 7, 11, 23, 0, 22, 12, 9, 16, 14, 5, 4, 2, 10 },
112 .rev_indices = { 16, 2, 24, 11, 23, 22, 4, 13, 5, 19, 25, 14, 18,
113 12, 21, 9, 20, 3, 10, 6, 8, 0, 17, 15, 7, 1 },
114 .notches = { 25 },
115 .notches_count = 1,
116};
117
124static const EnigmaRotor enigma_rotor_VI = {
125 .name = "VI",
126 .fwd_indices = { 9, 15, 6, 21, 14, 20, 12, 5, 24, 16, 1, 4, 13,
127 7, 25, 17, 3, 10, 0, 18, 23, 11, 8, 2, 19, 22 },
128 .rev_indices = { 18, 10, 23, 16, 11, 7, 2, 13, 22, 0, 17, 21, 6,
129 12, 4, 1, 9, 15, 19, 24, 5, 3, 25, 20, 8, 14 },
130 .notches = { 25, 12 },
131 .notches_count = 2,
132};
133
140static const EnigmaRotor enigma_rotor_VII = {
141 .name = "VII",
142 .fwd_indices = { 13, 25, 9, 7, 6, 17, 2, 23, 12, 24, 18, 22, 1,
143 14, 20, 5, 0, 8, 21, 11, 15, 4, 10, 16, 3, 19 },
144 .rev_indices = { 16, 12, 6, 24, 21, 15, 4, 3, 17, 2, 22, 19, 8,
145 0, 13, 20, 23, 5, 10, 25, 14, 18, 11, 7, 9, 1 },
146 .notches = { 25, 12 },
147 .notches_count = 2,
148};
149
156static const EnigmaRotor enigma_rotor_VIII = {
157 .name = "VIII",
158 .fwd_indices = { 5, 10, 16, 7, 19, 11, 23, 14, 2, 1, 9, 18, 15,
159 3, 25, 17, 0, 12, 4, 22, 13, 8, 20, 24, 6, 21 },
160 .rev_indices = { 16, 9, 8, 13, 18, 0, 24, 3, 21, 10, 1, 5, 17,
161 20, 7, 12, 2, 15, 11, 4, 22, 25, 19, 6, 23, 14 },
162 .notches = { 25, 12 },
163 .notches_count = 2,
164};
165
166// clang-format on
167
171static const EnigmaRotor* enigma_rotors[]
172 = { &enigma_rotor_I, &enigma_rotor_II, &enigma_rotor_III, &enigma_rotor_IV,
173 &enigma_rotor_V, &enigma_rotor_VI, &enigma_rotor_VII, &enigma_rotor_VIII };
174
176
177/* --- EnigmaRotor getters and setters --- */
178const char* enigma_rotor_get_name(const EnigmaRotor*);
181const int* enigma_rotor_get_notches(const EnigmaRotor*);
183int enigma_rotor_set_name(EnigmaRotor*, const char*);
186int enigma_rotor_set_notches(EnigmaRotor*, const int*, int);
188
189#endif
#define ENIGMA_ALPHA_SIZE
Defines the size of the alphabet used in the Enigma machine.
Definition: common.h:18
int enigma_rotor_set_fwd_indices(EnigmaRotor *, const int *)
Set the forward indices of the rotor.
Definition: rotor.c:148
int enigma_rotor_set_rev_indices(EnigmaRotor *, const int *)
Set the reverse indices of the rotor.
Definition: rotor.c:165
const int * enigma_rotor_get_rev_indices(const EnigmaRotor *)
Get the reverse indices of the rotor.
Definition: rotor.c:83
int enigma_rotor_set_notches_count(EnigmaRotor *, int)
Set the notches count of the rotor.
Definition: rotor.c:202
const int * enigma_rotor_get_fwd_indices(const EnigmaRotor *)
Get the forward indices of the rotor.
Definition: rotor.c:67
int enigma_rotor_generate_indices(EnigmaRotor *, const char *)
Generate the indices for the rotor.
Definition: rotor.c:23
int enigma_rotor_set_name(EnigmaRotor *, const char *)
Set the name of the rotor.
Definition: rotor.c:131
int enigma_rotor_set_notches(EnigmaRotor *, const int *, int)
Set the notches of the rotor.
Definition: rotor.c:184
const int * enigma_rotor_get_notches(const EnigmaRotor *)
Get the notches of the rotor.
Definition: rotor.c:99
const char * enigma_rotor_get_name(const EnigmaRotor *)
Get the name of the rotor.
Definition: rotor.c:51
int enigma_rotor_get_notches_count(const EnigmaRotor *)
Get the number of notches of the rotor.
Definition: rotor.c:115
Represents a rotor configuration for the Enigma machine.
Definition: rotor.h:23
const char * name
Name of the rotor.
Definition: rotor.h:24
int notches_count
Number of notches.
Definition: rotor.h:30