-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphillips.c
More file actions
822 lines (721 loc) · 22 KB
/
Copy pathphillips.c
File metadata and controls
822 lines (721 loc) · 22 KB
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
* phillips.c --
*
* This file implements the phillips cipher type.
*
* Copyright (c) 1998-2005 Michael Thomas <wart@kobold.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <tcl.h>
#include <string.h>
#include <cipher.h>
#include <cipherDebug.h>
#define PERIOD 5
#define DEFAULT_NUM_BLOCKS 8
#define MAX_NUM_BLOCKS 20
static int CreatePhillips _ANSI_ARGS_((Tcl_Interp *interp,
CipherItem *, int, const char **));
void DeletePhillips _ANSI_ARGS_((ClientData));
static char *GetPhillips _ANSI_ARGS_((Tcl_Interp *, CipherItem *));
static int SetPhillips _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *));
static int RestorePhillips _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, const char *));
static int SolvePhillips _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
char *));
int PhillipsCmd _ANSI_ARGS_((ClientData, Tcl_Interp *,
int, const char **));
static int PhillipsUndo _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, int));
static int PhillipsSubstitute _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, const char *, int));
static int PhillipsLocateTip _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, const char *));
static int PhillipsSwapCols _ANSI_ARGS_((Tcl_Interp *, CipherItem *, int,
int));
static int EncodePhillips _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, const char *));
static char *PhillipsTransform _ANSI_ARGS_((CipherItem *, const char *, int));
typedef struct PhillipsItem {
CipherItem header;
/*
* The key for a phillips cipher is the keyblock for the first square.
* We derive the other 7 squares only when needed, ie, when we need
* to extract the plaintext.
*/
char key[PERIOD][PERIOD];
char keyValPos[PERIOD*PERIOD+1];
int numBlocks;
char *pt;
char **maxSolKey;
int maxSolVal;
} PhillipsItem;
CipherType PhillipsType = {
"phillips",
ATOZ,
sizeof(PhillipsItem),
CreatePhillips, /* create proc */
DeletePhillips, /* delete proc */
PhillipsCmd, /* cipher command proc */
GetPhillips, /* get plaintext proc */
SetPhillips, /* show ciphertext proc */
SolvePhillips, /* solve cipher proc */
RestorePhillips, /* restore proc */
PhillipsLocateTip, /* locate proc */
PhillipsSubstitute, /* sub proc */
PhillipsUndo, /* undo proc */
EncodePhillips, /* encode proc */
(CipherType *)NULL /* next cipher type */
};
static int
CreatePhillips(Tcl_Interp *interp, CipherItem *itemPtr, int argc, const char **argv)
{
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
char temp_ptr[128];
Tcl_DString dsPtr;
int i;
int j;
philPtr->header.period = 0;
philPtr->maxSolVal = 0;
philPtr->maxSolKey = (char **)NULL;
philPtr->numBlocks = DEFAULT_NUM_BLOCKS;
philPtr->pt = (char *)NULL;
for(i=0; i < PERIOD; i++) {
for(j=0; j < PERIOD; j++) {
philPtr->key[i][j] = '\0';
}
}
for(i=0; i < 26; i++) {
philPtr->keyValPos[i] = 0;
}
sprintf(temp_ptr, "cipher%d", cipherid);
Tcl_DStringInit(&dsPtr);
Tcl_DStringAppendElement(&dsPtr, temp_ptr);
Tcl_DStringAppendElement(&dsPtr, "configure");
for(i=0;i<argc;i++) {
Tcl_DStringAppendElement(&dsPtr, argv[i]);
}
Tcl_CreateCommand(interp, temp_ptr, PhillipsCmd, itemPtr,
itemPtr->typePtr->deleteProc);
if (argc) {
if (Tcl_Eval(interp, Tcl_DStringValue(&dsPtr)) != TCL_OK) {
Tcl_DeleteCommand(interp, temp_ptr);
Tcl_DStringFree(&dsPtr);
return TCL_ERROR;
}
}
Tcl_SetResult(interp, temp_ptr, TCL_VOLATILE);
Tcl_DStringFree(&dsPtr);
return TCL_OK;
}
void
DeletePhillips(ClientData clientData)
{
PhillipsItem *philPtr = (PhillipsItem *)clientData;
if (philPtr->pt != NULL) {
ckfree(philPtr->pt);
}
DeleteCipher(clientData);
}
static int
SetPhillips(Tcl_Interp *interp, CipherItem *itemPtr, const char *ctext)
{
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
char *c;
int valid = TCL_OK,
length=0;
length = CountValidChars(itemPtr, ctext, (int *)NULL);
if (!length) {
Tcl_SetResult(interp, "No valid characters found in the ciphertext",
TCL_STATIC);
return TCL_ERROR;
}
c = ExtractValidChars(itemPtr, ctext);
if (!c) {
Tcl_SetResult(interp, "Could not extract ciphertext from string",
TCL_STATIC);
return TCL_ERROR;
}
valid = TCL_OK;
if (valid==TCL_OK) {
itemPtr->length = length;
if (itemPtr->ciphertext) {
ckfree(itemPtr->ciphertext);
}
itemPtr->ciphertext = c;
if (philPtr->pt) {
ckfree(philPtr->pt);
}
philPtr->pt = (char *)ckalloc(sizeof(char) * length + 1);
if (itemPtr->ciphertext == NULL || philPtr->pt == NULL) {
Tcl_SetResult(interp, "Error mallocing memory for new cipher",
TCL_STATIC);
return TCL_ERROR;
}
itemPtr->length = length;
Tcl_SetResult(interp, itemPtr->ciphertext, TCL_STATIC);
}
return valid;
}
static int
PhillipsUndo(Tcl_Interp *interp, CipherItem *itemPtr, const char *ct, int offset)
{
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
int i, j;
if (! ct) {
for(i=0; i < PERIOD; i++) {
for(j=0; j < PERIOD; j++) {
philPtr->key[i][j] = '\0';
}
}
for(i=0; i < 26; i++) {
philPtr->keyValPos[i] = 0;
}
} else {
while (*ct) {
int keyVal;
if (*ct < 'a' || *ct > 'z') {
Tcl_SetResult(interp, "Invalid key value", TCL_STATIC);
return TCL_ERROR;
}
keyVal = philPtr->keyValPos[*ct-'a'];
if (keyVal) {
keyVal--;
philPtr->key[keyVal/PERIOD][keyVal%PERIOD]='\0';
}
ct++;
}
}
return TCL_OK;
}
static int
PhillipsSubstitute(Tcl_Interp *interp, CipherItem *itemPtr, const char *ct, const char *pt, int offset)
{
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
int row, col;
/*
* pt == row
* ct == col
* offset == val
*/
row = *ct - '1';
col = *pt - '1';
if (row < 0 || row >= PERIOD) {
Tcl_SetResult(interp, "Invalid row specification", TCL_STATIC);
return TCL_ERROR;
}
if (col < 0 || col >= PERIOD) {
Tcl_SetResult(interp, "Invalid column specification", TCL_STATIC);
return TCL_ERROR;
}
if (offset < 'a' || offset > 'z') {
Tcl_SetResult(interp, "Invalid key value", TCL_STATIC);
return TCL_ERROR;
}
philPtr->key[row][col] = offset;
philPtr->keyValPos[offset-'a'] = row*PERIOD+col+1;
return TCL_OK;
}
static char *
GetPhillips(Tcl_Interp *interp, CipherItem *itemPtr)
{
if (itemPtr->length == 0) {
Tcl_SetResult(interp, "Can't do anything until ciphertext has been set",
TCL_STATIC);
return (char *)NULL;
}
return PhillipsTransform(itemPtr, itemPtr->ciphertext, DECODE);
}
/*
* Encode/decode a phillips cipher. The memory for the result string
* is managed by the cipher, so don't try to free it yourself!
*/
static char *
PhillipsTransform(CipherItem *itemPtr, const char *text, int mode) {
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
int i;
int keyRowToBlock[20][5] = {{0, 1, 2, 3, 4},
{1, 0, 2, 3, 4},
{2, 0, 1, 3, 4},
{3, 0, 1, 2, 4},
{4, 0, 1, 2, 3},
{4, 1, 0, 2, 3},
{4, 2, 0, 1, 3},
{4, 3, 0, 1, 2},
{3, 4, 0, 1, 2},
{3, 4, 1, 0, 2},
{3, 4, 2, 0, 1},
{2, 4, 3, 0, 1},
{2, 3, 4, 0, 1},
{2, 3, 4, 1, 0},
{1, 3, 4, 2, 0},
{1, 2, 4, 3, 0},
{1, 2, 3, 4, 0},
{0, 2, 3, 4, 1},
{0, 1, 3, 4, 2},
{0, 1, 2, 4, 3}};
int blockRowToKey[20][5] = {{0, 1, 2, 3, 4},
{1, 0, 2, 3, 4},
{1, 2, 0, 3, 4},
{1, 2, 3, 0, 4},
{1, 2, 3, 4, 0},
{2, 1, 3, 4, 0},
{2, 3, 1, 4, 0},
{2, 3, 4, 1, 0},
{2, 3, 4, 0, 1},
{3, 2, 4, 0, 1},
{3, 4, 2, 0, 1},
{3, 4, 0, 2, 1},
{3, 4, 0, 1, 2},
{4, 3, 0, 1, 2},
{4, 0, 3, 1, 2},
{4, 0, 1, 3, 2},
{4, 0, 1, 2, 3},
{0, 4, 1, 2, 3},
{0, 1, 4, 2, 3},
{0, 1, 2, 4, 3}};
for(i=0; i < itemPtr->length; i++) {
char ct = text[i];
char pt = ' ';
int block = 0;
int keyPos = philPtr->keyValPos[ct-'a'];
int ctKeyCol;
int ptKeyCol;
int ctKeyRow;
int ptKeyRow;
int ctBlockRow;
int ptBlockRow;
if (keyPos) {
block = (i/5)%philPtr->numBlocks;
keyPos--;
ctKeyCol = keyPos%5;
ctKeyRow = keyPos/5;
if (mode == DECODE) {
ptKeyCol = ctKeyCol - 1;
if (ptKeyCol < 0) {
ptKeyCol = 4;
}
} else {
ptKeyCol = ctKeyCol + 1;
if (ptKeyCol > 4) {
ptKeyCol = 0;
}
}
ctBlockRow = keyRowToBlock[block][ctKeyRow];
if (mode == DECODE) {
ptBlockRow = ctBlockRow - 1;
if (ptBlockRow < 0) {
ptBlockRow = 4;
}
} else {
ptBlockRow = ctBlockRow + 1;
if (ptBlockRow > 4) {
ptBlockRow = 0;
}
}
ptKeyRow = blockRowToKey[block][ptBlockRow];
pt = philPtr->key[ptKeyRow][ptKeyCol];
if (!pt) {
pt = ' ';
}
}
philPtr->pt[i] = pt;
}
philPtr->pt[itemPtr->length] = '\0';
return philPtr->pt;
}
static int
RestorePhillips(Tcl_Interp *interp, CipherItem *itemPtr, const char *key, const char *dummy)
{
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
int i;
int row;
int col;
char used[26];
if (itemPtr->length <= 0) {
Tcl_SetResult(interp, "Can't do anything until ciphertext has been set",
TCL_STATIC);
return TCL_ERROR;
}
if (strlen(key) != PERIOD * PERIOD) {
Tcl_SetResult(interp, "Invalid key length.", TCL_STATIC);
return TCL_ERROR;
}
for(i=0; i < PERIOD*PERIOD; i++) {
philPtr->keyValPos[i] = 0;
}
for(i=0; i < 26; i++) {
used[i] = '\0';
}
for(i=0; i < PERIOD*PERIOD; i++) {
if ( (key[i] < 'a' || key[i] > 'z') && (key[i] != ' ')) {
Tcl_SetResult(interp, "Invalid character in key", TCL_STATIC);
return TCL_ERROR;
}
row = i / PERIOD;
col = i % PERIOD;
if (key[i] == ' ') {
philPtr->key[row][col] = '\0';
} else {
philPtr->key[row][col] = key[i];
philPtr->keyValPos[key[i]-'a'] = i+1;
}
}
/*
* Look for duplicates in the key
*/
for(i=0; i < PERIOD*PERIOD; i++) {
row = i / PERIOD;
col = i % PERIOD;
if (philPtr->key[row][col] && used[philPtr->key[row][col]-'a']) {
char badChar[2];
badChar[0] = philPtr->key[row][col];
badChar[1] = '\0';
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "Duplicate character in key: ", badChar, (char *)NULL);
return TCL_ERROR;
}
used[philPtr->key[row][col]-'a'] = 1;
}
return TCL_OK;
}
static int
SolvePhillips(Tcl_Interp *interp, CipherItem *itemPtr, char *maxkey)
{
Tcl_SetResult(interp, "Solving phillips ciphers is not yet implemented.",
TCL_STATIC);
return TCL_ERROR;
}
static int
PhillipsLocateTip(Tcl_Interp *interp, CipherItem *itemPtr, const char *tip, const char *start)
{
Tcl_SetResult(interp,
"No locate tip function defined for phillips ciphers.",
TCL_STATIC);
return TCL_ERROR;
}
static int
PhillipsSwapCols(Tcl_Interp *interp, CipherItem *itemPtr, int col1, int col2)
{
PhillipsItem *philPtr = (PhillipsItem *)itemPtr;
int i, tempCol, row, col;
if (col1 < 1 || col2 < 1 || col1 > PERIOD || col2 > PERIOD) {
Tcl_SetResult(interp, "Invalid column in phillips swap", TCL_STATIC);
return TCL_ERROR;
}
col1--;
col2--;
for(i=0; i < PERIOD; i++) {
tempCol = philPtr->key[i][col1];
philPtr->key[i][col1] = philPtr->key[i][col2];
philPtr->key[i][col2] = tempCol;
}
for(i=0; i < 26; i++) {
philPtr->keyValPos[i] = 0;
}
for(row=0; row < PERIOD; row++) {
for(col=0; col < PERIOD; col++) {
int keyPos = row*PERIOD+col;
if (philPtr->key[keyPos]) {
philPtr->keyValPos[philPtr->key[row][col]-'a'] = keyPos+1;
}
}
}
return TCL_OK;
}
int
PhillipsCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
{
PhillipsItem *philPtr = (PhillipsItem *)clientData;
CipherItem *itemPtr = (CipherItem *)clientData;
char temp_str[256];
const char *cmd;
int i;
char *tPtr=(char *)NULL;
cmd = *argv;
argv++, argc--;
if (argc == 0) {
Tcl_AppendResult(interp, "Usage: ", cmd, " ?option?", (char *)NULL);
return TCL_ERROR;
}
if (**argv == 'c' && (strncmp(*argv, "cget", 2) == 0)) {
if (argc != 2) {
Tcl_AppendResult(interp, "Usage: ", cmd, " cget option", (char *)NULL);
return TCL_ERROR;
}
if (strncmp(argv[1], "-length", 6) == 0) {
sprintf(temp_str, "%d", philPtr->header.length);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-period", 6) == 0) {
sprintf(temp_str, "%d", PERIOD);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-blocks", 6) == 0) {
sprintf(temp_str, "%d", philPtr->numBlocks);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-ciphertext", 10) == 0 ||
strncmp(argv[1], "-ctext", 3) == 0) {
if (!philPtr->header.ciphertext) {
Tcl_SetResult(interp, "{}", TCL_STATIC);
} else {
Tcl_SetResult(interp, philPtr->header.ciphertext, TCL_VOLATILE);
}
return TCL_OK;
} else if (strncmp(argv[1], "-plaintext", 9) == 0 ||
strncmp(argv[1], "-ptext", 3) == 0) {
tPtr = (itemPtr->typePtr->decipherProc)(interp, itemPtr);
if (!tPtr) {
return TCL_ERROR;
}
Tcl_SetResult(interp, tPtr, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-key", 4) == 0) {
for(i=0; i < PERIOD; i++) {
int j;
for(j=0; j < PERIOD; j++) {
if (philPtr->key[i][j]) {
temp_str[j+i*PERIOD] = philPtr->key[i][j];
} else {
temp_str[j+i*PERIOD] = ' ';
}
}
}
temp_str[PERIOD*PERIOD] = '\0';
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-type", 5) == 0) {
Tcl_SetResult(interp, itemPtr->typePtr->type, TCL_STATIC);
return TCL_OK;
} else if (strncmp(argv[1], "-stepinterval", 6) == 0) {
sprintf(temp_str, "%ld", itemPtr->stepInterval);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-stepcommand", 6) == 0) {
if (itemPtr->stepCommand) {
Tcl_SetResult(interp, itemPtr->stepCommand, TCL_VOLATILE);
} else {
Tcl_SetResult(interp, "", TCL_STATIC);
}
return TCL_OK;
} else if (strncmp(argv[1], "-bestfitcommand", 6) == 0) {
if (itemPtr->bestFitCommand) {
Tcl_SetResult(interp, itemPtr->bestFitCommand, TCL_VOLATILE);
} else {
Tcl_SetResult(interp, "", TCL_STATIC);
}
return TCL_OK;
} else if (strncmp(argv[1], "-language", 8) == 0) {
Tcl_SetResult(interp, cipherGetLanguage(itemPtr->language),
TCL_VOLATILE);
return TCL_OK;
} else {
sprintf(temp_str, "Unknown option %s", argv[1]);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_ERROR;
}
return TCL_OK;
} else if (**argv == 'c' && (strncmp(*argv, "configure", 2) == 0)) {
if (argc < 3 || (argc%2 != 1)) {
Tcl_AppendResult(interp, "Usage: ", cmd, " configure ?option value?", (char *)NULL);
return TCL_ERROR;
}
argc--, argv++;
while(argc > 0) {
if (strncmp(*argv, "-stepinterval", 12) == 0) {
if (sscanf(argv[1], "%d", &i) != 1) {
Tcl_SetResult(interp, "Invalid interval.", TCL_STATIC);
return TCL_ERROR;
}
if (i < 0 ) {
Tcl_SetResult(interp, "Invalid interval.", TCL_STATIC);
return TCL_ERROR;
}
itemPtr->stepInterval = i;
} else if (strncmp(*argv, "-bestfitcommand", 14) == 0) {
if (CipherSetBestFitCmd(itemPtr, argv[1]) != TCL_OK) {
return TCL_ERROR;
}
} else if (strncmp(*argv, "-stepcommand", 14) == 0) {
if (CipherSetStepCmd(itemPtr, argv[1]) != TCL_OK) {
return TCL_ERROR;
}
} else if (strncmp(*argv, "-blocks", 7) == 0) {
if (sscanf(argv[1], "%d", &i) != 1) {
Tcl_AppendResult(interp,
"Bad number of blocks. Integer expected: ",
argv[1], (char *)NULL);
return TCL_ERROR;
}
if (i <= 0 || i > MAX_NUM_BLOCKS) {
Tcl_AppendResult(interp,
"Number of phillips blocks must be between 1 and 20. found: ",
argv[1], (char *)NULL);
return TCL_ERROR;
}
philPtr->numBlocks = i;
} else if (strncmp(*argv, "-ciphertext", 10) == 0 ||
strncmp(*argv, "-ctext", 3) == 0) {
if ((itemPtr->typePtr->setctProc)(interp, itemPtr, argv[1]) != TCL_OK) {
return TCL_ERROR;
}
} else if (strncmp(*argv, "-language", 8) == 0) {
itemPtr->language = cipherSelectLanguage(argv[1]);
Tcl_SetResult(interp, cipherGetLanguage(itemPtr->language),
TCL_VOLATILE);
} else {
sprintf(temp_str, "Unknown option %s", *argv);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_ERROR;
}
argc-=2, argv+=2;
}
return TCL_OK;
} else if (**argv == 'e' && (strncmp(*argv, "encode", 1) == 0)) {
if (argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd, " encode pt key",
(char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->encodeProc)(interp, itemPtr, argv[1], argv[2]);
} else if (**argv == 's' && (strncmp(*argv, "swap", 4) == 0)) {
int col1, col2;
if (argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd,
" swap col1 col2", (char *)NULL);
return TCL_ERROR;
} else {
if (sscanf(argv[1], "%d", &col1) != 1) {
Tcl_SetResult(interp,
"Invalid column value. Value must be between 1 and 5.",
TCL_STATIC);
return TCL_ERROR;
}
if (sscanf(argv[2], "%d", &col2) != 1) {
Tcl_SetResult(interp,
"Invalid column value. Value must be between 1 and 5.",
TCL_STATIC);
return TCL_ERROR;
}
}
return PhillipsSwapCols(interp, itemPtr, col1, col2);
} else if (**argv == 'l' && (strncmp(*argv, "locate", 3) == 0)) {
Tcl_SetResult(interp,
"No locate tip function defined for phillips ciphers.",
TCL_STATIC);
return TCL_ERROR;
} else if (**argv == 's' && (strncmp(*argv, "substitute", 3) == 0)) {
if (argc != 4) {
Tcl_AppendResult(interp, "Usage: ", cmd,
" substitute row col val", (char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->subProc)(interp, itemPtr, argv[1], argv[2], argv[3][0]);
} else if (**argv == 'r' && (strncmp(*argv, "restore", 7) == 0)) {
if (argc != 2 && argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd,
" restore key ?junk?", (char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->restoreProc)(interp, itemPtr, argv[1],
(char *)NULL);
} else if (**argv == 'e' && (strncmp(*argv, "encode", 1) == 0)) {
if (argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd, " encode pt key",
(char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->encodeProc)(interp, itemPtr, argv[1], argv[2]);
} else if (**argv == 'u' && (strncmp(*argv, "undo", 4) == 0)) {
if (argc == 1) {
return (itemPtr->typePtr->undoProc)(interp, itemPtr, (char *)NULL, 0);
}
if (argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd,
" undo row col", (char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->subProc)(interp, itemPtr, argv[1], argv[2], 0);
} else if (**argv == 's' && (strncmp(*argv, "solve", 5) == 0)) {
return (itemPtr->typePtr->solveProc)(interp, itemPtr, temp_str);
} else {
Tcl_AppendResult(interp, "Unknown option ", *argv, (char *)NULL);
Tcl_AppendResult(interp, "\nMust be one of: ", cmd,
" cget ?option?", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" configure ?option value?", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" restore key", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" solve", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" substitute row col val", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" swap row1 row2", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" undo row col", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" encode pt key", (char *)NULL);
return TCL_ERROR;
}
}
static int
EncodePhillips(Tcl_Interp *interp, CipherItem *itemPtr, const char *pt, const char *key) {
char *ct = (char *)NULL;
int count;
const char **argv;
if (Tcl_SplitList(interp, key, &count, &argv) != TCL_OK) {
return TCL_ERROR;
}
if (count != 1) {
ckfree((char *)argv);
Tcl_AppendResult(interp, "Invalid number of items in encoding key '",
key, "'. Should have found 1.", (char *)NULL);
return TCL_ERROR;
}
if (strlen(argv[0]) != 25) {
Tcl_SetResult(interp, "Invalid length of key.", TCL_STATIC);
ckfree((char *)argv);
return TCL_ERROR;
}
if ((itemPtr->typePtr->setctProc)(interp, itemPtr, pt) != TCL_OK) {
ckfree((char *)argv);
return TCL_ERROR;
}
if ((itemPtr->typePtr->restoreProc)(interp, itemPtr, argv[0], (char *)NULL) != TCL_OK) {
ckfree((char *)argv);
return TCL_ERROR;
}
ct = PhillipsTransform(itemPtr, itemPtr->ciphertext, ENCODE);
if (ct == (char *)NULL) {
ckfree((char *)argv);
return TCL_ERROR;
}
if ((itemPtr->typePtr->setctProc)(interp, itemPtr, ct) != TCL_OK) {
ckfree((char *)argv);
return TCL_ERROR;
}
if ((itemPtr->typePtr->restoreProc)(interp, itemPtr, argv[0], (char *)NULL) != TCL_OK) {
ckfree((char *)argv);
return TCL_ERROR;
}
Tcl_SetResult(interp, itemPtr->ciphertext, TCL_VOLATILE);
ckfree((char *)argv);
return TCL_OK;
}
#undef PERIOD