-
Notifications
You must be signed in to change notification settings - Fork 2
/
perm.c
248 lines (189 loc) · 4.61 KB
/
perm.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Abstraction for the table of cipher text blocks and
* their decoded permutations so far.
*
* Robert W. Baldwin, December 1984.
*/
#include <stdlib.h>
#include <stdio.h>
#include "window.h"
#include "specs.h"
#define NPERLINE 10 /* How many values per line in save file. */
#define FROMSTART 0 /* For fseek call, how offset measured. */
extern void loadzee(FILE *fd);
extern void storezee(FILE *fd);
/* Input file name for permutations. */
char *permfile;
/* Global state. */
int permchgflg = FALSE; /* True if perms changed since last save. */
int *permtab[NPERMS]; /* Table of saved permutations or null. */
int perminit = FALSE; /* Initialization flag. */
/* Allocate and clear a permutation.
*/
int *permalloc(void)
{
int i;
int *perm;
perm = ((int *) malloc((BLOCKSIZE+1)*sizeof(int)));
if (perm == NULL) {
printf("\nNo room to allocate permutation.\n");
exit(0);
}
for (i = 0 ; i < BLOCKSIZE ; i++) {
perm[i] = -1;
}
return(perm);
}
/* Return a pointer (for read or write use) to the permutation
* for the given block number.
* Return NULL if the block number is bad.
*/
int *refperm(int blocknum)
{
int i;
if ((blocknum < 0) || (NPERMS <= blocknum)) return(NULL);
if (!perminit) {
perminit = TRUE;
for (i = 0 ; i < NPERMS ; i++) permtab[i] = NULL;
}
if (permtab[blocknum] == NULL) {
permtab[blocknum] = permalloc();
}
return(permtab[blocknum]);
}
/* Save all the permutations in a file.
* This can be invoked as a command.
* For now, the are no arguments, the filename is fixed.
* Return NULL if successful, else error mesage.
* First the Zee matrix is dumped, then the permutations.
* Each block is separated by a newline character.
* Individual numbers are separated by blanks.
*/
char *permsave(char *str __attribute__((unused)))
{
FILE *fd;
int i;
if ((fd = fopen(permfile, "w")) == NULL) {
sprintf(statmsg, "Could not open %s to write permutations.", permfile);
return(statmsg);
}
storezee(fd);
for (i = 0 ; i < NPERMS ; i++) {
writeperm(fd, refperm(i));
}
fclose(fd);
permchgflg = FALSE;
return(NULL);
}
/* Restore all the permutations by reading them from a file.
* This can be invoked as a command.
* For now, the are no arguments, the filename is fixed.
* Return NULL if successful, else ptr to error message.
* Also call dblock to update its display.
*/
char *permload(char *str __attribute__((unused)))
{
FILE *fd;
int i;
if ((fd = fopen(permfile, "r")) == NULL) {
sprintf(statmsg, "Could not open %s to read permutations.", permfile);
return(statmsg);
}
loadzee(fd);
for (i = 0 ; i < NPERMS ; i++) {
readperm(fd, refperm(i));
}
fclose(fd);
permchgflg = FALSE;
dbssetblk(&dbstore, dbsgetblk(&dbstore)); /* Update perm and cbuf. */
return(NULL);
}
/* Compute a permutation raised to some power.
*/
void expperm(int *srcperm, int *dstperm, int power)
{
int i, k, v;
for (i = 0 ; i < BLOCKSIZE ; i++) {
v = i;
for (k = 0 ; k < power ; k++) {
v = srcperm[v];
if (v == -1) break;
}
dstperm[i] = v;
}
}
/* Computer product of two permutations.
*/
void multperm(int *left, int *right, int *result)
{
int i, v;
for (i = 0 ; i < BLOCKSIZE ; i++) {
v = right[i];
if (v != -1) v = left[v];
result[i] = v;
}
}
/* Write a permutation onto the given stream.
*/
void writeperm(FILE *fd, int perm[])
{
int j;
for (j = 0 ; j < BLOCKSIZE ; j++) {
fprintf(fd, "%3d ", perm[j]);
if ((j+1)%NPERLINE == 0) fprintf(fd,"\n");
}
fprintf(fd,"\n");
}
/* Copy a permutation to another buffer.
*/
void copyperm(int src[], int dst[])
{
int j;
for (j = 0 ; j < BLOCKSIZE ; j++) {
dst[j] = src[j];
}
}
/* Read a permutation from the given stream into the given buffer.
*/
void readperm(FILE *fd, int perm[])
{
int j;
for (j = 0 ; j < BLOCKSIZE ; j++) {
fscanf(fd, "%3d", &perm[j]);
}
fscanf(fd,"\n");
}
/* Return a count of the number of values in the permutation that
* are not equal to -1.
* Max value is 256.
*/
int permcount(int perm[])
{
int i;
int count;
count = 0;
for (i = 0 ; i < BLOCKSIZE ; i++) {
if (perm[i] != -1) count++;
}
return(count);
}
/* Return a count of the number of wires in a symetric permutation.
* Return -1 if the permutation is not its own inverse or
* if it has fixed points.
* Max value is 128.
*/
int permwcount(int perm[])
{
int i,v;
int count;
count = 0;
for (i = 0 ; i < BLOCKSIZE ; i++) {
v = perm[i];
if (v == -1) continue;
if (perm[v] != i) return(-1); /* Not self inverse. */
if (v == i) return(-1); /* Has fixed point. */
if (i < v) continue; /* Count first instance. */
count++;
}
return(count);
}