[Python-checkins] CVS: python/dist/src/Modules shamodule.c,2.6,2.7
Fred L. Drake
python-dev@python.org
Fri, 7 Jul 2000 23:41:05 -0700
Update of /cvsroot/python/python/dist/src/Modules
In directory slayer.i.sourceforge.net:/tmp/cvs-serv24609
Modified Files:
shamodule.c
Log Message:
ANSI-fied sources, converted to four-space indentation.
Converted to PyArg_ParseTuple() with method names to get better error
messages.
Index: shamodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/shamodule.c,v
retrieving revision 2.6
retrieving revision 2.7
diff -C2 -r2.6 -r2.7
*** shamodule.c 2000/06/30 23:50:38 2.6
--- shamodule.c 2000/07/08 06:41:03 2.7
***************
*** 56,65 ****
typedef struct {
! PyObject_HEAD
! SHA_INT32 digest[5]; /* Message digest */
! SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
! SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
! int Endianness;
! int local; /* unprocessed amount in data */
} SHAobject;
--- 56,65 ----
typedef struct {
! PyObject_HEAD
! SHA_INT32 digest[5]; /* Message digest */
! SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
! SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
! int Endianness;
! int local; /* unprocessed amount in data */
} SHAobject;
***************
*** 67,73 ****
array of longwords. */
! static void longReverse(buffer, byteCount, Endianness)
! SHA_INT32 *buffer;
! int byteCount, Endianness;
{
SHA_INT32 value;
--- 67,71 ----
array of longwords. */
! static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
{
SHA_INT32 value;
***************
*** 77,82 ****
byteCount /= sizeof(*buffer);
! while( byteCount-- )
! {
value = *buffer;
value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
--- 75,79 ----
byteCount /= sizeof(*buffer);
! while (byteCount--) {
value = *buffer;
value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
***************
*** 86,98 ****
}
! static void SHAcopy(src, dest)
! SHAobject *src, *dest;
{
! dest->Endianness = src->Endianness;
! dest->local = src->local;
! dest->count_lo = src->count_lo;
! dest->count_hi = src->count_hi;
! memcpy(dest->digest, src->digest, sizeof(src->digest));
! memcpy(dest->data, src->data, sizeof(src->data));
}
--- 83,94 ----
}
! static void SHAcopy(SHAobject *src, SHAobject *dest)
{
! dest->Endianness = src->Endianness;
! dest->local = src->local;
! dest->count_lo = src->count_lo;
! dest->count_hi = src->count_hi;
! memcpy(dest->digest, src->digest, sizeof(src->digest));
! memcpy(dest->data, src->data, sizeof(src->data));
}
***************
*** 174,179 ****
static void
! sha_transform(sha_info)
! SHAobject *sha_info;
{
int i;
--- 170,174 ----
static void
! sha_transform(SHAobject *sha_info)
{
int i;
***************
*** 236,241 ****
static void
! sha_init(sha_info)
! SHAobject *sha_info;
{
TestEndianness(sha_info->Endianness)
--- 231,235 ----
static void
! sha_init(SHAobject *sha_info)
{
TestEndianness(sha_info->Endianness)
***************
*** 254,261 ****
static void
! sha_update(sha_info, buffer, count)
! SHAobject *sha_info;
! SHA_BYTE *buffer;
! int count;
{
int i;
--- 248,252 ----
static void
! sha_update(SHAobject *sha_info, SHA_BYTE *buffer, int count)
{
int i;
***************
*** 264,292 ****
clo = sha_info->count_lo + ((SHA_INT32) count << 3);
if (clo < sha_info->count_lo) {
! ++sha_info->count_hi;
}
sha_info->count_lo = clo;
sha_info->count_hi += (SHA_INT32) count >> 29;
if (sha_info->local) {
! i = SHA_BLOCKSIZE - sha_info->local;
! if (i > count) {
! i = count;
! }
! memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local,
! buffer, i);
! count -= i;
! buffer += i;
! sha_info->local += i;
! if (sha_info->local == SHA_BLOCKSIZE) {
! sha_transform(sha_info);
! } else {
! return;
! }
}
while (count >= SHA_BLOCKSIZE) {
! memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
! buffer += SHA_BLOCKSIZE;
! count -= SHA_BLOCKSIZE;
! sha_transform(sha_info);
}
memcpy(sha_info->data, buffer, count);
--- 255,283 ----
clo = sha_info->count_lo + ((SHA_INT32) count << 3);
if (clo < sha_info->count_lo) {
! ++sha_info->count_hi;
}
sha_info->count_lo = clo;
sha_info->count_hi += (SHA_INT32) count >> 29;
if (sha_info->local) {
! i = SHA_BLOCKSIZE - sha_info->local;
! if (i > count) {
! i = count;
! }
! memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
! count -= i;
! buffer += i;
! sha_info->local += i;
! if (sha_info->local == SHA_BLOCKSIZE) {
! sha_transform(sha_info);
! }
! else {
! return;
! }
}
while (count >= SHA_BLOCKSIZE) {
! memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
! buffer += SHA_BLOCKSIZE;
! count -= SHA_BLOCKSIZE;
! sha_transform(sha_info);
}
memcpy(sha_info->data, buffer, count);
***************
*** 297,303 ****
static void
! sha_final(digest, sha_info)
! unsigned char digest[20];
! SHAobject *sha_info;
{
int count;
--- 288,292 ----
static void
! sha_final(unsigned char digest[20], SHAobject *sha_info)
{
int count;
***************
*** 308,313 ****
count = (int) ((lo_bit_count >> 3) & 0x3f);
((SHA_BYTE *) sha_info->data)[count++] = 0x80;
! if (count > SHA_BLOCKSIZE - 8)
! {
memset(((SHA_BYTE *) sha_info->data) + count, 0,
SHA_BLOCKSIZE - count);
--- 297,301 ----
count = (int) ((lo_bit_count >> 3) & 0x3f);
((SHA_BYTE *) sha_info->data)[count++] = 0x80;
! if (count > SHA_BLOCKSIZE - 8) {
memset(((SHA_BYTE *) sha_info->data) + count, 0,
SHA_BLOCKSIZE - count);
***************
*** 315,320 ****
memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
}
! else
! {
memset(((SHA_BYTE *) sha_info->data) + count, 0,
SHA_BLOCKSIZE - 8 - count);
--- 303,307 ----
memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
}
! else {
memset(((SHA_BYTE *) sha_info->data) + count, 0,
SHA_BLOCKSIZE - 8 - count);
***************
*** 366,370 ****
newSHAobject()
{
! return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
}
--- 353,357 ----
newSHAobject()
{
! return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
}
***************
*** 372,379 ****
static void
! SHA_dealloc(ptr)
! PyObject *ptr;
{
! PyObject_Del(ptr);
}
--- 359,365 ----
static void
! SHA_dealloc(PyObject *ptr)
{
! PyObject_Del(ptr);
}
***************
*** 385,403 ****
static PyObject *
! SHA_copy(self, args)
! SHAobject *self;
! PyObject *args;
! {
! SHAobject *newobj;
!
! if (!PyArg_NoArgs(args)) {
! return NULL;
! }
! if ( (newobj = newSHAobject())==NULL)
! return NULL;
!
! SHAcopy(self, newobj);
! return (PyObject *)newobj;
}
--- 371,386 ----
static PyObject *
! SHA_copy(SHAobject *self, PyObject *args)
! {
! SHAobject *newobj;
! if (!PyArg_ParseTuple(args, ":copy")) {
! return NULL;
! }
! if ( (newobj = newSHAobject())==NULL)
! return NULL;
!
! SHAcopy(self, newobj);
! return (PyObject *)newobj;
}
***************
*** 406,422 ****
static PyObject *
! SHA_digest(self, args)
! SHAobject *self;
! PyObject *args;
! {
! unsigned char digest[SHA_DIGESTSIZE];
! SHAobject temp;
!
! if (!PyArg_NoArgs(args))
! return NULL;
!
! SHAcopy(self, &temp);
! sha_final(digest, &temp);
! return PyString_FromStringAndSize((const char *)digest, sizeof(digest));
}
--- 389,403 ----
static PyObject *
! SHA_digest(SHAobject *self, PyObject *args)
! {
! unsigned char digest[SHA_DIGESTSIZE];
! SHAobject temp;
!
! if (!PyArg_ParseTuple(args, ":digest"))
! return NULL;
!
! SHAcopy(self, &temp);
! sha_final(digest, &temp);
! return PyString_FromStringAndSize((const char *)digest, sizeof(digest));
}
***************
*** 425,460 ****
static PyObject *
! SHA_hexdigest(self, args)
! SHAobject *self;
! PyObject *args;
! {
! unsigned char digest[SHA_DIGESTSIZE];
! SHAobject temp;
! PyObject *retval;
! char *hex_digest;
! int i, j;
!
! if (!PyArg_NoArgs(args))
! return NULL;
!
! /* Get the raw (binary) digest value */
! SHAcopy(self, &temp);
! sha_final(digest, &temp);
!
! /* Create a new string */
! retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
! hex_digest = PyString_AsString(retval);
!
! /* Make hex version of the digest */
! for(i=j=0; i<sizeof(digest); i++)
! {
! char c;
! c = digest[i] / 16; c = (c>9) ? c+'a'-10 : c + '0';
! hex_digest[j++] = c;
! c = digest[i] % 16; c = (c>9) ? c+'a'-10 : c + '0';
! hex_digest[j++] = c;
! }
!
! return retval;
}
--- 406,437 ----
static PyObject *
! SHA_hexdigest(SHAobject *self, PyObject *args)
! {
! unsigned char digest[SHA_DIGESTSIZE];
! SHAobject temp;
! PyObject *retval;
! char *hex_digest;
! int i, j;
!
! if (!PyArg_ParseTuple(args, ":hexdigest"))
! return NULL;
!
! /* Get the raw (binary) digest value */
! SHAcopy(self, &temp);
! sha_final(digest, &temp);
!
! /* Create a new string */
! retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
! hex_digest = PyString_AsString(retval);
!
! /* Make hex version of the digest */
! for(i=j=0; i<sizeof(digest); i++) {
! char c;
! c = digest[i] / 16; c = (c>9) ? c+'a'-10 : c + '0';
! hex_digest[j++] = c;
! c = digest[i] % 16; c = (c>9) ? c+'a'-10 : c + '0';
! hex_digest[j++] = c;
! }
! return retval;
}
***************
*** 463,513 ****
static PyObject *
! SHA_update(self, args)
! SHAobject *self;
! PyObject *args;
{
! unsigned char *cp;
! int len;
! if (!PyArg_Parse(args, "s#", &cp, &len))
! return NULL;
! sha_update(self, cp, len);
! Py_INCREF(Py_None);
! return Py_None;
}
static PyMethodDef SHA_methods[] = {
! {"copy", (PyCFunction)SHA_copy, 0, SHA_copy__doc__},
! {"digest", (PyCFunction)SHA_digest, 0, SHA_digest__doc__},
! {"hexdigest", (PyCFunction)SHA_hexdigest, 0, SHA_hexdigest__doc__},
! {"update", (PyCFunction)SHA_update, 0, SHA_update__doc__},
! {NULL, NULL} /* sentinel */
};
static PyObject *
! SHA_getattr(self, name)
! PyObject *self;
! char *name;
! {
! if (strcmp(name, "blocksize")==0)
! return PyInt_FromLong(1);
! if (strcmp(name, "digestsize")==0)
! return PyInt_FromLong(20);
!
! return Py_FindMethod(SHA_methods, self, name);
}
static PyTypeObject SHAtype = {
! PyObject_HEAD_INIT(NULL)
! 0, /*ob_size*/
! "SHA", /*tp_name*/
! sizeof(SHAobject), /*tp_size*/
! 0, /*tp_itemsize*/
! /* methods */
! SHA_dealloc, /*tp_dealloc*/
! 0, /*tp_print*/
! SHA_getattr, /*tp_getattr*/
};
--- 440,486 ----
static PyObject *
! SHA_update(SHAobject *self, PyObject *args)
{
! unsigned char *cp;
! int len;
! if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
! return NULL;
! sha_update(self, cp, len);
! Py_INCREF(Py_None);
! return Py_None;
}
static PyMethodDef SHA_methods[] = {
! {"copy", (PyCFunction)SHA_copy, METH_VARARGS, SHA_copy__doc__},
! {"digest", (PyCFunction)SHA_digest, METH_VARARGS, SHA_digest__doc__},
! {"hexdigest", (PyCFunction)SHA_hexdigest, METH_VARARGS, SHA_hexdigest__doc__},
! {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__},
! {NULL, NULL} /* sentinel */
};
static PyObject *
! SHA_getattr(PyObject *self, char *name)
! {
! if (strcmp(name, "blocksize")==0)
! return PyInt_FromLong(1);
! if (strcmp(name, "digestsize")==0)
! return PyInt_FromLong(20);
!
! return Py_FindMethod(SHA_methods, self, name);
}
static PyTypeObject SHAtype = {
! PyObject_HEAD_INIT(NULL)
! 0, /*ob_size*/
! "SHA", /*tp_name*/
! sizeof(SHAobject), /*tp_size*/
! 0, /*tp_itemsize*/
! /* methods */
! SHA_dealloc, /*tp_dealloc*/
! 0, /*tp_print*/
! SHA_getattr, /*tp_getattr*/
};
***************
*** 521,553 ****
static PyObject *
! SHA_new(self, args, kwdict)
! PyObject *self;
! PyObject *args;
! PyObject *kwdict;
! {
! static char *kwlist[] = {"string", NULL};
! SHAobject *new;
! unsigned char *cp = NULL;
! int len;
! if ((new = newSHAobject()) == NULL)
! return NULL;
! if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
! &cp, &len)) {
! Py_DECREF(new);
! return NULL;
! }
!
! sha_init(new);
!
! if (PyErr_Occurred()) {
! Py_DECREF(new);
! return NULL;
! }
! if (cp)
! sha_update(new, cp, len);
! return (PyObject *)new;
}
--- 494,523 ----
static PyObject *
! SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
! {
! static char *kwlist[] = {"string", NULL};
! SHAobject *new;
! unsigned char *cp = NULL;
! int len;
! if ((new = newSHAobject()) == NULL)
! return NULL;
! if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
! &cp, &len)) {
! Py_DECREF(new);
! return NULL;
! }
!
! sha_init(new);
! if (PyErr_Occurred()) {
! Py_DECREF(new);
! return NULL;
! }
! if (cp)
! sha_update(new, cp, len);
!
! return (PyObject *)new;
}
***************
*** 556,562 ****
static struct PyMethodDef SHA_functions[] = {
! {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
! {"sha", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
! {NULL, NULL} /* Sentinel */
};
--- 526,532 ----
static struct PyMethodDef SHA_functions[] = {
! {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
! {"sha", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
! {NULL, NULL} /* Sentinel */
};
***************
*** 571,589 ****
initsha()
{
! PyObject *d, *m;
! SHAtype.ob_type = &PyType_Type;
! m = Py_InitModule("sha", SHA_functions);
! /* Add some symbolic constants to the module */
! d = PyModule_GetDict(m);
! insint("blocksize", 1); /* For future use, in case some hash
! functions require an integral number of
! blocks */
! insint("digestsize", 20);
!
! /* Check for errors */
! if (PyErr_Occurred())
! Py_FatalError("can't initialize module SHA");
}
-
--- 541,558 ----
initsha()
{
! PyObject *d, *m;
! SHAtype.ob_type = &PyType_Type;
! m = Py_InitModule("sha", SHA_functions);
! /* Add some symbolic constants to the module */
! d = PyModule_GetDict(m);
! insint("blocksize", 1); /* For future use, in case some hash
! functions require an integral number of
! blocks */
! insint("digestsize", 20);
!
! /* Check for errors */
! if (PyErr_Occurred())
! Py_FatalError("can't initialize module SHA");
}