-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbdriver.c
215 lines (176 loc) · 4.03 KB
/
bdriver.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
/*
* Test driver for BIGRAM character guessing stuff.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "window.h"
#include "specs.h"
#include "cipher.h"
#define NDOBLOCKS 2 /* Number of blocks to do. */
#define DEBUG FALSE
ecinfo myecinfo;
char plainbuf[BLOCKSIZE+1];
float accept_level;
float prob_cutoff;
extern int ec_best();
extern void ec_dplain();
extern void ec_dscipher();
extern void ec_dnext();
extern void ec_dsizetab();
extern void ec_dperm();
extern void ec_dpmap();
extern void ec_init();
extern void ec_cscore();
extern void lp_init();
extern int lp_best_char();
extern void lp_cscore();
extern void lp_accept();
extern int lp_best_pos();
extern void lp_dclasses();
extern char mcbuf[];
extern char *fname; /* Used by fillcbuf. */
void do_lp_block();
/* Test routine for equiv class info. */
int main(argc, argv)
int argc;
char *argv[];
{
ecinfo *eci;
FILE *inp;
FILE *sout, *sin;
int blknum;
int maxblock;
long filelength;
char infile[100];
char inplain[100];
char *plain = ".txt";
char *code = ".cipher";
char *p, *q;
sout = stdout; /* For use within debugger, dbx. */
sin = stdin;
if (argc != 4) {
printf("Usage: %s input_file_root acceptance_level prob_cutoff\n",
argv[0]);
exit(0);
}
p = inplain;
q = argv[1];
while ((*p++ = *q++));
--p;
q = plain;
while ((*p++ = *q++));
p = infile;
q = argv[1];
while ((*p++ = *q++));
--p;
q = code;
while ((*p++ = *q++));
if (sscanf(argv[2], "%f", &accept_level) != 1) {
printf("Could not parse the acceptance level from %s.\n", argv[2]);
exit(0);
}
if (sscanf(argv[3], "%f", &prob_cutoff) != 1) {
printf("Could not parse the probability cutoff from %s.\n", argv[2]);
exit(0);
}
printf("\t\tEquivalence Class Guessing\n\n");
printf("Filename = %s. Acceptance level = %4.2f\n",infile,accept_level);
printf("Loading statistics ...");
printf(" 1");
load_1stats_from("mss.stats");
printf(" 2");
load_2stats_from("mss-bigram.stats");
printf(" done.\n");
eci = &myecinfo;
if ((inp = fopen(infile, "r")) == NULL) {
printf("\nCannot open %s for reading.\n", infile);
exit(0);
}
fseek(inp, 0L, 2);
filelength = ftell(inp);
fclose(inp);
maxblock = filelength / BLOCKSIZE;
if (maxblock > (NDOBLOCKS-1)) maxblock = (NDOBLOCKS-1);
for (blknum = 0 ; blknum <= maxblock ; blknum++) {
do_lp_block(eci, blknum, infile, inplain);
}
return 0;
}
/* Do a block using the letter pair statistics.
*/
void do_lp_block(eci, blknum, cfile, plainfile)
reg ecinfo *eci;
int blknum;
char *cfile, *plainfile;
{
int i;
reg int c;
int ntried;
int naccepted, nwrong;
reg int classpos;
int charcount;
int *permp, repeat;
cipherfile = cfile;
fillcbuf(blknum, mcbuf);
cipherfile = plainfile;
fillcbuf(blknum, plainbuf);
lp_init(mcbuf, refperm(blknum), eci);
for(repeat = 0 ; repeat < 3 ; repeat++) {
naccepted = 0;
nwrong = 0;
ntried = 0;
for (ntried = 0 ; ntried < BLOCKSIZE ; ntried++) {
classpos = lp_best_pos(eci, 2);
if (classpos == NONE)
break;
c = lp_best_char(eci, classpos,
accept_level - ((repeat == 0) ? 0.0 : 0.0),
prob_cutoff);
if (c != NONE) {
lp_accept(eci, classpos, c);
naccepted++;
#if DEBUG
printf("ACCEPTED");
#endif
if (plainbuf[classpos] != c) {
nwrong++;
#if DEBUG
printf(" -- INCORRECT");
#endif
}
#if DEBUG
printf("\n");
#endif
}
}
/* decode(eci->ciphertext, eci->plaintext, eci->perm);
*/
for (i = 0 ; i < eci->nclasses ; i++) {
eci->classlist[i].changed = TRUE;
}
charcount = 0;
for (i = 0 ; i < BLOCKSIZE ; i++) {
if (eci->plaintext[i] != NONE) charcount++;
}
printf("\n\nPlaintext for block %d using %d wires", blknum, naccepted);
printf(" (%d wrong)", nwrong);
printf(" yields %d characters.", charcount);
printf("\nThere were %d classes and %d guess tries.",eci->nclasses,ntried);
printf("\n\n");
ec_dplain(stdout, eci);
}
permp = refperm(blknum);
for (i = 0 ; i < BLOCKSIZE ; i++) {
permp[i] = eci->perm[i];
}
}
key u_getkey(void)
{
return 0;
}
keyer topktab[] ={{0, NULL}};
char *quitcmd(void)
{
return NULL;
}