diff options
author | Gregory P. Smith <greg@mad-scientist.com> | 2009-02-13 03:00:00 +0000 |
---|---|---|
committer | Gregory P. Smith <greg@mad-scientist.com> | 2009-02-13 03:00:00 +0000 |
commit | ea38826ab28e1621f109953f068992c69d386dee (patch) | |
tree | 5816309e758042633f930c9e0781d456ba97dac5 /Modules/md5module.c | |
parent | b516c126ef7b69e4b6560fe98ed4529f21fdb7eb (diff) | |
download | cpython-git-ea38826ab28e1621f109953f068992c69d386dee.tar.gz |
- Issue #3745: Fix hashlib to always reject unicode and non buffer-api
supporting objects as input no matter how it was compiled (built in
implementations or external openssl library).
(backported from a py3k branch)
Diffstat (limited to 'Modules/md5module.c')
-rw-r--r-- | Modules/md5module.c | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/Modules/md5module.c b/Modules/md5module.c index 1e2e6e3ee8..0b150a0bd2 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -12,6 +12,7 @@ #include "Python.h" #include "structmember.h" #include "md5.h" +#include "hashlib.h" typedef struct { PyObject_HEAD @@ -50,14 +51,18 @@ md5_dealloc(md5object *md5p) static PyObject * md5_update(md5object *self, PyObject *args) { - unsigned char *cp; - int len; + PyObject *data_obj; + Py_buffer view; - if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) + if (!PyArg_ParseTuple(args, "O:update", &data_obj)) return NULL; - md5_append(&self->md5, cp, len); + GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); + md5_append(&self->md5, (unsigned char*)view.buf, + Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); + + PyBuffer_Release(&view); Py_INCREF(Py_None); return Py_None; } @@ -261,18 +266,25 @@ static PyObject * MD5_new(PyObject *self, PyObject *args) { md5object *md5p; - unsigned char *cp = NULL; - int len = 0; + PyObject *data_obj = NULL; + Py_buffer view; - if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len)) + if (!PyArg_ParseTuple(args, "|O:new", &data_obj)) return NULL; - if ((md5p = newmd5object()) == NULL) + GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); + + if ((md5p = newmd5object()) == NULL) { + PyBuffer_Release(&view); return NULL; + } - if (cp) - md5_append(&md5p->md5, cp, len); + if (data_obj) { + md5_append(&md5p->md5, (unsigned char*)view.buf, + Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); + } + PyBuffer_Release(&view); return (PyObject *)md5p; } |