diff options
Diffstat (limited to 'contrib/tools/python3/Modules/md5module.c')
| -rw-r--r-- | contrib/tools/python3/Modules/md5module.c | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/contrib/tools/python3/Modules/md5module.c b/contrib/tools/python3/Modules/md5module.c index dcc9da38d75..c56fa5fc13e 100644 --- a/contrib/tools/python3/Modules/md5module.c +++ b/contrib/tools/python3/Modules/md5module.c @@ -15,14 +15,13 @@ */ /* MD5 objects */ + #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif #include "Python.h" #include "hashlib.h" -#include "pycore_strhex.h" // _Py_strhex() -#include "pycore_typeobject.h" // _PyType_GetModuleState() /*[clinic input] module _md5 @@ -30,15 +29,6 @@ class MD5Type "MD5object *" "&PyType_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/ -/* Some useful types */ - -#if SIZEOF_INT == 4 -typedef unsigned int MD5_INT32; /* 32-bit integer */ -typedef long long MD5_INT64; /* 64-bit integer */ -#else -/* not defined. compilation will die. */ -#endif - /* The MD5 block size and message digest sizes, in bytes */ #define MD5_BLOCKSIZE 64 @@ -50,8 +40,8 @@ typedef long long MD5_INT64; /* 64-bit integer */ typedef struct { PyObject_HEAD // Prevents undefined behavior via multiple threads entering the C API. - // The lock will be NULL before threaded access has been enabled. - PyThread_type_lock lock; + bool use_mutex; + PyMutex mutex; Hacl_Hash_MD5_state_t *hash_state; } MD5object; @@ -74,7 +64,11 @@ static MD5object * newMD5object(MD5State * st) { MD5object *md5 = (MD5object *)PyObject_GC_New(MD5object, st->md5_type); - md5->lock = NULL; + if (!md5) { + return NULL; + } + HASHLIB_INIT_MUTEX(md5); + PyObject_GC_Track(md5); return md5; } @@ -90,9 +84,9 @@ MD5_traverse(PyObject *ptr, visitproc visit, void *arg) static void MD5_dealloc(MD5object *ptr) { - Hacl_Hash_MD5_free(ptr->hash_state); - if (ptr->lock != NULL) { - PyThread_free_lock(ptr->lock); + if (ptr->hash_state != NULL) { + Hacl_Hash_MD5_free(ptr->hash_state); + ptr->hash_state = NULL; } PyTypeObject *tp = Py_TYPE((PyObject*)ptr); PyObject_GC_UnTrack(ptr); @@ -115,7 +109,7 @@ static PyObject * MD5Type_copy_impl(MD5object *self, PyTypeObject *cls) /*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/ { - MD5State *st = _PyType_GetModuleState(cls); + MD5State *st = PyType_GetModuleState(cls); MD5object *newobj; if ((newobj = newMD5object(st))==NULL) @@ -158,7 +152,16 @@ MD5Type_hexdigest_impl(MD5object *self) ENTER_HASHLIB(self); Hacl_Hash_MD5_digest(self->hash_state, digest); LEAVE_HASHLIB(self); - return _Py_strhex((const char*)digest, MD5_DIGESTSIZE); + + const char *hexdigits = "0123456789abcdef"; + char digest_hex[MD5_DIGESTSIZE * 2]; + char *str = digest_hex; + for (size_t i=0; i < MD5_DIGESTSIZE; i++) { + unsigned char byte = digest[i]; + *str++ = hexdigits[byte >> 4]; + *str++ = hexdigits[byte & 0x0f]; + } + return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex)); } static void update(Hacl_Hash_MD5_state_t *state, uint8_t *buf, Py_ssize_t len) { @@ -189,14 +192,14 @@ MD5Type_update(MD5object *self, PyObject *obj) GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); - if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) { - self->lock = PyThread_allocate_lock(); + if (!self->use_mutex && buf.len >= HASHLIB_GIL_MINSIZE) { + self->use_mutex = true; } - if (self->lock != NULL) { + if (self->use_mutex) { Py_BEGIN_ALLOW_THREADS - PyThread_acquire_lock(self->lock, 1); + PyMutex_Lock(&self->mutex); update(self->hash_state, buf.buf, buf.len); - PyThread_release_lock(self->lock); + PyMutex_Unlock(&self->mutex); Py_END_ALLOW_THREADS } else { update(self->hash_state, buf.buf, buf.len); @@ -269,17 +272,24 @@ static PyType_Spec md5_type_spec = { /*[clinic input] _md5.md5 - string: object(c_default="NULL") = b'' + data: object(c_default="NULL") = b'' * usedforsecurity: bool = True + string as string_obj: object(c_default="NULL") = None Return a new MD5 hash object; optionally initialized with a string. [clinic start generated code]*/ static PyObject * -_md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity) -/*[clinic end generated code: output=587071f76254a4ac input=7a144a1905636985]*/ +_md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity, + PyObject *string_obj) +/*[clinic end generated code: output=d45e187d3d16f3a8 input=7ea5c5366dbb44bf]*/ { + PyObject *string; + if (_Py_hashlib_data_argument(&string, data, string_obj) < 0) { + return NULL; + } + MD5object *new; Py_buffer buf; @@ -356,13 +366,7 @@ md5_exec(PyObject *m) st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &md5_type_spec, NULL); - if (st->md5_type == NULL) { - return -1; - } - - Py_INCREF((PyObject *)st->md5_type); - if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) { - Py_DECREF(st->md5_type); + if (PyModule_AddObjectRef(m, "MD5Type", (PyObject *)st->md5_type) < 0) { return -1; } @@ -372,6 +376,7 @@ md5_exec(PyObject *m) static PyModuleDef_Slot _md5_slots[] = { {Py_mod_exec, md5_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; |
