ziheng | 6ffd9b0 | 2019-06-25 01:59:51 +0800 | [diff] [blame] | 1 | /* |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 2 | * Python UUID module that wraps libuuid or Windows rpcrt4.dll. |
ziheng | 6ffd9b0 | 2019-06-25 01:59:51 +0800 | [diff] [blame] | 3 | * DCE compatible Universally Unique Identifier library. |
| 4 | */ |
| 5 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
| 7 | |
| 8 | #include "Python.h" |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 9 | #ifdef HAVE_UUID_UUID_H |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 10 | #include <uuid/uuid.h> |
ziheng | 6ffd9b0 | 2019-06-25 01:59:51 +0800 | [diff] [blame] | 11 | #elif defined(HAVE_UUID_H) |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 12 | #include <uuid.h> |
| 13 | #endif |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 14 | |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 15 | #ifdef MS_WINDOWS |
| 16 | #include <rpc.h> |
| 17 | #endif |
| 18 | |
| 19 | #ifndef MS_WINDOWS |
| 20 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 21 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 22 | py_uuid_generate_time_safe(PyObject *Py_UNUSED(context), |
| 23 | PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 24 | { |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 25 | uuid_t uuid; |
Berker Peksag | 9a10ff4 | 2017-11-08 23:09:16 +0300 | [diff] [blame] | 26 | #ifdef HAVE_UUID_GENERATE_TIME_SAFE |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 27 | int res; |
| 28 | |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 29 | res = uuid_generate_time_safe(uuid); |
| 30 | return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res); |
Serhiy Storchaka | 17d8830 | 2018-05-25 01:45:09 +0300 | [diff] [blame] | 31 | #elif defined(HAVE_UUID_CREATE) |
David Carlier | b4ebaa7 | 2018-01-09 19:38:07 +0000 | [diff] [blame] | 32 | uint32_t status; |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 33 | uuid_create(&uuid, &status); |
Serhiy Storchaka | 17d8830 | 2018-05-25 01:45:09 +0300 | [diff] [blame] | 34 | # if defined(HAVE_UUID_ENC_BE) |
| 35 | unsigned char buf[sizeof(uuid)]; |
| 36 | uuid_enc_be(buf, &uuid); |
| 37 | return Py_BuildValue("y#i", buf, sizeof(uuid), (int) status); |
| 38 | # else |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 39 | return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status); |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 40 | # endif /* HAVE_UUID_CREATE */ |
| 41 | #else /* HAVE_UUID_GENERATE_TIME_SAFE */ |
Michael Felt | 0d3ccb4 | 2017-12-30 22:39:20 +0100 | [diff] [blame] | 42 | uuid_generate_time(uuid); |
| 43 | return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None); |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 44 | #endif /* HAVE_UUID_GENERATE_TIME_SAFE */ |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 47 | #else /* MS_WINDOWS */ |
| 48 | |
| 49 | static PyObject * |
| 50 | py_UuidCreate(PyObject *Py_UNUSED(context), |
| 51 | PyObject *Py_UNUSED(ignored)) |
| 52 | { |
| 53 | UUID uuid; |
| 54 | RPC_STATUS res; |
| 55 | |
| 56 | Py_BEGIN_ALLOW_THREADS |
| 57 | res = UuidCreateSequential(&uuid); |
| 58 | Py_END_ALLOW_THREADS |
| 59 | |
| 60 | switch (res) { |
| 61 | case RPC_S_OK: |
| 62 | case RPC_S_UUID_LOCAL_ONLY: |
| 63 | case RPC_S_UUID_NO_ADDRESS: |
| 64 | /* |
| 65 | All success codes, but the latter two indicate that the UUID is random |
| 66 | rather than based on the MAC address. If the OS can't figure this out, |
| 67 | neither can we, so we'll take it anyway. |
| 68 | */ |
| 69 | return Py_BuildValue("y#", (const char *)&uuid, sizeof(uuid)); |
| 70 | } |
| 71 | PyErr_SetFromWindowsErr(res); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | #endif /* MS_WINDOWS */ |
| 76 | |
| 77 | |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 78 | static int |
| 79 | uuid_exec(PyObject *module) { |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 80 | assert(sizeof(uuid_t) == 16); |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 81 | #if defined(MS_WINDOWS) |
| 82 | int has_uuid_generate_time_safe = 0; |
| 83 | #elif defined(HAVE_UUID_GENERATE_TIME_SAFE) |
Victor Stinner | 4337a0d | 2017-10-02 07:57:59 -0700 | [diff] [blame] | 84 | int has_uuid_generate_time_safe = 1; |
| 85 | #else |
| 86 | int has_uuid_generate_time_safe = 0; |
| 87 | #endif |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 88 | if (PyModule_AddIntConstant(module, "has_uuid_generate_time_safe", |
Victor Stinner | 4337a0d | 2017-10-02 07:57:59 -0700 | [diff] [blame] | 89 | has_uuid_generate_time_safe) < 0) { |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 90 | return -1; |
Victor Stinner | 4337a0d | 2017-10-02 07:57:59 -0700 | [diff] [blame] | 91 | } |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 92 | return 0; |
| 93 | } |
Victor Stinner | 4337a0d | 2017-10-02 07:57:59 -0700 | [diff] [blame] | 94 | |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 95 | static PyMethodDef uuid_methods[] = { |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 96 | #if defined(HAVE_UUID_UUID_H) || defined(HAVE_UUID_H) |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 97 | {"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL}, |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 98 | #endif |
| 99 | #if defined(MS_WINDOWS) |
| 100 | {"UuidCreate", py_UuidCreate, METH_NOARGS, NULL}, |
| 101 | #endif |
Dong-hee Na | 1cb763b | 2020-03-31 21:43:47 +0900 | [diff] [blame] | 102 | {NULL, NULL, 0, NULL} /* sentinel */ |
| 103 | }; |
| 104 | |
| 105 | static PyModuleDef_Slot uuid_slots[] = { |
| 106 | {Py_mod_exec, uuid_exec}, |
| 107 | {0, NULL} |
| 108 | }; |
| 109 | |
| 110 | static struct PyModuleDef uuidmodule = { |
| 111 | PyModuleDef_HEAD_INIT, |
| 112 | .m_name = "_uuid", |
| 113 | .m_size = 0, |
| 114 | .m_methods = uuid_methods, |
| 115 | .m_slots = uuid_slots, |
| 116 | }; |
| 117 | |
| 118 | PyMODINIT_FUNC |
| 119 | PyInit__uuid(void) |
| 120 | { |
| 121 | return PyModuleDef_Init(&uuidmodule); |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 122 | } |