2019-06-24 17:59:51 +00:00
|
|
|
/*
|
2020-05-12 22:32:32 +00:00
|
|
|
* Python UUID module that wraps libuuid or Windows rpcrt4.dll.
|
2019-06-24 17:59:51 +00:00
|
|
|
* DCE compatible Universally Unique Identifier library.
|
|
|
|
*/
|
|
|
|
|
2017-09-28 21:03:06 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
|
|
|
#include "Python.h"
|
2017-12-30 21:39:20 +00:00
|
|
|
#ifdef HAVE_UUID_UUID_H
|
2017-09-28 21:03:06 +00:00
|
|
|
#include <uuid/uuid.h>
|
2019-06-24 17:59:51 +00:00
|
|
|
#elif defined(HAVE_UUID_H)
|
2017-12-30 21:39:20 +00:00
|
|
|
#include <uuid.h>
|
|
|
|
#endif
|
2017-09-28 21:03:06 +00:00
|
|
|
|
2020-05-12 22:32:32 +00:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#include <rpc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
|
2017-09-28 21:03:06 +00:00
|
|
|
static PyObject *
|
2018-04-29 18:59:33 +00:00
|
|
|
py_uuid_generate_time_safe(PyObject *Py_UNUSED(context),
|
|
|
|
PyObject *Py_UNUSED(ignored))
|
2017-09-28 21:03:06 +00:00
|
|
|
{
|
2017-12-30 21:39:20 +00:00
|
|
|
uuid_t uuid;
|
2017-11-08 20:09:16 +00:00
|
|
|
#ifdef HAVE_UUID_GENERATE_TIME_SAFE
|
2017-09-28 21:03:06 +00:00
|
|
|
int res;
|
|
|
|
|
2017-12-30 21:39:20 +00:00
|
|
|
res = uuid_generate_time_safe(uuid);
|
|
|
|
return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
|
2018-05-24 22:45:09 +00:00
|
|
|
#elif defined(HAVE_UUID_CREATE)
|
2018-01-09 19:38:07 +00:00
|
|
|
uint32_t status;
|
2017-12-30 21:39:20 +00:00
|
|
|
uuid_create(&uuid, &status);
|
2018-05-24 22:45:09 +00: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 21:39:20 +00:00
|
|
|
return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
|
2020-05-12 22:32:32 +00:00
|
|
|
# endif /* HAVE_UUID_CREATE */
|
|
|
|
#else /* HAVE_UUID_GENERATE_TIME_SAFE */
|
2017-12-30 21:39:20 +00:00
|
|
|
uuid_generate_time(uuid);
|
|
|
|
return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
|
2020-05-12 22:32:32 +00:00
|
|
|
#endif /* HAVE_UUID_GENERATE_TIME_SAFE */
|
2017-09-28 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:32:32 +00:00
|
|
|
#else /* MS_WINDOWS */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
py_UuidCreate(PyObject *Py_UNUSED(context),
|
|
|
|
PyObject *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
UUID uuid;
|
|
|
|
RPC_STATUS res;
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
res = UuidCreateSequential(&uuid);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
case RPC_S_OK:
|
|
|
|
case RPC_S_UUID_LOCAL_ONLY:
|
|
|
|
case RPC_S_UUID_NO_ADDRESS:
|
|
|
|
/*
|
|
|
|
All success codes, but the latter two indicate that the UUID is random
|
|
|
|
rather than based on the MAC address. If the OS can't figure this out,
|
|
|
|
neither can we, so we'll take it anyway.
|
|
|
|
*/
|
|
|
|
return Py_BuildValue("y#", (const char *)&uuid, sizeof(uuid));
|
|
|
|
}
|
|
|
|
PyErr_SetFromWindowsErr(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MS_WINDOWS */
|
|
|
|
|
|
|
|
|
2020-03-31 12:43:47 +00:00
|
|
|
static int
|
|
|
|
uuid_exec(PyObject *module) {
|
|
|
|
assert(sizeof(uuid_t) == 16);
|
2020-05-12 22:32:32 +00:00
|
|
|
#if defined(MS_WINDOWS)
|
|
|
|
int has_uuid_generate_time_safe = 0;
|
|
|
|
#elif defined(HAVE_UUID_GENERATE_TIME_SAFE)
|
2020-03-31 12:43:47 +00:00
|
|
|
int has_uuid_generate_time_safe = 1;
|
|
|
|
#else
|
|
|
|
int has_uuid_generate_time_safe = 0;
|
|
|
|
#endif
|
|
|
|
if (PyModule_AddIntConstant(module, "has_uuid_generate_time_safe",
|
|
|
|
has_uuid_generate_time_safe) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-28 21:03:06 +00:00
|
|
|
|
|
|
|
static PyMethodDef uuid_methods[] = {
|
2020-05-12 22:32:32 +00:00
|
|
|
#if defined(HAVE_UUID_UUID_H) || defined(HAVE_UUID_H)
|
2018-04-29 18:59:33 +00:00
|
|
|
{"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL},
|
2020-05-12 22:32:32 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MS_WINDOWS)
|
|
|
|
{"UuidCreate", py_UuidCreate, METH_NOARGS, NULL},
|
|
|
|
#endif
|
2017-09-28 21:03:06 +00:00
|
|
|
{NULL, NULL, 0, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2020-03-31 12:43:47 +00:00
|
|
|
static PyModuleDef_Slot uuid_slots[] = {
|
|
|
|
{Py_mod_exec, uuid_exec},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
2017-09-28 21:03:06 +00:00
|
|
|
static struct PyModuleDef uuidmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_uuid",
|
2020-03-31 12:43:47 +00:00
|
|
|
.m_size = 0,
|
2017-09-28 21:03:06 +00:00
|
|
|
.m_methods = uuid_methods,
|
2020-03-31 12:43:47 +00:00
|
|
|
.m_slots = uuid_slots,
|
2017-09-28 21:03:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__uuid(void)
|
|
|
|
{
|
2020-03-31 12:43:47 +00:00
|
|
|
return PyModuleDef_Init(&uuidmodule);
|
2017-09-28 21:03:06 +00:00
|
|
|
}
|