-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.c
112 lines (93 loc) · 2.59 KB
/
key.c
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
#include "global.h"
#include "key.h"
/* initialize key to defaults */
int init_key_default(Key *key, int model)
{
Key def_H = {H, 1,0,1,2,3,0,0,0,0,0,0,0,0,{0},{0},0,0};
Key def_M3 = {M3,1,0,1,2,3,0,0,0,0,0,0,0,0,{0},{0},0,0};
Key def_M4 = {M4,3,9,1,2,3,0,0,0,0,0,0,0,0,{0},{0},0,0};
int i;
switch (model) {
case H : *key = def_H; break;
case M3: *key = def_M3; break;
case M4: *key = def_M4; break;
default: return 0;
}
for (i = 0; i < 26; i++)
key->stbrett[i] = key->sf[i] = i;
return 1;
}
/* initializes each key element to the lowest possible value */
int init_key_low(Key *key, int model)
{
Key low_H = {H, 0,0,1,1,1,0,0,0,0,0,0,0,0,{0},{0},0,0};
Key low_M3 = {M3,1,0,1,1,1,0,0,0,0,0,0,0,0,{0},{0},0,0};
Key low_M4 = {M4,3,9,1,1,1,0,0,0,0,0,0,0,0,{0},{0},0,0};
int i;
switch (model) {
case H : *key = low_H; break;
case M3: *key = low_M3; break;
case M4: *key = low_M4; break;
default: return 0;
}
for (i = 0; i < 26; i++)
key->stbrett[i] = key->sf[i] = i;
return 1;
}
/* compares ukwnum thru r_mesg, omits g_ring, l_ring */
/* returns -1 for k1 < k2, 0 for k1 == k2, 1 for k1 > k2 */
int keycmp(const Key *k1, const Key *k2)
{
if ( k1->ukwnum != k2->ukwnum ) {
if ( k1->ukwnum > k2->ukwnum ) return 1;
else return -1;
}
if ( k1->g_slot != k2->g_slot ) {
if ( k1->g_slot > k2->g_slot ) return 1;
else return -1;
}
if ( k1->l_slot != k2->l_slot ) {
if ( k1->l_slot > k2->l_slot ) return 1;
else return -1;
}
if ( k1->m_slot != k2->m_slot ) {
if ( k1->m_slot > k2->m_slot ) return 1;
else return -1;
}
if ( k1->r_slot != k2->r_slot ) {
if ( k1->r_slot > k2->r_slot ) return 1;
else return -1;
}
if ( k1->m_ring != k2->m_ring ) {
if ( k1->m_ring > k2->m_ring ) return 1;
else return -1;
}
if ( k1->r_ring != k2->r_ring ) {
if ( k1->r_ring > k2->r_ring ) return 1;
else return -1;
}
if ( k1->g_mesg != k2->g_mesg ) {
if ( k1->g_mesg > k2->g_mesg ) return 1;
else return -1;
}
if ( k1->l_mesg != k2->l_mesg ) {
if ( k1->l_mesg > k2->l_mesg ) return 1;
else return -1;
}
if ( k1->m_mesg != k2->m_mesg ) {
if ( k1->m_mesg > k2->m_mesg ) return 1;
else return -1;
}
if ( k1->r_mesg != k2->r_mesg ) {
if ( k1->r_mesg > k2->r_mesg ) return 1;
else return -1;
}
return 0;
}
/*
* This file is part of enigma-suite-0.76, which is distributed under the terms
* of the General Public License (GPL), version 2. See doc/COPYING for details.
*
* Copyright (C) 2005 Stefan Krah
*
*/