-
Notifications
You must be signed in to change notification settings - Fork 2
/
autotri.c
369 lines (311 loc) · 7.53 KB
/
autotri.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Automatic guessing based on trigrams.
*
* Bob Baldwin, January 1985.
*/
#include <stdio.h>
#include <math.h>
#include "window.h"
#include "terminal.h"
#include "layout.h"
#include "specs.h"
#include "cipher.h"
#include "autotri.h"
#include "dblock.h"
#define DEBUGP FALSE /* Perm building */
#define DEBUGB FALSE /* Best guess */
#define ATRLABEL1 \
"Auto Trigram, max SD: %4.2f total: %d wire: %d -- Please Wait"
#define ATRLABEL2 \
"Auto Trigram, max SD: %4.2f total: %d wire: %d -- Done"
#define ATRHELP "F3 enters guess, ^G undoes it."
extern char mcbuf[];
extern ecinfo gecinfo;
/* Forward declarations */
void atrdraw();
void atrfirst();
void atrenter();
void atrundo();
void atr_guess_init();
/* Global State. */
char *trigramstats; /* Filename for statistics. */
atrinfo gatrinfo;
keyer atrktab[] = {
{CACCEPT, atrenter},
{CUNDO, atrundo},
{CGO_UP, jogup},
{CGO_DOWN, jogdown},
{CGO_LEFT, jogleft},
{CGO_RIGHT, jogright},
{0, NULL},
};
/* Routine invoked by user to put up the equivalence class
* guessing window.
* The window is drawn empty, and then filled in with the guess.
* Return NULL if command completes ok.
*/
char *atrguess(str)
char *str; /* Command line */
{
gwindow *atr;
atrinfo *atri;
ecinfo *ecbi;
int *dbsperm;
int i;
atr = &gbstore;
atri = &gatrinfo;
dbsperm = refperm(dbsgetblk(&dbstore));
atr_init(mcbuf, dbsperm, atri);
ecbi = atri->eci;
atri->min_total_chars = 123;
atri->min_wire_chars = 456;
if ((i = sscanf(str, "%*[^:]: %f %*[^:]: %d %*[^:]: %d",
&atri->max_score, &atri->min_total_chars,
&atri->min_wire_chars)) != 3) {
return("Could not parse all three arguments.");
}
gbsswitch(atr, ((char *) atri), atrktab, atrfirst, wl_noop, atrdraw);
sprintf(statmsg, ATRLABEL1,
atri->max_score, atri->min_total_chars, atri->min_wire_chars);
gblset(&gblabel, statmsg);
atrdraw(atr);
fflush(stdout);
atr_autoguess(atri);
decode(ecbi->ciphertext, ecbi->plaintext, ecbi->perm);
sprintf(statmsg, ATRLABEL2,
atri->max_score, atri->min_total_chars, atri->min_wire_chars);
gblset(&gblabel, statmsg);
atrdraw(atr);
return(NULL);
}
/* (re) Draw the window.
*/
void atrdraw(atr)
gwindow *atr;
{
int i;
int row, col;
atrinfo *atri;
ecinfo *ecbi;
atri = ((atrinfo *) atr->wprivate);
ecbi = atri->eci;
row = 1;
col = 1;
for (i = 0 ; i < BLOCKSIZE ; i++) {
if (i%LINELEN == 0) {
wl_setcur(atr, gbspos2row(i), gbspos2col(i));
}
plnchars(1, char2sym(ecbi->plaintext[i]));
}
for (i = gbspos2row(BLOCKSIZE) ; i <= GBHEIGHT ; i++) {
wl_setcur(atr, i, 1);
plnchars(LINELEN, ' ');
}
for (i = 1 ; i <= GBHEIGHT ; i++) {
wl_setcur(atr, i, LINELEN+1);
plnchars(atr->wwidth - LINELEN, ' ');
}
wl_setcur(atr, row, col);
}
/* First time cursor enters window.
*/
void atrfirst(atr, row, col)
gwindow *atr;
int row, col;
{
usrhelp(&user, ATRHELP);
wl_setcur(atr, row, col);
}
/* Enter the guess into the decryption block.
*/
void atrenter(atr)
gwindow *atr;
{
atrinfo *atri;
atri = ((atrinfo *) atr->wprivate);
dbsmerge(&dbstore, atri->eci->perm);
wl_rcursor(atr);
}
/* Undo the last guess.
*/
void atrundo(atr)
gwindow *atr;
{
dbsundo(&dbstore);
wl_rcursor(atr);
}
/* Fill in auto-trigram info from given ciphertext block.
* The filter parameters are not set by this routine.
*/
void atr_init(cipher, perm, atri)
char cipher[];
int perm[];
atrinfo *atri;
{
extern int *trig_loaded;
atri->eci = &gecinfo;
if (!trig_loaded)
load_tri_from(trigramstats);
ec_init(cipher, perm, atri->eci);
atr_guess_init(atri);
}
/* Per guess initialization.
*/
void atr_guess_init(atri)
atrinfo *atri;
{
atri->best_trigram = NULL;
atri->best_score = 10.0;
atri->gcount = 0;
atri->total_score = 0;
atri->best_pvec[0] = NONE;
atri->best_permvec[0].x = NONE;
}
/* Score a trigram at a given position.
* It also looks in atri for filtering parameters (total number
* of chars must not be less than min_total_chars, and the minimum number
* of chars deduced per wire must not be less than min_wire_chars).
* This routine fills in permvec and pvec.
* Returns -1.0 if guess is unacceptable.
*/
float atr_score(atri, trigent, pos, permvec, pvec)
atrinfo *atri;
trig_ent *trigent;
int pos;
perment permvec[];
int pvec[];
{
int length;
int i, x, y;
int ccount;
int added;
/* extern float logvar; */
float score;
ecinfo *eci;
int butfirst;
int butlast;
for (length = 0 ; trigent->trigram[length] != 0 ; length++);
eci = atri->eci;
added = permvec_from_string(atri->eci, trigent->trigram, pos, permvec);
if (added < 0) return(-1.0);
butfirst = pos;
butlast = pos + length -1;
ccount = 0;
for (i = 0 ; i < PERMSZ && permvec[i].x != NONE ; i++) {
if (ccount >= BLOCKSIZE-1) break;
x = permvec[i].x;
y = permvec[i].y;
/* added = decode_wire_but(eci, x, y, &pvec[ccount], -1, -1);*/
added = decode_wire_but(eci, x, y, &pvec[ccount], butfirst, butlast);
if (added < 0) {
ccount = 0;
break;
}
if (added < atri->min_wire_chars) {
ccount = 0;
break;
}
ccount += added;
}
pvec[ccount] = -1;
if (ccount <= 0) return(-1.0);
if (ccount < atri->min_total_chars) return(-1.0);
score = pvec_1score(pvec);
if (score < 0.0) return(-1.0);
/*
score = exp(-(score * score) / 2.0);
score = score / sqrt(2*PI*logvar/ccount);
score = score * trigent->prob;
*/
return(score);
}
/* Select the best trigram for a given position.
* Returns pointer to trigram string, or NULL.
* Fills in atri with additional information.
* Filtering parameters are in atri.
*/
char *atr_best(atri, pos)
atrinfo *atri;
int pos;
{
int tgram;
float score;
perment permvec[PERMSZ];
int pvec[BLOCKSIZE+1];
#if DEBUGB
char str[BLOCKSIZE+1];
#endif
atr_guess_init(atri);
for (tgram = 0 ; trig_tab[tgram].trigram != NULL ; tgram++) {
score = atr_score(atri, &trig_tab[tgram], pos, permvec, pvec);
if (score < 0.0) continue;
atri->total_score += score;
atri->gcount++;
if (score < atri->best_score) {
atri->best_score = score;
atri->best_trigram = trig_tab[tgram].trigram;
pvec_copy(pvec, atri->best_pvec);
permvec_copy(permvec, atri->best_permvec, PERMSZ);
}
}
#if DEBUGB
if (atri->best_score < atri->max_score) {
printf("\nTrigram '%s' at %d", atri->best_trigram, pos);
pvec2str(str, atri->best_pvec);
printf(" deduces '%s'", str);
printf(" which scores as %g", atri->best_score);
printf(".\n");
}
#endif
if (atri->best_score < atri->max_score)
{return(atri->best_trigram);}
else
{return(NULL);}
}
/* Merge the given permvector into the permutation table.
* Return ERROR if there is a conflict, otherwise TRUE.
*/
int accept_permvec(atri, permvec)
atrinfo *atri;
perment permvec[];
{
int i, x, y;
ecinfo *ecbi;
ecbi = atri->eci;
for (i = 0 ; i < PERMSZ && permvec[i].x != NONE ; i++) {
x = MODMASK & permvec[i].x;
y = MODMASK & permvec[i].y;
if (perm_conflict(ecbi->perm, x, y)) {
#if DEBUGP
printf("CONFLICT trying to wire %d to %d.\n", x, y);
#endif
return(ERROR);
}
}
/* Now know that there are no conflicts. */
for (i = 0 ; i < PERMSZ && permvec[i].x != NONE ; i++) {
#if DEBUGP
printf("ACCEPTING wiring of %d to %d.\n", x, y);
#endif
x = MODMASK & permvec[i].x;
y = MODMASK & permvec[i].y;
ecbi->perm[x] = y;
ecbi->perm[y] = x;
}
return(TRUE);
}
/* Perform automatic guessing given a set of
* filter parameters in an atrinfo structure.
*/
void atr_autoguess(atri)
atrinfo *atri;
{
int pos;
char *trigram;
for (pos = 0 ; pos < BLOCKSIZE ; pos++) {
trigram = atr_best(atri, pos);
if (trigram != NULL) {
accept_permvec(atri, atri->best_permvec);
}
}
}