-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheclass.c
472 lines (389 loc) · 9.27 KB
/
eclass.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Equivalence class information about a cipher block.
*
* 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 "dblock.h"
#define DEBUG FALSE
#define MIN_SHOW_SCORE (0.7)
#define ECBLABEL1 "Equiv class guessing at level %6.3f -- Please Wait"
#define ECBLABEL2 "Equiv class guessing at level %6.3f -- Done"
#define ECBHELP "F3 enters guess, ^G undoes it."
extern char mcbuf[];
extern ecinfo gecinfo;
/* Forward declarations */
void ecbdraw(gwindow *ecb);
void ecbfirst(gwindow *ecb, int row, int col);
void ecbenter(gwindow *ecb);
void ecbundo(gwindow *ecb);
void ec_autoguess(ecinfo *ecbi, float alevel);
int ec_best(ecinfo *eci, int firstpos, float alevel);
int ec_compsize(ecinfo *eci, int member);
void ec_addsize(/* eci, size, firstmember */);
/* Global State. */
float ec_accept_level = 0.0;
keyer ecbktab[] = {
{CACCEPT, ecbenter},
{CUNDO, ecbundo},
{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 *ecbguess(str)
char *str; /* Command line */
{
ecinfo *ecbi;
int i;
gwindow *ecb;
ecb = &gbstore;
ecbi = &gecinfo;
ec_init(mcbuf, refperm(dbsgetblk(&dbstore)), ecbi);
if ((i = sscanf(str, "%*[^:]: %f", &ec_accept_level)) != 1) {
return("Could not parse acceptance level.");
}
gbsswitch(ecb, ((char *) ecbi), ecbktab, ecbfirst, wl_noop, ecbdraw);
sprintf(statmsg, ECBLABEL1, ec_accept_level);
gblset(&gblabel, statmsg);
ecbdraw(ecb);
fflush(stdout);
ec_autoguess(ecbi, ec_accept_level);
decode(ecbi->ciphertext, ecbi->plaintext, ecbi->perm);
sprintf(statmsg, ECBLABEL2, ec_accept_level);
gblset(&gblabel, statmsg);
ecbdraw(ecb);
return(NULL);
}
/* (re) Draw the window.
*/
void ecbdraw(ecb)
gwindow *ecb;
{
int i;
int row, col;
ecinfo *ecbi;
ecbi = ((ecinfo *) ecb->wprivate);
row = 1;
col = 1;
for (i = 0 ; i < BLOCKSIZE ; i++) {
if (i%LINELEN == 0) {
wl_setcur(ecb, gbspos2row(i), gbspos2col(i));
}
plnchars(1, char2sym(ecbi->plaintext[i]));
}
for (i = gbspos2row(BLOCKSIZE) ; i <= GBHEIGHT ; i++) {
wl_setcur(ecb, i, 1);
plnchars(LINELEN, ' ');
}
for (i = 1 ; i <= GBHEIGHT ; i++) {
wl_setcur(ecb, i, LINELEN+1);
plnchars(ecb->wwidth - LINELEN, ' ');
}
wl_setcur(ecb, row, col);
}
/* First time cursor enters window.
*/
void ecbfirst(gwindow *ecb, int row, int col)
{
usrhelp(&user, ECBHELP);
wl_setcur(ecb, row, col);
}
/* Enter the guess into the decryption block.
*/
void ecbenter(gwindow *ecb)
{
ecinfo *ecbi;
ecbi = ((ecinfo *) ecb->wprivate);
dbsmerge(&dbstore, ecbi->perm);
wl_rcursor(ecb);
}
/* Undo the last guess.
*/
void ecbundo(gwindow *ecb)
{
ecinfo *ecbi;
ecbi = ((ecinfo *) ecb->wprivate);
dbsundo(&dbstore);
wl_rcursor(ecb);
}
/* Dump plaintext chars onto stream.
*/
void ec_dplain(FILE *out, ecinfo *eci)
{
int i,c;
int *pbuf;
pbuf = &eci->plaintext[0];
for (i = 0 ; i < BLOCKSIZE ; i++) {
c = *pbuf++;
if (i % 20 == 0) fprintf(out,"\n");
if (c != NONE)
write_char(out, c);
else
write_char(out, '.');
}
fprintf(out,"\n");
}
/* Dump shifted cipher chars onto stream.
*/
void ec_dscipher(out, eci)
FILE *out;
ecinfo *eci;
{
int i,c;
int *pbuf;
pbuf = &eci->scipher[0];
for (i = 0 ; i < BLOCKSIZE ; i++) {
c = *pbuf++;
if (i++ % 20 == 0) fprintf(out,"\n");
if (c != NONE)
write_char(out, c);
else
write_char(out, '.');
}
fprintf(out,"\n");
}
/* Dump table of next pointers onto a stream.
*/
void ec_dnext(out, eci)
FILE *out;
ecinfo *eci;
{
writeperm(out, &(eci->next[0]));
}
/* Dump size table onto a stream.
*/
void ec_dsizetab(out, eci)
FILE *out;
ecinfo *eci;
{
int i;
fprintf(out, "\nThere are %d classes longer than 1 character.\n",
eci->sizelast);
for (i = 0 ; i < eci->sizelast ; i++) {
fprintf(out, "Size: %d, First member: %d.\n",
eci->sizelist[i].size, eci->sizelist[i].firstpos);
}
}
/* Dump our permutation onto a stream.
*/
void ec_dperm(out, eci)
FILE *out;
ecinfo *eci;
{
writeperm(out, &(eci->perm[0]));
}
/* Dump the permutation map onto a stream.
*/
void ec_dpmap(out, eci)
FILE *out;
ecinfo *eci;
{
writeperm(out, &(eci->permmap[0]));
}
/* Update ecbi to reflect the automatic guesses.
*/
void ec_autoguess(ecinfo *ecbi, float alevel)
{ int i, c;
int classpos;
int x, y;
for (i = 0 ; i < ecbi->sizelast ; i++) {
classpos = ecbi->sizelist[i].firstpos;
c = ec_best(ecbi, classpos, alevel);
if (c != NONE) {
x = ecbi->scipher[classpos];
y = MODMASK & (c + classpos);
if (!perm_conflict(ecbi->perm, x, y)) {
ecbi->perm[x] = y;
ecbi->perm[y] = x;
}
#if DEBUG
else {
printf("ec_autoguess: Best guess conflicts");
printf(" with an accepted.\n");
}
#endif
}
}
}
/* Score a single equivalence class.
* Bigger scores are better scores. They range from 0 to 1.
* A score of zero means the choice is not possible.
*/
float ec_cscore(eci, firstpos, plainchar)
ecinfo *eci;
int firstpos;
int plainchar;
{
extern float logvar;
float score;
int pvec[BLOCKSIZE+1];
int ccount;
#if DEBUG
char str[BLOCKSIZE+1];
#endif
if (decode_class(eci, firstpos, plainchar, pvec) == ERROR) {
return(0.0);
}
score = pvec_1score(pvec);
if (score < 0.0) return(0.0);
score = exp(-(score * score) / 2.0);
for (ccount = 0 ; pvec[ccount] != NONE ; ccount++);
score = score / sqrt(2*PI*logvar/ccount);
#if DEBUG
if (score > MIN_SHOW_SCORE) {
pvec2str(str, pvec);
printf("Derived characters are '%s", str);
printf("', their score is %7.4f\n", score);
}
#endif
return(score);
}
/* Select best plaintext value for a ciphertext equiv class.
* The class is identified by the position in the block of one
* of the characters in the class. The plaintext value for
* an entire class can be specified by the plaintext value of
* one of its members. This routine returns the best plaintext
* value for the ciphertext character at position firstpos.
* If there is not a clear best value, NONE is returned.
*/
int ec_best(ecinfo *eci, int firstpos, float alevel)
{
float total_score, score;
float best_score;
int best_char;
int c;
/* int x,y; */
float count;
#if DEBUG
int pvec[BLOCKSIZE+1];
char str[BLOCKSIZE+1];
printf("\n");
printf("The first position of this class is %d.\n", firstpos);
#endif
total_score = 0.0;
best_score = 0.0;
count = 0.0;
best_char = NONE;
for (c = 0 ; c <= MAXCHAR ; c++) {
score = ec_cscore(eci, firstpos, c);
if (score > 0.0) {
count += 1.0;
total_score += score;
}
if (score > best_score) {
best_score = score;
best_char = c;
}
}
#if DEBUG
printf("Total score is %7.4f", total_score);
printf(". Count is %4.0f.\n", count);
#endif
if (total_score == 0.0 || count == 0.0 || best_char == NONE) {
#if DEBUG
printf("NO GUESSES\n");
#endif
return(NONE);
}
#if DEBUG
printf("Best score is %7.4f", best_score);
printf(", which is %7.4f fraction of total", best_score/total_score);
printf(".\n");
decode_class(eci, firstpos, best_char, pvec);
pvec2str(str, pvec);
printf("The best chars are '%s.\n", str);
#endif
if (best_score > alevel * (total_score - best_score)) {
return(best_char);
}
else {
return(NONE);
}
}
/* Fill in equiv class info from given ciphertext block
* and permutation.
*/
void ec_init(char cipher[], int perm[], ecinfo *eci)
{
int i;
int lastmember;
int firstpos, size;
eci->sizelast = 0;
eci->sizemin = 2;
for (i = 0 ; i < BLOCKSIZE ; i++) {
eci->ciphertext[i] = cipher[i];
eci->scipher[i] = (cipher[i] + i)&MODMASK;
eci->perm[i] = perm[i];
eci->permmap[i] = NONE;
}
decode(eci->ciphertext, eci->plaintext, eci->perm);
/* The permmap points to the most recent member we have seen */
/* of each known class, or a NONE. Ptrs are array indexes. */
for (i = BLOCKSIZE-1 ; i >= 0 ; i--) {
eci->next[i] = i;
if ((lastmember = eci->permmap[eci->scipher[i]]) != NONE) {
eci->next[i] = eci->next[lastmember];
eci->next[lastmember] = i;
}
eci->permmap[eci->scipher[i]] = i;
}
for (i = 0 ; i < BLOCKSIZE ; i++) {
firstpos = eci->permmap[i];
if (firstpos != NONE) {
size = ec_compsize(eci, firstpos);
ec_addsize(eci, size, firstpos);
}
}
}
/* Add an entry to the size list.
* Implementation: Find the first slot before sizelast+1 that
* has a size less than the size arg. Shuffle down the list
* to create a hole and insert the new entry.
*/
void ec_addsize(eci, size, firstmember)
register ecinfo *eci;
int size;
int firstmember;
{
int k; /* Slot where new entry will go. */
int i;
ecsize sizeinfo;
if (size < eci->sizemin) return;
sizeinfo.size = size;
sizeinfo.firstpos = firstmember;
for (k = 0 ; k < eci->sizelast ; k++) {
if (eci->sizelist[k].size < size) break;
}
if (k >= SZMAX) return;
for (i = eci->sizelast ; i > k ; i--) {
eci->sizelist[i] = eci->sizelist[i-1];
}
eci->sizelast++;
eci->sizelist[k] = sizeinfo;
}
/* Compute the size of a clas given a pointer to one of its members.
*/
int ec_compsize(ecinfo *eci, int member)
{
int size;
int position;
int firstflag;
size = 0;
for_pos_in_class(position, member) {
size++;
}
return(size);
}