-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnitrans.c
More file actions
888 lines (760 loc) · 24.3 KB
/
Copy pathnitrans.c
File metadata and controls
888 lines (760 loc) · 24.3 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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/*
* nitrams.c --
*
* This file implements the nihilist transposition cipher type.
*
* Copyright (c) 1995-2004 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 <score.h>
#include <math.h>
#include <perm.h>
#include <cipherDebug.h>
static int CreateNitrans _ANSI_ARGS_((Tcl_Interp *interp,
CipherItem *, int, const char **));
void DeleteNitrans _ANSI_ARGS_((ClientData));
static char *GetNitrans _ANSI_ARGS_((Tcl_Interp *, CipherItem *));
static int SetNitrans _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *));
static int RestoreNitrans _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, const char *));
static int SolveNitrans _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
char *));
int NitransCmd _ANSI_ARGS_((ClientData, Tcl_Interp *,
int, const char **));
static int NitransUndo _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, int));
static void NitransInitKey _ANSI_ARGS_((CipherItem *));
static int NitransSwapVals _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
int, int));
int NitransCheckSolutionValue _ANSI_ARGS_((Tcl_Interp *, ClientData,
int *, int));
static int NitransShiftColumn _ANSI_ARGS_((Tcl_Interp *, CipherItem *, int,
int));
static int EncodeNitrans _ANSI_ARGS_((Tcl_Interp *, CipherItem *,
const char *, const char *));
static char *NitransTransform _ANSI_ARGS_((CipherItem *, const char *, int));
#define VERTICAL 1
#define HORIZONTAL 2
typedef struct NitransItem {
CipherItem header;
char *key; /* Nitrans used to write in the orignal plaintext */
char *maxKey; /* Nitrans used to write in the orignal plaintext */
double maxVal;
int readDir;
} NitransItem;
CipherType NitransType = {
"nitrans",
"abcdefghijklmnopqrstuvwxyz-0123456789",
sizeof(NitransItem),
CreateNitrans, /* create proc */
DeleteNitrans, /* delete proc */
NitransCmd, /* cipher command proc */
GetNitrans, /* get plaintext proc */
SetNitrans, /* show ciphertext proc */
SolveNitrans, /* solve cipher proc */
RestoreNitrans, /* restore proc */
(LocateTipProc *)NULL, /* locate proc */
(SubstituteProc *)NULL, /* sub proc */
NitransUndo, /* undo proc */
EncodeNitrans, /* encode proc */
(CipherType *)NULL /* next cipher type */
};
static int
CreateNitrans(Tcl_Interp *interp, CipherItem *itemPtr, int argc, const char **argv)
{
NitransItem *nitransPtr = (NitransItem *)itemPtr;
char temp_ptr[128];
Tcl_DString dsPtr;
int i;
nitransPtr->header.period = 0;
nitransPtr->key = (char *)NULL;
nitransPtr->maxKey = (char *)NULL;
nitransPtr->maxVal = 0.0;
nitransPtr->readDir = VERTICAL;
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, NitransCmd, 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
DeleteNitrans(ClientData clientData)
{
NitransItem *nitransPtr = (NitransItem *)clientData;
if (nitransPtr->key != NULL) {
ckfree(nitransPtr->key);
}
DeleteCipher(clientData);
}
static int
SetNitrans(Tcl_Interp *interp, CipherItem *itemPtr, const char *ctext)
{
char *c;
int valid = TCL_OK,
width = 0,
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;
}
width = sqrt(length);
if (width * width != length) {
ckfree(c);
Tcl_SetResult(interp,
"Invalid cipher length. Length must be a perfect square.",
TCL_STATIC);
return TCL_ERROR;
}
valid = TCL_OK;
if (valid==TCL_OK) {
if (itemPtr->ciphertext) {
ckfree(itemPtr->ciphertext);
}
itemPtr->ciphertext = c;
if (itemPtr->ciphertext == NULL) {
Tcl_SetResult(interp,
"Error mallocing memory for new cipher",
TCL_STATIC);
return TCL_ERROR;
}
itemPtr->length = length;
itemPtr->period = width;
NitransInitKey(itemPtr);
Tcl_SetResult(interp, itemPtr->ciphertext, TCL_STATIC);
}
return valid;
}
static int
NitransUndo(Tcl_Interp *interp, CipherItem *itemPtr, const char *ct, int offset)
{
NitransInitKey(itemPtr);
return TCL_OK;
}
static void
NitransInitKey(CipherItem *itemPtr)
{
NitransItem *nitransPtr = (NitransItem *)itemPtr;
int i;
if (nitransPtr->key) {
ckfree(nitransPtr->key);
}
nitransPtr->key = (char *)NULL;
if (itemPtr->period) {
nitransPtr->key = (char *)ckalloc(sizeof(char) * itemPtr->period);
for(i=0; i < itemPtr->period; i++) {
nitransPtr->key[i] = i;
}
}
}
static int
NitransSwapVals(Tcl_Interp *interp, CipherItem *itemPtr, int val1, int val2)
{
NitransItem *nitransPtr = (NitransItem *)itemPtr;
int i;
int tcol1;
int tcol2;
if (itemPtr->period < 1) {
Tcl_SetResult(interp,
"Can't swap columns/rows until ciphertext has been set",
TCL_STATIC);
return TCL_ERROR;
}
if (val1 < 0 || val2 < 0 || val1 == val2
|| val2 >= itemPtr->period || val2 >= itemPtr->period) {
Tcl_SetResult(interp, "Invalid column", TCL_STATIC);
return TCL_ERROR;
}
for(tcol1 = 0; nitransPtr->key[tcol1] != val1; tcol1++);
for(tcol2 = 0; nitransPtr->key[tcol2] != val2; tcol2++);
i = nitransPtr->key[tcol1];
nitransPtr->key[tcol1] = nitransPtr->key[tcol2];
nitransPtr->key[tcol2] = i;
return TCL_OK;
}
static int
NitransShiftColumn(Tcl_Interp *interp, CipherItem *itemPtr, int col, int amount)
{
NitransItem *nitransPtr = (NitransItem *)itemPtr;
int i;
int start=col;
int tempKeyVal;
if (itemPtr->ciphertext == (char *)NULL) {
Tcl_SetResult(interp,
"Can't do anything until ciphertext has been set",
TCL_STATIC);
return TCL_ERROR;
}
if (col < 0 || col >= itemPtr->period) {
Tcl_SetResult(interp, "Bad column index", TCL_STATIC);
return TCL_ERROR;
}
if (amount == 0) {
/*
* No-op.
*/
return TCL_OK;
}
/*
* The user specified the columns by label, not absolute position.
* We need to convert this label to the absolute position to do
* our work.
*/
for(i=0; i < itemPtr->period; i++) {
if (col == nitransPtr->key[i]) {
start = i;
}
}
if (start + amount < 0) {
Tcl_SetResult(interp, "Can't shift backwards past the first column.", TCL_STATIC);
return TCL_ERROR;
}
tempKeyVal = nitransPtr->key[start];
if (amount < 0) {
for (i=0; i > amount; i--) {
int startIndex = (start + i + itemPtr->period)%itemPtr->period;
int endIndex = (start + i - 1 + itemPtr->period)%itemPtr->period;
nitransPtr->key[startIndex] = nitransPtr->key[endIndex];
}
} else {
for (i=0; i < amount; i++) {
int startIndex = (start + i + itemPtr->period)%itemPtr->period;
int endIndex = (start + i + 1 + itemPtr->period)%itemPtr->period;
nitransPtr->key[startIndex] = nitransPtr->key[endIndex];
}
}
nitransPtr->key[start+i] = tempKeyVal;
return TCL_OK;
}
static int
RestoreNitrans(Tcl_Interp *interp, CipherItem *itemPtr, const char *key, const char *dummy)
{
NitransItem *nitransPtr = (NitransItem *)itemPtr;
int i;
int j;
if (itemPtr->ciphertext == (char *)NULL) {
Tcl_SetResult(interp,
"Can't do anything until ciphertext has been set",
TCL_STATIC);
return TCL_ERROR;
}
if (strlen(key) != itemPtr->period) {
Tcl_SetResult(interp, "Length of key does not match period",
TCL_STATIC);
return TCL_ERROR;
}
for(i=0; i < itemPtr->period; i++) {
if (key[i] < 'a' || key[i] > 'z') {
Tcl_SetResult(interp, "Invalid character in key", TCL_STATIC);
return TCL_ERROR;
}
if (key[i] >= itemPtr->period + 'a') {
Tcl_SetResult(interp, "key character out of range", TCL_STATIC);
return TCL_ERROR;
}
for(j=0; j < itemPtr->period; j++) {
if (key[j] == key[i] && i != j) {
Tcl_SetResult(interp, "duplicate key characters not allowed",
TCL_STATIC);
return TCL_ERROR;
}
}
}
for(i=0; i < itemPtr->period; i++) {
nitransPtr->key[i] = key[i] - 'a';
}
return TCL_OK;
}
static char *
GetNitrans(Tcl_Interp *interp, CipherItem *itemPtr)
{
return NitransTransform(itemPtr, itemPtr->ciphertext, DECODE);
}
static char *
NitransTransform(CipherItem *itemPtr, const char *text, int mode) {
NitransItem *nitransPtr = (NitransItem *)itemPtr;
int i;
char *result=(char *)ckalloc(sizeof(char) * itemPtr->length + 1);
int newCol, oldCol;
int newRow, oldRow;
for(i=0; i < itemPtr->length; i++) {
result[i] = '_';
}
for(i=0; i < itemPtr->length; i++) {
if (nitransPtr->readDir == HORIZONTAL) {
oldCol = i % itemPtr->period;
oldRow = i / itemPtr->period;
} else {
oldCol = i / itemPtr->period;
oldRow = i % itemPtr->period;
}
for(newCol=0; nitransPtr->key[newCol] != oldCol; newCol++);
for(newRow=0; nitransPtr->key[newRow] != oldRow; newRow++);
if (mode == DECODE) {
result[newRow * itemPtr->period + newCol] = text[i];
} else {
result[i] = text[newRow * itemPtr->period + newCol];
}
}
result[itemPtr->length] = '\0';
return result;
}
int
NitransCheckSolutionValue(Tcl_Interp *interp, ClientData clientData, int *key, int keylen)
{
NitransItem *nitransPtr = (NitransItem *)clientData;
CipherItem *itemPtr = (CipherItem *)clientData;
char *pt=(char *)NULL;
int *tKey=(int *)NULL;
int i;
double value;
Tcl_DString dsPtr;
if (keylen != nitransPtr->header.period) {
/*
* This should never be able to happen.
*/
Tcl_SetResult(interp, "Key length != period!", TCL_STATIC);
return TCL_ERROR;
}
tKey = (int *)ckalloc(sizeof(int) * keylen);
for(i=0; i < keylen; i++) {
tKey[i] = nitransPtr->key[i];
}
for(i=0; i < keylen; i++) {
nitransPtr->key[i] = tKey[key[i]];
}
itemPtr->curIteration++;
pt = GetNitrans(interp, (CipherItem *)nitransPtr);
if (DefaultScoreValue(interp, pt, &value) != TCL_OK) {
/* TODO: Test */
return TCL_ERROR;
}
if ((itemPtr->stepInterval
&& itemPtr->curIteration % itemPtr->stepInterval == 0
&& itemPtr->stepCommand && pt)) {
char temp_str[128];
Tcl_DStringInit(&dsPtr);
Tcl_DStringAppendElement(&dsPtr, itemPtr->stepCommand);
sprintf(temp_str, "%ld", itemPtr->curIteration);
Tcl_DStringAppendElement(&dsPtr, temp_str);
Tcl_DStringStartSublist(&dsPtr);
for(i=0; i < keylen; i++) {
sprintf(temp_str, "%d", nitransPtr->key[i]);
Tcl_DStringAppendElement(&dsPtr, temp_str);
}
Tcl_DStringEndSublist(&dsPtr);
Tcl_DStringAppendElement(&dsPtr, pt);
if (Tcl_Eval(interp, Tcl_DStringValue(&dsPtr)) != TCL_OK) {
ckfree(pt);
Tcl_DStringFree(&dsPtr);
return TCL_ERROR;
}
Tcl_DStringFree(&dsPtr);
}
if (value > nitransPtr->maxVal) {
char temp_str[128];
nitransPtr->maxVal = value;
for(i=0; i < keylen; i++) {
nitransPtr->maxKey[i] = nitransPtr->key[i];
}
if (itemPtr->bestFitCommand) {
Tcl_DStringInit(&dsPtr);
Tcl_DStringAppendElement(&dsPtr, itemPtr->bestFitCommand);
sprintf(temp_str, "%ld", itemPtr->curIteration);
Tcl_DStringAppendElement(&dsPtr, temp_str);
Tcl_DStringStartSublist(&dsPtr);
for(i=0; i < keylen; i++) {
sprintf(temp_str, "%d", nitransPtr->maxKey[i]);
Tcl_DStringAppendElement(&dsPtr, temp_str);
}
Tcl_DStringEndSublist(&dsPtr);
sprintf(temp_str, "%g", value);
Tcl_DStringAppendElement(&dsPtr, temp_str);
Tcl_DStringAppendElement(&dsPtr, pt);
if (Tcl_Eval(interp, Tcl_DStringValue(&dsPtr)) != TCL_OK) {
ckfree(pt);
Tcl_DStringFree(&dsPtr);
return TCL_ERROR;
}
Tcl_DStringFree(&dsPtr);
}
}
for(i=0; i < keylen; i++) {
nitransPtr->key[i] = tKey[i];
}
ckfree(pt);
ckfree((char *)tKey);
return TCL_OK;
}
static int
SolveNitrans(Tcl_Interp *interp, CipherItem *itemPtr, char *maxkey)
{
NitransItem *nitransPtr = (NitransItem *)itemPtr;
int i, result;
char *result_key = (char *)NULL;
if (itemPtr->ciphertext == (char *)NULL) {
Tcl_SetResult(interp,
"Can't do anything until ciphertext has been set",
TCL_STATIC);
return TCL_ERROR;
}
itemPtr->curIteration = 0;
nitransPtr->maxVal = 0;
if (nitransPtr->maxKey) {
ckfree((char *)nitransPtr->maxKey);
}
nitransPtr->maxKey = (char *)ckalloc(sizeof(char)*itemPtr->period);
result_key = (char *)ckalloc(sizeof(char)*itemPtr->period + 1);
nitransPtr->readDir = VERTICAL;
result = _internalDoPermCmd((ClientData)itemPtr,
interp, itemPtr->period, NitransCheckSolutionValue);
/*
* Now apply the best key
*/
if (result == TCL_OK) {
for(i=0; i < itemPtr->period; i++) {
nitransPtr->key[i] = nitransPtr->maxKey[i];
result_key[i] = nitransPtr->key[i] + 'a';
}
result_key[i] = '\0';
Tcl_SetResult(interp, result_key, TCL_DYNAMIC);
}
return result;
}
int
NitransCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
{
NitransItem *nitransPtr = (NitransItem *)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", nitransPtr->header.length);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-period", 6) == 0) {
sprintf(temp_str, "%d", itemPtr->period);
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 (!nitransPtr->header.ciphertext) {
Tcl_SetResult(interp, "{}", TCL_STATIC);
} else {
Tcl_SetResult(interp, nitransPtr->header.ciphertext, TCL_VOLATILE);
}
return TCL_OK;
} else if (strncmp(argv[1], "-ptblock", 8) == 0) {
int j;
tPtr = (itemPtr->typePtr->decipherProc)(interp, itemPtr);
for(i=0; i < itemPtr->period; i++) {
for(j=0; j < itemPtr->period; j++) {
temp_str[j] = tPtr[i*itemPtr->period+j];
}
temp_str[itemPtr->period] = '\0';
Tcl_AppendElement(interp, temp_str);
}
ckfree(tPtr);
return TCL_OK;
} else if (strncmp(argv[1], "-plaintext", 9) == 0 ||
strncmp(argv[1], "-ptext", 3) == 0) {
tPtr = (itemPtr->typePtr->decipherProc)(interp, itemPtr);
Tcl_SetResult(interp, tPtr, TCL_DYNAMIC);
return TCL_OK;
} else if (strncmp(argv[1], "-key", 4) == 0) {
for(i=0; i < itemPtr->period; i++) {
temp_str[i] = nitransPtr->key[i] + 'a';
}
temp_str[i] = '\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], "-width", 6) == 0) {
sprintf(temp_str, "%d", itemPtr->period);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-height", 7) == 0) {
sprintf(temp_str, "%d", itemPtr->period);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_OK;
} else if (strncmp(argv[1], "-read", 5) == 0) {
if (nitransPtr->readDir == VERTICAL) {
Tcl_SetResult(interp, "vertical", TCL_STATIC);
} else if (nitransPtr->readDir == HORIZONTAL) {
Tcl_SetResult(interp, "horizontal", TCL_STATIC);
} else {
Tcl_SetResult(interp, "Unknown nitrans read direction!", TCL_STATIC);
return TCL_ERROR;
}
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, "-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, "-period", 14) == 0) {
/*
* Attempting to set the period of a nitrans cipher is a noop.
* This could throw an error, but why bother?
*/
return TCL_OK;
} else if (strncmp(*argv, "-bestfitcommand", 14) == 0) {
CipherSetBestFitCmd(itemPtr, argv[1]);
} else if (strncmp(*argv, "-stepcommand", 14) == 0) {
CipherSetStepCmd(itemPtr, argv[1]);
} else 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, "-read", 5) == 0) {
if (strncmp(argv[1], "vertical", 4) == 0) {
nitransPtr->readDir = VERTICAL;
} else if (strncmp(argv[1], "horizontal", 5) == 0) {
nitransPtr->readDir = HORIZONTAL;
} else {
Tcl_AppendResult(interp, "Invalid direction. Must be one of vertical or horizontal", (char *)NULL);
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, "shift", 2) == 0)) {
char col;
int amount;
if (argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd, " shift col amount",
(char *)NULL);
return TCL_ERROR;
}
col = *argv[1];
if (sscanf(argv[2], "%d", &amount) != 1) {
Tcl_AppendResult(interp, "Invalid shift value ", argv[2],
(char *)NULL);
return TCL_ERROR;
}
col -= 'a';
if (NitransShiftColumn(interp, itemPtr, col, amount) != TCL_OK) {
return TCL_ERROR;
}
} else if (**argv == 's' && (strncmp(*argv, "swap", 2) == 0)) {
char col1, col2;
if (argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd, " swap col1 col2",
(char *)NULL);
return TCL_ERROR;
}
col1 = *argv[1];
col2 = *argv[2];
col1 -= 'a';
col2 -= 'a';
if (NitransSwapVals(interp, itemPtr, col1, col2) != TCL_OK) {
return TCL_ERROR;
}
} else if (**argv == 's' && (strncmp(*argv, "solve", 5) == 0)) {
if (argc != 1) {
Tcl_AppendResult(interp, "Usage: ", cmd, " solve",
(char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->solveProc)(interp, itemPtr, temp_str);
} else if (**argv == 'u' && (strncmp(*argv, "undo", 5) == 0)) {
if (argc != 1) {
Tcl_AppendResult(interp, "Usage: ", cmd, " undo",
(char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->undoProc)(interp, itemPtr, (char *)NULL,
0);
} else if (**argv == 'r' && (strncmp(*argv, "restore", 2) == 0)) {
if (argc != 2 && argc != 3) {
Tcl_AppendResult(interp, "Usage: ", cmd, " restore key",
(char *)NULL);
return TCL_ERROR;
}
return (itemPtr->typePtr->restoreProc)(interp, itemPtr, argv[1],
(char *)NULL);
} 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,
" shift col amount", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" swap col1 col2", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" undo", (char *)NULL);
Tcl_AppendResult(interp, "\n ", cmd,
" encode pt key", (char *)NULL);
return TCL_ERROR;
}
return TCL_OK;
}
static int
EncodeNitrans(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 ((itemPtr->typePtr->setctProc)(interp, itemPtr, pt) != TCL_OK) {
ckfree((char *)argv);
return TCL_ERROR;
}
/*
* The period of a nitrans cipher is set at the same time that the
* ciphertext is set. We have to postpone checking the length of the
* key until after this.
*/
if (strlen(argv[0]) != itemPtr->period) {
Tcl_SetResult(interp,
"Length of key does not match the period.", TCL_STATIC);
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 = NitransTransform(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, ct, TCL_DYNAMIC);
ckfree((char *)argv);
return TCL_OK;
}
#undef VERTICAL
#undef HORIZONTAL