-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.c
More file actions
315 lines (278 loc) · 8.33 KB
/
Copy pathcipher.c
File metadata and controls
315 lines (278 loc) · 8.33 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
/*
* cipher.c --
*
* This file initializes the Tcl "cipher" package.
*
* Copyright (c) 1995-2000 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 <limits.h>
#include <stdint.h>
#include "cipher.h"
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
#include <cipherDebug.h>
static CipherType *typeList = (CipherType *)NULL;
extern CipherType AmscoType;
extern CipherType AristocratType;
extern CipherType BaconianType;
extern CipherType BazeriesType;
extern CipherType BeaufortType;
extern CipherType BifidType;
extern CipherType BigbifidType;
extern CipherType BigplayfairType;
extern CipherType CadenusType;
extern CipherType CaesarType;
extern CipherType ColumnarType;
extern CipherType DigrafidType;
extern CipherType FmorseType;
extern CipherType FoursquareType;
extern CipherType GrandpreType;
extern CipherType GrilleType;
extern CipherType GromarkType;
extern CipherType GronsfeldType;
extern CipherType HomophonicType;
extern CipherType MorbitType;
extern CipherType MyszcowskiType;
extern CipherType NicodemusType;
extern CipherType NitransType;
extern CipherType PhillipsType;
extern CipherType PlayfairType;
extern CipherType PolluxType;
extern CipherType PortaType;
extern CipherType Quagmire1Type;
extern CipherType Quagmire2Type;
extern CipherType Quagmire3Type;
extern CipherType Quagmire4Type;
extern CipherType RagbabyType;
extern CipherType RailfenceType;
extern CipherType RouteType;
extern CipherType SwagmanType;
extern CipherType TrifidType;
extern CipherType TwosquareType;
extern CipherType VariantType;
extern CipherType VigenereType;
int cipherid = 0;
void
InitCiphertypes(void)
{
if (typeList == NULL) {
typeList = &AmscoType;
AmscoType.nextPtr = &AristocratType;
AristocratType.nextPtr = &BaconianType;
BaconianType.nextPtr = &BeaufortType;
BeaufortType.nextPtr = &BazeriesType;
BazeriesType.nextPtr = &BifidType;
BifidType.nextPtr = &BigbifidType;
BigbifidType.nextPtr = &BigplayfairType;
BigplayfairType.nextPtr = &CadenusType;
CadenusType.nextPtr = &CaesarType;
CaesarType.nextPtr = &ColumnarType;
ColumnarType.nextPtr = &DigrafidType;
DigrafidType.nextPtr = &FmorseType;
FmorseType.nextPtr = &FoursquareType;
FoursquareType.nextPtr = &GrandpreType;
GrandpreType.nextPtr = &GrilleType;
GrilleType.nextPtr = &GromarkType;
GromarkType.nextPtr = &GronsfeldType;
GronsfeldType.nextPtr = &HomophonicType;
HomophonicType.nextPtr = &MorbitType;
MorbitType.nextPtr = &MyszcowskiType;
MyszcowskiType.nextPtr = &NicodemusType;
NicodemusType.nextPtr = &NitransType;
NitransType.nextPtr = &PhillipsType;
PhillipsType.nextPtr = &PlayfairType;
PlayfairType.nextPtr = &PolluxType;
PolluxType.nextPtr = &PortaType;
PortaType.nextPtr = &Quagmire1Type;
Quagmire1Type.nextPtr = &Quagmire2Type;
Quagmire2Type.nextPtr = &Quagmire3Type;
Quagmire3Type.nextPtr = &Quagmire4Type;
Quagmire4Type.nextPtr = &RagbabyType;
RagbabyType.nextPtr = &RailfenceType;
RailfenceType.nextPtr = &RouteType;
RouteType.nextPtr = &SwagmanType;
SwagmanType.nextPtr = &TrifidType;
TrifidType.nextPtr = &TwosquareType;
TwosquareType.nextPtr = &VariantType;
VariantType.nextPtr = &VigenereType;
VigenereType.nextPtr = NULL;
}
cipherid=0;
}
void
CreateCipherType(CipherType *typePtr)
{
CipherType *typePtr2, *prevPtr;
for(typePtr2 = typeList, prevPtr=(CipherType *)NULL;
typePtr2 != (CipherType *)NULL;
prevPtr = typePtr2, typePtr2 = typePtr2->nextPtr) {
if (strcmp(typePtr2->type, typePtr->type) == 0) {
if (prevPtr == (CipherType *)NULL) {
typeList = typePtr2->nextPtr;
} else {
prevPtr->nextPtr = typePtr2->nextPtr;
}
break;
}
}
typePtr->nextPtr = typeList;
typeList = typePtr;
}
int
CipherSetBestFitCmd(CipherItem *itemPtr, const char *cmd)
{
if (itemPtr->bestFitCommand) {
ckfree(itemPtr->bestFitCommand);
}
itemPtr->bestFitCommand = (char *)NULL;
if (strlen(cmd) > 0) {
itemPtr->bestFitCommand = (char *)ckalloc(sizeof(char) * strlen(cmd)+1);
strcpy(itemPtr->bestFitCommand, cmd);
}
return TCL_OK;
}
int
CipherSetStepCmd(CipherItem *itemPtr, const char *cmd)
{
if (itemPtr->stepCommand) {
ckfree(itemPtr->stepCommand);
}
itemPtr->stepCommand = (char *)NULL;
if (strlen(cmd) != 0) {
itemPtr->stepCommand = (char *)ckalloc(sizeof(char) * strlen(cmd)+1);
strcpy(itemPtr->stepCommand, cmd);
}
return TCL_OK;
}
void
DeleteCipher(ClientData clientData)
{
CipherItem *itemPtr = (CipherItem *)clientData;
if (itemPtr->ciphertext) {
ckfree((char *)(itemPtr->ciphertext));
}
if (itemPtr->stepCommand) {
ckfree((char *)(itemPtr->stepCommand));
}
if (itemPtr->bestFitCommand) {
ckfree((char *)(itemPtr->bestFitCommand));
}
ckfree((char *) clientData);
}
/*
* SafeCkalloc --
*
* Safely allocate memory with overflow checking
* Calculates size * multiplier + additional and checks for integer overflow
*
* Results:
* Pointer to allocated memory, or NULL if overflow detected or allocation failed
*/
char *
SafeCkalloc(size_t size, size_t multiplier, size_t additional)
{
size_t total;
/* Check for multiplication overflow */
if (multiplier > 0 && size > SIZE_MAX / multiplier) {
return NULL; /* Would overflow */
}
total = size * multiplier;
/* Check for addition overflow */
if (additional > SIZE_MAX - total) {
return NULL; /* Would overflow */
}
total += additional;
/* Additional safety check for very large allocations */
if (total > (SIZE_MAX / 2)) {
return NULL; /* Suspiciously large allocation */
}
return (char *)ckalloc(total);
}
int
CipherCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
{
CipherType *typePtr,
*matchPtr = NULL;
CipherItem *itemPtr;
char temp_str[128];
if (argc < 2) {
Tcl_SetResult(interp, "Usage: cipher ?option? ?args?", TCL_STATIC);
return TCL_ERROR;
}
if (*argv[1] == 'c' && (strncmp(argv[1], "create", 1) == 0)) {
if (argc < 3) {
/*
* Error: wrong # args
*/
Tcl_SetResult(interp, "Usage: cipher create type ?args?",
TCL_STATIC);
return TCL_ERROR;
}
for(typePtr = typeList; typePtr != NULL; typePtr = typePtr->nextPtr) {
if (strcmp(argv[2], typePtr->type) == 0) {
matchPtr = typePtr;
}
}
if (matchPtr == NULL) {
sprintf(temp_str, "unknown cipher type %s", argv[2]);
Tcl_SetResult(interp, temp_str, TCL_VOLATILE);
return TCL_ERROR;
}
typePtr = matchPtr;
itemPtr = (CipherItem *)ckalloc((unsigned)typePtr->size);
itemPtr->typePtr = typePtr;
cipherid++;
itemPtr->ciphertext = (char *)NULL;
itemPtr->period = 0;
itemPtr->length = 0;
itemPtr->language = 1;
itemPtr->id = 0;
itemPtr->stepCommand = (char *)NULL;
itemPtr->bestFitCommand = (char *)NULL;
itemPtr->stepInterval = 0;
itemPtr->curIteration = 0;
if ((*typePtr->createProc)(interp, itemPtr, argc-3, argv+3) != TCL_OK) {
/*
* If the create procedure failed then we should assume that it
* has also performed any necessary cleanup by calling the
* DeleteCommand procedure for the new object. No need to free
* up the memory for the itemPtr since it should have already
* been cleaned up.
*/
return TCL_ERROR;
}
return TCL_OK;
} else if (*argv[1] == 't' && (strncmp(argv[1], "types", 1) == 0)) {
if (argc > 2) {
Tcl_SetResult(interp,
"Wrong number of args. Should be: cipher types",
TCL_STATIC);
return TCL_ERROR;
}
for(typePtr = typeList; typePtr; typePtr = typePtr->nextPtr)
Tcl_AppendElement(interp, typePtr->type);
return TCL_OK;
} else {
Tcl_SetResult(interp, "Usage: cipher ?option? ?args?", TCL_STATIC);
return TCL_ERROR;
}
return TCL_ERROR;
}