annotate Modules/rotormodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 18fd07869b13
children 38023a425e48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
2 Copyright 1994 by Lance Ellinghouse,
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
3 Cathedral City, California Republic, United States of America.
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 documentation for any purpose and without fee is hereby granted,
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 both that copyright notice and this permission notice appear in
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
11 supporting documentation, and that the name of Lance Ellinghouse
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
12 not be used in advertising or publicity pertaining to distribution
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
13 of the software without specific, written prior permission.
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
15 LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
17 FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
18 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
19 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
20 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
21 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
22
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
23 ******************************************************************/
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* This creates an encryption and decryption engine I am calling
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 a rotor due to the original design was a harware rotor with
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 contacts used in Germany during WWII.
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 Rotor Module:
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 - rotor.newrotor('key') -> rotorobject (default of 6 rotors)
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 - rotor.newrotor('key', num_rotors) -> rotorobject
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 Rotor Objects:
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
36 - ro.setkey('string') -> None (resets the key as defined in newrotor().
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 - ro.encrypt('string') -> encrypted string
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 - ro.decrypt('encrypted string') -> unencrypted string
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
40 - ro.encryptmore('string') -> encrypted string
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
41 - ro.decryptmore('encrypted string') -> unencrypted string
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
42
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
43 NOTE: the {en,de}cryptmore() methods use the setup that was
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
44 established via the {en,de}crypt calls. They will NOT
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
45 re-initalize the rotors unless: 1) They have not been
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
46 initalized with {en,de}crypt since the last setkey() call;
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
47 2) {en,de}crypt has not been called for this rotor yet.
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
48
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 NOTE: you MUST use the SAME key in rotor.newrotor()
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 if you wish to decrypt an encrypted string.
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 Also, the encrypted string is NOT 0-127 ASCII.
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 It is considered BINARY data.
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 */
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 /* Rotor objects */
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
1888
be956b1efb97 * various modules: #include "Python.h" and remove most remporary
Guido van Rossum <guido@python.org>
parents: 1855
diff changeset
58 #include "Python.h"
2506
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2123
diff changeset
59 #include "mymath.h"
2123
fd5ab7e828fc cosmetic changes so these modules will work with the strict new naming scheme
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
60
5553
18fd07869b13 workaround for Mac MSL header definitions of TRUE and FALSE (Jack)
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
61 #ifndef TRUE
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
62 #define TRUE 1
5553
18fd07869b13 workaround for Mac MSL header definitions of TRUE and FALSE (Jack)
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
63 #endif
18fd07869b13 workaround for Mac MSL header definitions of TRUE and FALSE (Jack)
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
64 #ifndef FALSE
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
65 #define FALSE 0
5553
18fd07869b13 workaround for Mac MSL header definitions of TRUE and FALSE (Jack)
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
66 #endif
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 typedef struct {
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
69 PyObject_HEAD
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 int seed[3];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 short key[5];
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
72 int isinited;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 int size;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 int size_mask;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 int rotors;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
76 unsigned char *e_rotor; /* [num_rotors][size] */
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
77 unsigned char *d_rotor; /* [num_rotors][size] */
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
78 unsigned char *positions; /* [num_rotors] */
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
79 unsigned char *advances; /* [num_rotors] */
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
80 } Rotorobj;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
82 staticforward PyTypeObject Rotor_Type;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
84 #define is_rotor(v) ((v)->ob_type == &Rotor_Type)
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
86
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
87 /* This defines the necessary routines to manage rotor objects */
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
89 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
90 set_seed(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
91 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 r->seed[0] = r->key[0];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 r->seed[1] = r->key[1];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 r->seed[2] = r->key[2];
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
96 r->isinited = FALSE;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 /* Return the next random number in the range [0.0 .. 1.0) */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
100 static double
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
101 r_random(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
102 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 int x, y, z;
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
105 double val, term;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 x = r->seed[0];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 y = r->seed[1];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 z = r->seed[2];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 x = 171 * (x % 177) - 2 * (x/177);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 y = 172 * (y % 176) - 35 * (y/176);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 z = 170 * (z % 178) - 63 * (z/178);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 if (x < 0) x = x + 30269;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 if (y < 0) y = y + 30307;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 if (z < 0) z = z + 30323;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 r->seed[0] = x;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 r->seed[1] = y;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 r->seed[2] = z;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
123 term = (double)(
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
124 (((double)x)/(double)30269.0) +
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
125 (((double)y)/(double)30307.0) +
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
126 (((double)z)/(double)30323.0)
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
127 );
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
128 val = term - (double)floor((double)term);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
130 if (val >= 1.0)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
131 val = 0.0;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 return val;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
136 static short
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
137 r_rand(r, s)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
138 Rotorobj *r;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
139 short s;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
141 return (short)((short)(r_random(r) * (double)s) % s);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
144 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
145 set_key(r, key)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
146 Rotorobj *r;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
147 char *key;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 {
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1386
diff changeset
149 unsigned long k1=995, k2=576, k3=767, k4=671, k5=463;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 int i;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
151 int len = strlen(key);
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
152
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
153 for (i = 0; i < len; i++) {
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
154 unsigned short ki = Py_CHARMASK(key[i]);
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
155
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
156 k1 = (((k1<<3 | k1>>13) + ki) & 65535);
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
157 k2 = (((k2<<3 | k2>>13) ^ ki) & 65535);
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
158 k3 = (((k3<<3 | k3>>13) - ki) & 65535);
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
159 k4 = ((ki - (k4<<3 | k4>>13)) & 65535);
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
160 k5 = (((k5<<3 | k5>>13) ^ ~ki) & 65535);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 r->key[0] = (short)k1;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 r->key[1] = (short)(k2|1);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 r->key[2] = (short)k3;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 r->key[3] = (short)k4;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 r->key[4] = (short)k5;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 set_seed(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
171
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
172
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 /* These define the interface to a rotor object */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
174 static Rotorobj *
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
175 rotorobj_new(num_rotors, key)
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 int num_rotors;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 char *key;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
179 Rotorobj *xp;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
180
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
181 xp = PyObject_NEW(Rotorobj, &Rotor_Type);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if (xp == NULL)
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 return NULL;
918
e43a220c287a Changes to make it compile under SGI -- revamped new/dealloc a bit.
Guido van Rossum <guido@python.org>
parents: 917
diff changeset
184 set_key(xp, key);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 xp->size = 256;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 xp->size_mask = xp->size - 1;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 xp->size_mask = 0;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 xp->rotors = num_rotors;
918
e43a220c287a Changes to make it compile under SGI -- revamped new/dealloc a bit.
Guido van Rossum <guido@python.org>
parents: 917
diff changeset
190 xp->e_rotor = NULL;
e43a220c287a Changes to make it compile under SGI -- revamped new/dealloc a bit.
Guido van Rossum <guido@python.org>
parents: 917
diff changeset
191 xp->d_rotor = NULL;
e43a220c287a Changes to make it compile under SGI -- revamped new/dealloc a bit.
Guido van Rossum <guido@python.org>
parents: 917
diff changeset
192 xp->positions = NULL;
e43a220c287a Changes to make it compile under SGI -- revamped new/dealloc a bit.
Guido van Rossum <guido@python.org>
parents: 917
diff changeset
193 xp->advances = NULL;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
195 if (!(xp->e_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size)))
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
196 goto finally;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
197 if (!(xp->d_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size)))
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
198 goto finally;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
199 if (!(xp->positions = PyMem_NEW(unsigned char, num_rotors)))
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
200 goto finally;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
201 if (!(xp->advances = PyMem_NEW(unsigned char, num_rotors)))
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
202 goto finally;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
203
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 return xp;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
205
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
206 finally:
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
207 PyMem_XDEL(xp->e_rotor);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
208 PyMem_XDEL(xp->d_rotor);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
209 PyMem_XDEL(xp->positions);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
210 PyMem_XDEL(xp->advances);
2123
fd5ab7e828fc cosmetic changes so these modules will work with the strict new naming scheme
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
211 Py_DECREF(xp);
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
212 return (Rotorobj*)PyErr_NoMemory();
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
215
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 /* These routines impliment the rotor itself */
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
218 /* Here is a fairly sophisticated {en,de}cryption system. It is based on
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
219 the idea of a "rotor" machine. A bunch of rotors, each with a
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
220 different permutation of the alphabet, rotate around a different amount
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
221 after encrypting one character. The current state of the rotors is
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
222 used to encrypt one character.
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
224 The code is smart enought to tell if your alphabet has a number of
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
225 characters equal to a power of two. If it does, it uses logical
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
226 operations, if not it uses div and mod (both require a division).
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
228 You will need to make two changes to the code 1) convert to c, and
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
229 customize for an alphabet of 255 chars 2) add a filter at the begining,
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
230 and end, which subtracts one on the way in, and adds one on the way
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
231 out.
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
233 You might wish to do some timing studies. Another viable alternative
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
234 is to "byte stuff" the encrypted data of a normal (perhaps this one)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
235 encryption routine.
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
237 j'
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
238
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
239 */
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
241 /* Note: the C code here is a fairly straightforward transliteration of a
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
242 * rotor implemented in lisp. The original lisp code has been removed from
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
243 * this file to for simplification, but I've kept the docstrings as
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
244 * comments in front of the functions.
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
245 */
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
246
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
247
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
248 /* Set ROTOR to the identity permutation */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
249 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
250 RTR_make_id_rotor(r, rtr)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
251 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 unsigned char *rtr;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 register int j;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 register int size = r->size;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
256 for (j = 0; j < size; j++) {
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 rtr[j] = (unsigned char)j;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
262 /* The current set of encryption rotors */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
263 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
264 RTR_e_rotors(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
265 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 int i;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
268 for (i = 0; i < r->rotors; i++) {
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
269 RTR_make_id_rotor(r, &(r->e_rotor[(i*r->size)]));
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
273 /* The current set of decryption rotors */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
274 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
275 RTR_d_rotors(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
276 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 register int i, j;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
279 for (i = 0; i < r->rotors; i++) {
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
280 for (j = 0; j < r->size; j++) {
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 r->d_rotor[((i*r->size)+j)] = (unsigned char)j;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
286 /* The positions of the rotors at this time */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
287 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
288 RTR_positions(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
289 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 int i;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
292 for (i = 0; i < r->rotors; i++) {
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 r->positions[i] = 1;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
297 /* The number of positions to advance the rotors at a time */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
298 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
299 RTR_advances(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
300 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 int i;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
303 for (i = 0; i < r->rotors; i++) {
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 r->advances[i] = 1;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
308 /* Permute the E rotor, and make the D rotor its inverse
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
309 * see Knuth for explanation of algorithm.
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
310 */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
311 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
312 RTR_permute_rotor(r, e, d)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
313 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 unsigned char *e;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 unsigned char *d;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 short i = r->size;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 short q;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 unsigned char j;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 RTR_make_id_rotor(r,e);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 while (2 <= i) {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 q = r_rand(r,i);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 i--;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 j = e[q];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 e[q] = (unsigned char)e[i];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 e[i] = (unsigned char)j;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 d[j] = (unsigned char)i;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 e[0] = (unsigned char)e[0];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 d[(e[0])] = (unsigned char)0;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
333 /* Given KEY (a list of 5 16 bit numbers), initialize the rotor machine.
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
334 * Set the advancement, position, and permutation of the rotors
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
335 */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
336 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
337 RTR_init(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
338 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 int i;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 set_seed(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 RTR_positions(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 RTR_advances(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 RTR_e_rotors(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 RTR_d_rotors(r);
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
346 for (i = 0; i < r->rotors; i++) {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4971
diff changeset
347 r->positions[i] = (unsigned char) r_rand(r,r->size);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 r->advances[i] = (1+(2*(r_rand(r,r->size/2))));
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
349 RTR_permute_rotor(r,
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
350 &(r->e_rotor[(i*r->size)]),
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
351 &(r->d_rotor[(i*r->size)]));
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 }
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
353 r->isinited = TRUE;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
356 /* Change the RTR-positions vector, using the RTR-advances vector */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
357 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
358 RTR_advance(r)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
359 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 register int i=0, temp=0;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 if (r->size_mask) {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
363 while (i < r->rotors) {
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 temp = r->positions[i] + r->advances[i];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 r->positions[i] = temp & r->size_mask;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 if ((temp >= r->size) && (i < (r->rotors - 1))) {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 r->positions[(i+1)] = 1 + r->positions[(i+1)];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 i++;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 } else {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
372 while (i < r->rotors) {
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 temp = r->positions[i] + r->advances[i];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 r->positions[i] = temp%r->size;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 if ((temp >= r->size) && (i < (r->rotors - 1))) {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 r->positions[(i+1)] = 1 + r->positions[(i+1)];
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 i++;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
383 /* Encrypt the character P with the current rotor machine */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
384 static unsigned char
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
385 RTR_e_char(r, p)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
386 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 unsigned char p;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 register int i=0;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 register unsigned char tp=p;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 if (r->size_mask) {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 while (i < r->rotors) {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
393 tp = r->e_rotor[(i*r->size) +
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
394 (((r->positions[i] ^ tp) &
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
395 r->size_mask))];
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 i++;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 } else {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 while (i < r->rotors) {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
400 tp = r->e_rotor[(i*r->size) +
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
401 (((r->positions[i] ^ tp) %
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
402 (unsigned int) r->size))];
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 i++;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 RTR_advance(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 return ((unsigned char)tp);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
410 /* Decrypt the character C with the current rotor machine */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
411 static unsigned char
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
412 RTR_d_char(r, c)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
413 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 unsigned char c;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
416 register int i = r->rotors - 1;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
417 register unsigned char tc = c;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
418
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 if (r->size_mask) {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 while (0 <= i) {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
421 tc = (r->positions[i] ^
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
422 r->d_rotor[(i*r->size)+tc]) & r->size_mask;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 i--;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 } else {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 while (0 <= i) {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
427 tc = (r->positions[i] ^
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
428 r->d_rotor[(i*r->size)+tc]) %
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
429 (unsigned int) r->size;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 i--;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 RTR_advance(r);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 return(tc);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
436
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
437 /* Perform a rotor encryption of the region from BEG to END by KEY */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
438 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
439 RTR_e_region(r, beg, len, doinit)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
440 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 unsigned char *beg;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 int len;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
443 int doinit;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 register int i;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
446 if (doinit || r->isinited == FALSE)
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
447 RTR_init(r);
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
448 for (i = 0; i < len; i++) {
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
449 beg[i] = RTR_e_char(r, beg[i]);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
453 /* Perform a rotor decryption of the region from BEG to END by KEY */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
454 static void
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
455 RTR_d_region(r, beg, len, doinit)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
456 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 unsigned char *beg;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 int len;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
459 int doinit;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 {
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 register int i;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
462 if (doinit || r->isinited == FALSE)
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
463 RTR_init(r);
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
464 for (i = 0; i < len; i++) {
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
465 beg[i] = RTR_d_char(r, beg[i]);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
468
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
469
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
470
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 /* Rotor methods */
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 static void
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
473 rotor_dealloc(xp)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
474 Rotorobj *xp;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 {
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
476 PyMem_XDEL(xp->e_rotor);
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
477 PyMem_XDEL(xp->d_rotor);
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
478 PyMem_XDEL(xp->positions);
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
479 PyMem_XDEL(xp->advances);
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
480 PyMem_DEL(xp);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
483 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
484 rotorobj_encrypt(self, args)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
485 Rotorobj *self;
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
486 PyObject * args;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
488 char *string = NULL;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 int len = 0;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
490 PyObject *rtn = NULL;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 char *tmp;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
493 if (!PyArg_Parse(args, "s#", &string, &len))
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 return NULL;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
495 if (!(tmp = PyMem_NEW(char, len+5))) {
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
496 PyErr_NoMemory();
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 return NULL;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 }
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
499 memset(tmp, '\0', len+1);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
500 memcpy(tmp, string, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
501 RTR_e_region(self, (unsigned char *)tmp, len, TRUE);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
502 rtn = PyString_FromStringAndSize(tmp, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
503 PyMem_DEL(tmp);
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
504 return(rtn);
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
505 }
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
506
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
507 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
508 rotorobj_encrypt_more(self, args)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
509 Rotorobj *self;
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
510 PyObject * args;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
511 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
512 char *string = NULL;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
513 int len = 0;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
514 PyObject *rtn = NULL;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
515 char *tmp;
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
516
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
517 if (!PyArg_Parse(args, "s#", &string, &len))
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
518 return NULL;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
519 if (!(tmp = PyMem_NEW(char, len+5))) {
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
520 PyErr_NoMemory();
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
521 return NULL;
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
522 }
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
523 memset(tmp, '\0', len+1);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
524 memcpy(tmp, string, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
525 RTR_e_region(self, (unsigned char *)tmp, len, FALSE);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
526 rtn = PyString_FromStringAndSize(tmp, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
527 PyMem_DEL(tmp);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 return(rtn);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
530
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
531 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
532 rotorobj_decrypt(self, args)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
533 Rotorobj *self;
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
534 PyObject * args;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
536 char *string = NULL;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 int len = 0;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
538 PyObject *rtn = NULL;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 char *tmp;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
540
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
541 if (!PyArg_Parse(args, "s#", &string, &len))
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 return NULL;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
543 if (!(tmp = PyMem_NEW(char, len+5))) {
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
544 PyErr_NoMemory();
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 return NULL;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 }
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
547 memset(tmp, '\0', len+1);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
548 memcpy(tmp, string, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
549 RTR_d_region(self, (unsigned char *)tmp, len, TRUE);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
550 rtn = PyString_FromStringAndSize(tmp, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
551 PyMem_DEL(tmp);
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
552 return(rtn);
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
553 }
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
554
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
555 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
556 rotorobj_decrypt_more(self, args)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
557 Rotorobj *self;
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
558 PyObject * args;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
559 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
560 char *string = NULL;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
561 int len = 0;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
562 PyObject *rtn = NULL;
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
563 char *tmp;
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
564
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
565 if (!PyArg_Parse(args, "s#", &string, &len))
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
566 return NULL;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
567 if (!(tmp = PyMem_NEW(char, len+5))) {
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
568 PyErr_NoMemory();
919
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
569 return NULL;
d3b27fff00b2 New rotor methods: setkey(), encryptmore() and decryptmore().
Guido van Rossum <guido@python.org>
parents: 918
diff changeset
570 }
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
571 memset(tmp, '\0', len+1);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
572 memcpy(tmp, string, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
573 RTR_d_region(self, (unsigned char *)tmp, len, FALSE);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
574 rtn = PyString_FromStringAndSize(tmp, len);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
575 PyMem_DEL(tmp);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 return(rtn);
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
578
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
579 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
580 rotorobj_setkey(self, args)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
581 Rotorobj *self;
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
582 PyObject * args;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 {
4857
e0aaee7fd1fd rotorobj_setkey(): A single string argument is now required (i.e. no
Barry Warsaw <barry@python.org>
parents: 4824
diff changeset
584 char *key;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
585
4857
e0aaee7fd1fd rotorobj_setkey(): A single string argument is now required (i.e. no
Barry Warsaw <barry@python.org>
parents: 4824
diff changeset
586 if (!PyArg_ParseTuple(args, "s", &key))
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
587 return NULL;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
588
4857
e0aaee7fd1fd rotorobj_setkey(): A single string argument is now required (i.e. no
Barry Warsaw <barry@python.org>
parents: 4824
diff changeset
589 set_key(self, key);
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
590 Py_INCREF(Py_None);
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
591 return Py_None;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
593
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
594 static struct PyMethodDef
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
595 rotorobj_methods[] = {
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
596 {"encrypt", (PyCFunction)rotorobj_encrypt},
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
597 {"encryptmore", (PyCFunction)rotorobj_encrypt_more},
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
598 {"decrypt", (PyCFunction)rotorobj_decrypt},
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
599 {"decryptmore", (PyCFunction)rotorobj_decrypt_more},
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
600 {"setkey", (PyCFunction)rotorobj_setkey, 1},
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 {NULL, NULL} /* sentinel */
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 };
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
603
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
604
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 /* Return a rotor object's named attribute. */
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
606 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
607 rotorobj_getattr(s, name)
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
608 Rotorobj *s;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 char *name;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
611 return Py_FindMethod(rotorobj_methods, (PyObject*)s, name);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
613
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
614
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
615 statichere PyTypeObject Rotor_Type = {
2123
fd5ab7e828fc cosmetic changes so these modules will work with the strict new naming scheme
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
616 PyObject_HEAD_INIT(&PyType_Type)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
617 0, /*ob_size*/
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 "rotor", /*tp_name*/
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
619 sizeof(Rotorobj), /*tp_size*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
620 0, /*tp_itemsize*/
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 /* methods */
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
622 (destructor)rotor_dealloc, /*tp_dealloc*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
623 0, /*tp_print*/
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
624 (getattrfunc)rotorobj_getattr, /*tp_getattr*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
625 0, /*tp_setattr*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
626 0, /*tp_compare*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1393
diff changeset
627 0, /*tp_repr*/
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
628 0, /*tp_hash*/
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 };
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
630
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
631
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
632 static PyObject *
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
633 rotor_rotor(self, args)
1855
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
634 PyObject * self;
9dbdb83d4178 Lance has converted his modules to the new naming conventions, and
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
635 PyObject * args;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
637 Rotorobj *r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 char *string;
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 int len;
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
640 int num_rotors = 6;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
641
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
642 if (!PyArg_ParseTuple(args, "s#|i", &string, &len, &num_rotors))
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
643 return NULL;
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
644
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
645 r = rotorobj_new(num_rotors, string);
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
646 return (PyObject *)r;
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 }
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
648
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
649
4971
8d8c76a1d250 set_key(): Use Py_CHARMASK macro to get the character from the array
Barry Warsaw <barry@python.org>
parents: 4857
diff changeset
650
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
651 static struct PyMethodDef
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
652 rotor_methods[] = {
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
653 {"newrotor", rotor_rotor, 1},
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
654 {NULL, NULL} /* sentinel */
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 };
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
656
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
657
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 void
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 initrotor()
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 {
4824
8783cfdc7c6f Reworked to check for memory problems (one potential found),
Barry Warsaw <barry@python.org>
parents: 2506
diff changeset
661 (void)Py_InitModule("rotor", rotor_methods);
917
cfdd575efff2 New file rotormodule.c (by Lance Ellinghouse).
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 }