Remove mpz, rotor, xreadlines modules
This commit is contained in:
parent
810b76aebe
commit
57269d0c7c
|
@ -180,7 +180,6 @@ class PyBuildExt(build_ext):
|
|||
|
||||
exts.append( Extension('_weakref', ['_weakref.c']) )
|
||||
exts.append( Extension('_symtable', ['symtablemodule.c']) )
|
||||
exts.append( Extension('xreadlines', ['xreadlinesmodule.c']) )
|
||||
|
||||
# array objects
|
||||
exts.append( Extension('array', ['arraymodule.c']) )
|
||||
|
@ -244,10 +243,7 @@ class PyBuildExt(build_ext):
|
|||
# Memory-mapped files (also works on Win32).
|
||||
exts.append( Extension('mmap', ['mmapmodule.c']) )
|
||||
|
||||
# Lance Ellinghaus's modules:
|
||||
# enigma-inspired encryption
|
||||
exts.append( Extension('rotor', ['rotormodule.c']) )
|
||||
# syslog daemon interface
|
||||
# Lance Ellinghaus's syslog daemon interface
|
||||
exts.append( Extension('syslog', ['syslogmodule.c']) )
|
||||
|
||||
# George Neville-Neil's timing module:
|
||||
|
@ -361,23 +357,6 @@ class PyBuildExt(build_ext):
|
|||
include_dirs = db_inc,
|
||||
libraries = dblib) )
|
||||
|
||||
# The mpz module interfaces to the GNU Multiple Precision library.
|
||||
# You need to ftp the GNU MP library.
|
||||
# This was originally written and tested against GMP 1.2 and 1.3.2.
|
||||
# It has been modified by Rob Hooft to work with 2.0.2 as well, but I
|
||||
# haven't tested it recently. For a more complete module,
|
||||
# refer to pympz.sourceforge.net.
|
||||
|
||||
# A compatible MP library unencombered by the GPL also exists. It was
|
||||
# posted to comp.sources.misc in volume 40 and is widely available from
|
||||
# FTP archive sites. One URL for it is:
|
||||
# ftp://gatekeeper.dec.com/.b/usenet/comp.sources.misc/volume40/fgmp/part01.Z
|
||||
|
||||
if (self.compiler.find_library_file(lib_dirs, 'gmp')):
|
||||
exts.append( Extension('mpz', ['mpzmodule.c'],
|
||||
libraries = ['gmp'] ) )
|
||||
|
||||
|
||||
# Unix-only modules
|
||||
if platform not in ['mac', 'win32']:
|
||||
# Steen Lumholt's termios module
|
||||
|
|
1690
Modules/mpzmodule.c
1690
Modules/mpzmodule.c
File diff suppressed because it is too large
Load Diff
|
@ -1,627 +0,0 @@
|
|||
/***********************************************************
|
||||
Copyright 1994 by Lance Ellinghouse,
|
||||
Cathedral City, California Republic, United States of America.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
Permission to use, copy, modify, and distribute this software and its
|
||||
documentation for any purpose and without fee is hereby granted,
|
||||
provided that the above copyright notice appear in all copies and that
|
||||
both that copyright notice and this permission notice appear in
|
||||
supporting documentation, and that the name of Lance Ellinghouse
|
||||
not be used in advertising or publicity pertaining to distribution
|
||||
of the software without specific, written prior permission.
|
||||
|
||||
LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
||||
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
|
||||
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
||||
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
******************************************************************/
|
||||
|
||||
/* This creates an encryption and decryption engine I am calling
|
||||
a rotor due to the original design was a hardware rotor with
|
||||
contacts used in Germany during WWII.
|
||||
|
||||
Rotor Module:
|
||||
|
||||
- rotor.newrotor('key') -> rotorobject (default of 6 rotors)
|
||||
- rotor.newrotor('key', num_rotors) -> rotorobject
|
||||
|
||||
Rotor Objects:
|
||||
|
||||
- ro.setkey('string') -> None (resets the key as defined in newrotor().
|
||||
- ro.encrypt('string') -> encrypted string
|
||||
- ro.decrypt('encrypted string') -> unencrypted string
|
||||
|
||||
- ro.encryptmore('string') -> encrypted string
|
||||
- ro.decryptmore('encrypted string') -> unencrypted string
|
||||
|
||||
NOTE: the {en,de}cryptmore() methods use the setup that was
|
||||
established via the {en,de}crypt calls. They will NOT
|
||||
re-initalize the rotors unless: 1) They have not been
|
||||
initialized with {en,de}crypt since the last setkey() call;
|
||||
2) {en,de}crypt has not been called for this rotor yet.
|
||||
|
||||
NOTE: you MUST use the SAME key in rotor.newrotor()
|
||||
if you wish to decrypt an encrypted string.
|
||||
Also, the encrypted string is NOT 0-127 ASCII.
|
||||
It is considered BINARY data.
|
||||
|
||||
*/
|
||||
|
||||
/* Rotor objects */
|
||||
|
||||
#include "Python.h"
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
int seed[3];
|
||||
short key[5];
|
||||
int isinited;
|
||||
int size;
|
||||
int size_mask;
|
||||
int rotors;
|
||||
unsigned char *e_rotor; /* [num_rotors][size] */
|
||||
unsigned char *d_rotor; /* [num_rotors][size] */
|
||||
unsigned char *positions; /* [num_rotors] */
|
||||
unsigned char *advances; /* [num_rotors] */
|
||||
} Rotorobj;
|
||||
|
||||
static PyTypeObject Rotor_Type;
|
||||
|
||||
#define is_rotor(v) ((v)->ob_type == &Rotor_Type)
|
||||
|
||||
|
||||
/* This defines the necessary routines to manage rotor objects */
|
||||
|
||||
static void
|
||||
set_seed(Rotorobj *r)
|
||||
{
|
||||
r->seed[0] = r->key[0];
|
||||
r->seed[1] = r->key[1];
|
||||
r->seed[2] = r->key[2];
|
||||
r->isinited = FALSE;
|
||||
}
|
||||
|
||||
/* Return the next random number in the range [0.0 .. 1.0) */
|
||||
static double
|
||||
r_random(Rotorobj *r)
|
||||
{
|
||||
int x, y, z;
|
||||
double val, term;
|
||||
|
||||
x = r->seed[0];
|
||||
y = r->seed[1];
|
||||
z = r->seed[2];
|
||||
|
||||
x = 171 * (x % 177) - 2 * (x/177);
|
||||
y = 172 * (y % 176) - 35 * (y/176);
|
||||
z = 170 * (z % 178) - 63 * (z/178);
|
||||
|
||||
if (x < 0) x = x + 30269;
|
||||
if (y < 0) y = y + 30307;
|
||||
if (z < 0) z = z + 30323;
|
||||
|
||||
r->seed[0] = x;
|
||||
r->seed[1] = y;
|
||||
r->seed[2] = z;
|
||||
|
||||
term = (double)(
|
||||
(((double)x)/(double)30269.0) +
|
||||
(((double)y)/(double)30307.0) +
|
||||
(((double)z)/(double)30323.0)
|
||||
);
|
||||
val = term - (double)floor((double)term);
|
||||
|
||||
if (val >= 1.0)
|
||||
val = 0.0;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
static short
|
||||
r_rand(Rotorobj *r, short s)
|
||||
{
|
||||
return (short)((short)(r_random(r) * (double)s) % s);
|
||||
}
|
||||
|
||||
static void
|
||||
set_key(Rotorobj *r, char *key)
|
||||
{
|
||||
unsigned long k1=995, k2=576, k3=767, k4=671, k5=463;
|
||||
size_t i;
|
||||
size_t len = strlen(key);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
unsigned short ki = Py_CHARMASK(key[i]);
|
||||
|
||||
k1 = (((k1<<3 | k1>>13) + ki) & 65535);
|
||||
k2 = (((k2<<3 | k2>>13) ^ ki) & 65535);
|
||||
k3 = (((k3<<3 | k3>>13) - ki) & 65535);
|
||||
k4 = ((ki - (k4<<3 | k4>>13)) & 65535);
|
||||
k5 = (((k5<<3 | k5>>13) ^ ~ki) & 65535);
|
||||
}
|
||||
r->key[0] = (short)k1;
|
||||
r->key[1] = (short)(k2|1);
|
||||
r->key[2] = (short)k3;
|
||||
r->key[3] = (short)k4;
|
||||
r->key[4] = (short)k5;
|
||||
|
||||
set_seed(r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* These define the interface to a rotor object */
|
||||
static Rotorobj *
|
||||
rotorobj_new(int num_rotors, char *key)
|
||||
{
|
||||
Rotorobj *xp;
|
||||
|
||||
xp = PyObject_New(Rotorobj, &Rotor_Type);
|
||||
if (xp == NULL)
|
||||
return NULL;
|
||||
set_key(xp, key);
|
||||
|
||||
xp->size = 256;
|
||||
xp->size_mask = xp->size - 1;
|
||||
xp->size_mask = 0;
|
||||
xp->rotors = num_rotors;
|
||||
xp->e_rotor = NULL;
|
||||
xp->d_rotor = NULL;
|
||||
xp->positions = NULL;
|
||||
xp->advances = NULL;
|
||||
|
||||
if (!(xp->e_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size)))
|
||||
goto finally;
|
||||
if (!(xp->d_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size)))
|
||||
goto finally;
|
||||
if (!(xp->positions = PyMem_NEW(unsigned char, num_rotors)))
|
||||
goto finally;
|
||||
if (!(xp->advances = PyMem_NEW(unsigned char, num_rotors)))
|
||||
goto finally;
|
||||
|
||||
return xp;
|
||||
|
||||
finally:
|
||||
if (xp->e_rotor)
|
||||
PyMem_DEL(xp->e_rotor);
|
||||
if (xp->d_rotor)
|
||||
PyMem_DEL(xp->d_rotor);
|
||||
if (xp->positions)
|
||||
PyMem_DEL(xp->positions);
|
||||
if (xp->advances)
|
||||
PyMem_DEL(xp->advances);
|
||||
Py_DECREF(xp);
|
||||
return (Rotorobj*)PyErr_NoMemory();
|
||||
}
|
||||
|
||||
|
||||
/* These routines implement the rotor itself */
|
||||
|
||||
/* Here is a fairly sophisticated {en,de}cryption system. It is based on
|
||||
the idea of a "rotor" machine. A bunch of rotors, each with a
|
||||
different permutation of the alphabet, rotate around a different amount
|
||||
after encrypting one character. The current state of the rotors is
|
||||
used to encrypt one character.
|
||||
|
||||
The code is smart enough to tell if your alphabet has a number of
|
||||
characters equal to a power of two. If it does, it uses logical
|
||||
operations, if not it uses div and mod (both require a division).
|
||||
|
||||
You will need to make two changes to the code 1) convert to c, and
|
||||
customize for an alphabet of 255 chars 2) add a filter at the begining,
|
||||
and end, which subtracts one on the way in, and adds one on the way
|
||||
out.
|
||||
|
||||
You might wish to do some timing studies. Another viable alternative
|
||||
is to "byte stuff" the encrypted data of a normal (perhaps this one)
|
||||
encryption routine.
|
||||
|
||||
j'
|
||||
|
||||
*/
|
||||
|
||||
/* Note: the C code here is a fairly straightforward transliteration of a
|
||||
* rotor implemented in lisp. The original lisp code has been removed from
|
||||
* this file to for simplification, but I've kept the docstrings as
|
||||
* comments in front of the functions.
|
||||
*/
|
||||
|
||||
|
||||
/* Set ROTOR to the identity permutation */
|
||||
static void
|
||||
RTR_make_id_rotor(Rotorobj *r, unsigned char *rtr)
|
||||
{
|
||||
register int j;
|
||||
register int size = r->size;
|
||||
for (j = 0; j < size; j++) {
|
||||
rtr[j] = (unsigned char)j;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* The current set of encryption rotors */
|
||||
static void
|
||||
RTR_e_rotors(Rotorobj *r)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < r->rotors; i++) {
|
||||
RTR_make_id_rotor(r, &(r->e_rotor[(i*r->size)]));
|
||||
}
|
||||
}
|
||||
|
||||
/* The current set of decryption rotors */
|
||||
static void
|
||||
RTR_d_rotors(Rotorobj *r)
|
||||
{
|
||||
register int i, j;
|
||||
for (i = 0; i < r->rotors; i++) {
|
||||
for (j = 0; j < r->size; j++) {
|
||||
r->d_rotor[((i*r->size)+j)] = (unsigned char)j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The positions of the rotors at this time */
|
||||
static void
|
||||
RTR_positions(Rotorobj *r)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < r->rotors; i++) {
|
||||
r->positions[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* The number of positions to advance the rotors at a time */
|
||||
static void
|
||||
RTR_advances(Rotorobj *r)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < r->rotors; i++) {
|
||||
r->advances[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Permute the E rotor, and make the D rotor its inverse
|
||||
* see Knuth for explanation of algorithm.
|
||||
*/
|
||||
static void
|
||||
RTR_permute_rotor(Rotorobj *r, unsigned char *e, unsigned char *d)
|
||||
{
|
||||
short i = r->size;
|
||||
short q;
|
||||
unsigned char j;
|
||||
RTR_make_id_rotor(r,e);
|
||||
while (2 <= i) {
|
||||
q = r_rand(r,i);
|
||||
i--;
|
||||
j = e[q];
|
||||
e[q] = (unsigned char)e[i];
|
||||
e[i] = (unsigned char)j;
|
||||
d[j] = (unsigned char)i;
|
||||
}
|
||||
e[0] = (unsigned char)e[0];
|
||||
d[(e[0])] = (unsigned char)0;
|
||||
}
|
||||
|
||||
/* Given KEY (a list of 5 16 bit numbers), initialize the rotor machine.
|
||||
* Set the advancement, position, and permutation of the rotors
|
||||
*/
|
||||
static void
|
||||
RTR_init(Rotorobj *r)
|
||||
{
|
||||
int i;
|
||||
set_seed(r);
|
||||
RTR_positions(r);
|
||||
RTR_advances(r);
|
||||
RTR_e_rotors(r);
|
||||
RTR_d_rotors(r);
|
||||
for (i = 0; i < r->rotors; i++) {
|
||||
r->positions[i] = (unsigned char) r_rand(r, (short)r->size);
|
||||
r->advances[i] = (1+(2*(r_rand(r, (short)(r->size/2)))));
|
||||
RTR_permute_rotor(r,
|
||||
&(r->e_rotor[(i*r->size)]),
|
||||
&(r->d_rotor[(i*r->size)]));
|
||||
}
|
||||
r->isinited = TRUE;
|
||||
}
|
||||
|
||||
/* Change the RTR-positions vector, using the RTR-advances vector */
|
||||
static void
|
||||
RTR_advance(Rotorobj *r)
|
||||
{
|
||||
register int i=0, temp=0;
|
||||
if (r->size_mask) {
|
||||
while (i < r->rotors) {
|
||||
temp = r->positions[i] + r->advances[i];
|
||||
r->positions[i] = temp & r->size_mask;
|
||||
if ((temp >= r->size) && (i < (r->rotors - 1))) {
|
||||
r->positions[(i+1)] = 1 + r->positions[(i+1)];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
while (i < r->rotors) {
|
||||
temp = r->positions[i] + r->advances[i];
|
||||
r->positions[i] = temp%r->size;
|
||||
if ((temp >= r->size) && (i < (r->rotors - 1))) {
|
||||
r->positions[(i+1)] = 1 + r->positions[(i+1)];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Encrypt the character P with the current rotor machine */
|
||||
static unsigned char
|
||||
RTR_e_char(Rotorobj *r, unsigned char p)
|
||||
{
|
||||
register int i=0;
|
||||
register unsigned char tp=p;
|
||||
if (r->size_mask) {
|
||||
while (i < r->rotors) {
|
||||
tp = r->e_rotor[(i*r->size) +
|
||||
(((r->positions[i] ^ tp) &
|
||||
r->size_mask))];
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
while (i < r->rotors) {
|
||||
tp = r->e_rotor[(i*r->size) +
|
||||
(((r->positions[i] ^ tp) %
|
||||
(unsigned int) r->size))];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
RTR_advance(r);
|
||||
return ((unsigned char)tp);
|
||||
}
|
||||
|
||||
/* Decrypt the character C with the current rotor machine */
|
||||
static unsigned char
|
||||
RTR_d_char(Rotorobj *r, unsigned char c)
|
||||
{
|
||||
register int i = r->rotors - 1;
|
||||
register unsigned char tc = c;
|
||||
|
||||
if (r->size_mask) {
|
||||
while (0 <= i) {
|
||||
tc = (r->positions[i] ^
|
||||
r->d_rotor[(i*r->size)+tc]) & r->size_mask;
|
||||
i--;
|
||||
}
|
||||
} else {
|
||||
while (0 <= i) {
|
||||
tc = (r->positions[i] ^
|
||||
r->d_rotor[(i*r->size)+tc]) %
|
||||
(unsigned int) r->size;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
RTR_advance(r);
|
||||
return(tc);
|
||||
}
|
||||
|
||||
/* Perform a rotor encryption of the region from BEG to END by KEY */
|
||||
static void
|
||||
RTR_e_region(Rotorobj *r, unsigned char *beg, int len, int doinit)
|
||||
{
|
||||
register int i;
|
||||
if (doinit || r->isinited == FALSE)
|
||||
RTR_init(r);
|
||||
for (i = 0; i < len; i++) {
|
||||
beg[i] = RTR_e_char(r, beg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Perform a rotor decryption of the region from BEG to END by KEY */
|
||||
static void
|
||||
RTR_d_region(Rotorobj *r, unsigned char *beg, int len, int doinit)
|
||||
{
|
||||
register int i;
|
||||
if (doinit || r->isinited == FALSE)
|
||||
RTR_init(r);
|
||||
for (i = 0; i < len; i++) {
|
||||
beg[i] = RTR_d_char(r, beg[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Rotor methods */
|
||||
static void
|
||||
rotor_dealloc(Rotorobj *xp)
|
||||
{
|
||||
if (xp->e_rotor)
|
||||
PyMem_DEL(xp->e_rotor);
|
||||
if (xp->d_rotor)
|
||||
PyMem_DEL(xp->d_rotor);
|
||||
if (xp->positions)
|
||||
PyMem_DEL(xp->positions);
|
||||
if (xp->advances)
|
||||
PyMem_DEL(xp->advances);
|
||||
PyObject_Del(xp);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rotorobj_encrypt(Rotorobj *self, PyObject *args)
|
||||
{
|
||||
char *string = NULL;
|
||||
int len = 0;
|
||||
PyObject *rtn = NULL;
|
||||
char *tmp;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s#:encrypt", &string, &len))
|
||||
return NULL;
|
||||
if (!(tmp = PyMem_NEW(char, len+5))) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
memset(tmp, '\0', len+1);
|
||||
memcpy(tmp, string, len);
|
||||
RTR_e_region(self, (unsigned char *)tmp, len, TRUE);
|
||||
rtn = PyString_FromStringAndSize(tmp, len);
|
||||
PyMem_DEL(tmp);
|
||||
return(rtn);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rotorobj_encrypt_more(Rotorobj *self, PyObject *args)
|
||||
{
|
||||
char *string = NULL;
|
||||
int len = 0;
|
||||
PyObject *rtn = NULL;
|
||||
char *tmp;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s#:encrypt_more", &string, &len))
|
||||
return NULL;
|
||||
if (!(tmp = PyMem_NEW(char, len+5))) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
memset(tmp, '\0', len+1);
|
||||
memcpy(tmp, string, len);
|
||||
RTR_e_region(self, (unsigned char *)tmp, len, FALSE);
|
||||
rtn = PyString_FromStringAndSize(tmp, len);
|
||||
PyMem_DEL(tmp);
|
||||
return(rtn);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rotorobj_decrypt(Rotorobj *self, PyObject *args)
|
||||
{
|
||||
char *string = NULL;
|
||||
int len = 0;
|
||||
PyObject *rtn = NULL;
|
||||
char *tmp;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s#:decrypt", &string, &len))
|
||||
return NULL;
|
||||
if (!(tmp = PyMem_NEW(char, len+5))) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
memset(tmp, '\0', len+1);
|
||||
memcpy(tmp, string, len);
|
||||
RTR_d_region(self, (unsigned char *)tmp, len, TRUE);
|
||||
rtn = PyString_FromStringAndSize(tmp, len);
|
||||
PyMem_DEL(tmp);
|
||||
return(rtn);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rotorobj_decrypt_more(Rotorobj *self, PyObject *args)
|
||||
{
|
||||
char *string = NULL;
|
||||
int len = 0;
|
||||
PyObject *rtn = NULL;
|
||||
char *tmp;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s#:decrypt_more", &string, &len))
|
||||
return NULL;
|
||||
if (!(tmp = PyMem_NEW(char, len+5))) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
memset(tmp, '\0', len+1);
|
||||
memcpy(tmp, string, len);
|
||||
RTR_d_region(self, (unsigned char *)tmp, len, FALSE);
|
||||
rtn = PyString_FromStringAndSize(tmp, len);
|
||||
PyMem_DEL(tmp);
|
||||
return(rtn);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rotorobj_setkey(Rotorobj *self, PyObject *args)
|
||||
{
|
||||
char *key;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s:setkey", &key))
|
||||
return NULL;
|
||||
|
||||
set_key(self, key);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
static struct PyMethodDef
|
||||
rotorobj_methods[] = {
|
||||
{"encrypt", (PyCFunction)rotorobj_encrypt, METH_VARARGS},
|
||||
{"encryptmore", (PyCFunction)rotorobj_encrypt_more, METH_VARARGS},
|
||||
{"decrypt", (PyCFunction)rotorobj_decrypt, METH_VARARGS},
|
||||
{"decryptmore", (PyCFunction)rotorobj_decrypt_more, METH_VARARGS},
|
||||
{"setkey", (PyCFunction)rotorobj_setkey, METH_VARARGS},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
||||
/* Return a rotor object's named attribute. */
|
||||
static PyObject *
|
||||
rotorobj_getattr(Rotorobj *s, char *name)
|
||||
{
|
||||
return Py_FindMethod(rotorobj_methods, (PyObject*)s, name);
|
||||
}
|
||||
|
||||
|
||||
static PyTypeObject Rotor_Type = {
|
||||
PyObject_HEAD_INIT(NULL)
|
||||
0, /*ob_size*/
|
||||
"rotor.rotor", /*tp_name*/
|
||||
sizeof(Rotorobj), /*tp_size*/
|
||||
0, /*tp_itemsize*/
|
||||
/* methods */
|
||||
(destructor)rotor_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
(getattrfunc)rotorobj_getattr, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare*/
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_hash*/
|
||||
};
|
||||
|
||||
|
||||
static PyObject *
|
||||
rotor_rotor(PyObject *self, PyObject *args)
|
||||
{
|
||||
Rotorobj *r;
|
||||
char *string;
|
||||
int num_rotors = 6;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s|i:newrotor", &string, &num_rotors))
|
||||
return NULL;
|
||||
|
||||
r = rotorobj_new(num_rotors, string);
|
||||
return (PyObject *)r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct PyMethodDef
|
||||
rotor_methods[] = {
|
||||
{"newrotor", rotor_rotor, METH_VARARGS},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
||||
PyMODINIT_FUNC
|
||||
initrotor(void)
|
||||
{
|
||||
Rotor_Type.ob_type = &PyType_Type;
|
||||
(void)Py_InitModule("rotor", rotor_methods);
|
||||
if (PyErr_Warn(PyExc_DeprecationWarning,
|
||||
"the rotor module uses an insecure algorithm "
|
||||
"and is deprecated") < 0)
|
||||
return;
|
||||
}
|
|
@ -1,171 +0,0 @@
|
|||
#include "Python.h"
|
||||
|
||||
PyDoc_STRVAR(xreadlines_doc,
|
||||
"xreadlines(f)\n\
|
||||
\n\
|
||||
Return an xreadlines object for the file f.");
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
PyObject *file;
|
||||
PyObject *lines;
|
||||
int lineslen;
|
||||
int lineno;
|
||||
int abslineno;
|
||||
} PyXReadlinesObject;
|
||||
|
||||
static PyTypeObject XReadlinesObject_Type;
|
||||
|
||||
static void
|
||||
xreadlines_dealloc(PyXReadlinesObject *op)
|
||||
{
|
||||
Py_XDECREF(op->file);
|
||||
Py_XDECREF(op->lines);
|
||||
PyObject_DEL(op);
|
||||
}
|
||||
|
||||
/* A larger chunk size doesn't seem to make a difference */
|
||||
#define CHUNKSIZE 8192
|
||||
|
||||
static PyXReadlinesObject *
|
||||
newreadlinesobject(PyObject *file)
|
||||
{
|
||||
PyXReadlinesObject *op;
|
||||
op = PyObject_NEW(PyXReadlinesObject, &XReadlinesObject_Type);
|
||||
if (op == NULL)
|
||||
return NULL;
|
||||
Py_XINCREF(file);
|
||||
op->file = file;
|
||||
op->lines = NULL;
|
||||
op->abslineno = op->lineno = op->lineslen = 0;
|
||||
return op;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
xreadlines(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *file;
|
||||
PyXReadlinesObject *ret;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O:xreadlines", &file))
|
||||
return NULL;
|
||||
ret = newreadlinesobject(file);
|
||||
return (PyObject*)ret;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
xreadlines_common(PyXReadlinesObject *a)
|
||||
{
|
||||
if (a->lineno >= a->lineslen) {
|
||||
Py_XDECREF(a->lines);
|
||||
a->lines = PyObject_CallMethod(a->file, "readlines", "(i)",
|
||||
CHUNKSIZE);
|
||||
if (a->lines == NULL)
|
||||
return NULL;
|
||||
a->lineno = 0;
|
||||
if ((a->lineslen = PySequence_Size(a->lines)) < 0)
|
||||
return NULL;
|
||||
}
|
||||
a->abslineno++;
|
||||
return PySequence_GetItem(a->lines, a->lineno++);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
xreadlines_item(PyXReadlinesObject *a, int i)
|
||||
{
|
||||
if (i != a->abslineno) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
"xreadlines object accessed out of order");
|
||||
return NULL;
|
||||
}
|
||||
return xreadlines_common(a);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
xreadlines_iternext(PyXReadlinesObject *a)
|
||||
{
|
||||
PyObject *res;
|
||||
|
||||
res = xreadlines_common(a);
|
||||
if (res == NULL && PyErr_ExceptionMatches(PyExc_IndexError))
|
||||
PyErr_Clear();
|
||||
return res;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
xreadlines_next(PyXReadlinesObject *a, PyObject *args)
|
||||
{
|
||||
PyObject *res;
|
||||
|
||||
if (!PyArg_ParseTuple(args, ""))
|
||||
return NULL;
|
||||
res = xreadlines_common(a);
|
||||
if (res == NULL && PyErr_ExceptionMatches(PyExc_IndexError))
|
||||
PyErr_SetObject(PyExc_StopIteration, Py_None);
|
||||
return res;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(next_doc, "x.next() -> the next line or raise StopIteration");
|
||||
|
||||
static PyMethodDef xreadlines_methods[] = {
|
||||
{"next", (PyCFunction)xreadlines_next, METH_VARARGS, next_doc},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static PyObject *
|
||||
xreadlines_getattr(PyObject *a, char *name)
|
||||
{
|
||||
return Py_FindMethod(xreadlines_methods, a, name);
|
||||
}
|
||||
|
||||
static PySequenceMethods xreadlines_as_sequence = {
|
||||
0, /*sq_length*/
|
||||
0, /*sq_concat*/
|
||||
0, /*sq_repeat*/
|
||||
(intargfunc)xreadlines_item, /*sq_item*/
|
||||
};
|
||||
|
||||
static PyTypeObject XReadlinesObject_Type = {
|
||||
PyObject_HEAD_INIT(NULL)
|
||||
0,
|
||||
"xreadlines.xreadlines",
|
||||
sizeof(PyXReadlinesObject),
|
||||
0,
|
||||
(destructor)xreadlines_dealloc, /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
xreadlines_getattr, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_compare */
|
||||
0, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
&xreadlines_as_sequence, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
||||
0, /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
PyObject_SelfIter, /* tp_iter */
|
||||
(iternextfunc)xreadlines_iternext, /* tp_iternext */
|
||||
};
|
||||
|
||||
static PyMethodDef xreadlines_functions[] = {
|
||||
{"xreadlines", xreadlines, METH_VARARGS, xreadlines_doc},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC
|
||||
initxreadlines(void)
|
||||
{
|
||||
XReadlinesObject_Type.ob_type = &PyType_Type;
|
||||
Py_InitModule("xreadlines", xreadlines_functions);
|
||||
PyErr_Warn(PyExc_DeprecationWarning,
|
||||
"xreadlines is deprecated; use 'for line in file'.");
|
||||
}
|
24
setup.py
24
setup.py
|
@ -300,7 +300,6 @@ class PyBuildExt(build_ext):
|
|||
|
||||
exts.append( Extension('_hotshot', ['_hotshot.c']) )
|
||||
exts.append( Extension('_weakref', ['_weakref.c']) )
|
||||
exts.append( Extension('xreadlines', ['xreadlinesmodule.c']) )
|
||||
|
||||
# array objects
|
||||
exts.append( Extension('array', ['arraymodule.c']) )
|
||||
|
@ -389,9 +388,7 @@ class PyBuildExt(build_ext):
|
|||
if platform not in ['atheos', 'mac']:
|
||||
exts.append( Extension('mmap', ['mmapmodule.c']) )
|
||||
|
||||
# Lance Ellinghaus's modules:
|
||||
# enigma-inspired encryption
|
||||
exts.append( Extension('rotor', ['rotormodule.c']) )
|
||||
# Lance Ellinghaus's syslog module
|
||||
if platform not in ['mac']:
|
||||
# syslog daemon interface
|
||||
exts.append( Extension('syslog', ['syslogmodule.c']) )
|
||||
|
@ -629,25 +626,6 @@ class PyBuildExt(build_ext):
|
|||
exts.append( Extension('gdbm', ['gdbmmodule.c'],
|
||||
libraries = ['gdbm'] ) )
|
||||
|
||||
# The mpz module interfaces to the GNU Multiple Precision library.
|
||||
# You need to ftp the GNU MP library.
|
||||
# This was originally written and tested against GMP 1.2 and 1.3.2.
|
||||
# It has been modified by Rob Hooft to work with 2.0.2 as well, but I
|
||||
# haven't tested it recently, and it definitely doesn't work with
|
||||
# GMP 4.0. For more complete modules, refer to
|
||||
# http://gmpy.sourceforge.net and
|
||||
# http://www.egenix.com/files/python/mxNumber.html
|
||||
|
||||
# A compatible MP library unencumbered by the GPL also exists. It was
|
||||
# posted to comp.sources.misc in volume 40 and is widely available from
|
||||
# FTP archive sites. One URL for it is:
|
||||
# ftp://gatekeeper.dec.com/.b/usenet/comp.sources.misc/volume40/fgmp/part01.Z
|
||||
|
||||
if (self.compiler.find_library_file(lib_dirs, 'gmp')):
|
||||
exts.append( Extension('mpz', ['mpzmodule.c'],
|
||||
libraries = ['gmp'] ) )
|
||||
|
||||
|
||||
# Unix-only modules
|
||||
if platform not in ['mac', 'win32']:
|
||||
# Steen Lumholt's termios module
|
||||
|
|
Loading…
Reference in New Issue