2019-06-24 14:59:51 -03:00
|
|
|
/*
|
|
|
|
* Python UUID module that wraps libuuid -
|
|
|
|
* DCE compatible Universally Unique Identifier library.
|
|
|
|
*/
|
|
|
|
|
2017-09-28 18:03:06 -03:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
|
|
|
#include "Python.h"
|
2017-12-30 17:39:20 -04:00
|
|
|
#ifdef HAVE_UUID_UUID_H
|
2017-09-28 18:03:06 -03:00
|
|
|
#include <uuid/uuid.h>
|
2019-06-24 14:59:51 -03:00
|
|
|
#elif defined(HAVE_UUID_H)
|
2017-12-30 17:39:20 -04:00
|
|
|
#include <uuid.h>
|
|
|
|
#endif
|
2017-09-28 18:03:06 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
py_uuid_generate_time_safe(PyObject *Py_UNUSED(context),
|
|
|
|
PyObject *Py_UNUSED(ignored))
|
2017-09-28 18:03:06 -03:00
|
|
|
{
|
2017-12-30 17:39:20 -04:00
|
|
|
uuid_t uuid;
|
2017-11-08 16:09:16 -04:00
|
|
|
#ifdef HAVE_UUID_GENERATE_TIME_SAFE
|
2017-09-28 18:03:06 -03:00
|
|
|
int res;
|
|
|
|
|
2017-12-30 17:39:20 -04:00
|
|
|
res = uuid_generate_time_safe(uuid);
|
|
|
|
return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
|
2018-05-24 19:45:09 -03:00
|
|
|
#elif defined(HAVE_UUID_CREATE)
|
2018-01-09 15:38:07 -04:00
|
|
|
uint32_t status;
|
2017-12-30 17:39:20 -04:00
|
|
|
uuid_create(&uuid, &status);
|
2018-05-24 19:45:09 -03:00
|
|
|
# if defined(HAVE_UUID_ENC_BE)
|
|
|
|
unsigned char buf[sizeof(uuid)];
|
|
|
|
uuid_enc_be(buf, &uuid);
|
|
|
|
return Py_BuildValue("y#i", buf, sizeof(uuid), (int) status);
|
|
|
|
# else
|
2017-12-30 17:39:20 -04:00
|
|
|
return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
|
2018-05-24 19:45:09 -03:00
|
|
|
# endif
|
2017-10-02 11:57:59 -03:00
|
|
|
#else
|
2017-12-30 17:39:20 -04:00
|
|
|
uuid_generate_time(uuid);
|
|
|
|
return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
|
2017-10-02 11:57:59 -03:00
|
|
|
#endif
|
2017-09-28 18:03:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef uuid_methods[] = {
|
2018-04-29 15:59:33 -03:00
|
|
|
{"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL},
|
2017-09-28 18:03:06 -03:00
|
|
|
{NULL, NULL, 0, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef uuidmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_uuid",
|
|
|
|
.m_size = -1,
|
|
|
|
.m_methods = uuid_methods,
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__uuid(void)
|
|
|
|
{
|
2017-10-02 11:57:59 -03:00
|
|
|
PyObject *mod;
|
2017-09-28 18:03:06 -03:00
|
|
|
assert(sizeof(uuid_t) == 16);
|
2017-11-08 16:09:16 -04:00
|
|
|
#ifdef HAVE_UUID_GENERATE_TIME_SAFE
|
2017-10-02 11:57:59 -03:00
|
|
|
int has_uuid_generate_time_safe = 1;
|
|
|
|
#else
|
|
|
|
int has_uuid_generate_time_safe = 0;
|
|
|
|
#endif
|
|
|
|
mod = PyModule_Create(&uuidmodule);
|
|
|
|
if (mod == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyModule_AddIntConstant(mod, "has_uuid_generate_time_safe",
|
|
|
|
has_uuid_generate_time_safe) < 0) {
|
2018-05-24 19:45:09 -03:00
|
|
|
Py_DECREF(mod);
|
2017-10-02 11:57:59 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod;
|
2017-09-28 18:03:06 -03:00
|
|
|
}
|