Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Samba utility functions
4 :
5 : Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 : #include <Python.h>
21 : #include "python/py3compat.h"
22 : #include "librpc/gen_ndr/misc.h"
23 :
24 : #if PY_MAJOR_VERSION >= 3
25 8697509 : static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op)
26 : {
27 : int ret;
28 8697509 : struct GUID *self = pytalloc_get_ptr(py_self), *other;
29 8697509 : other = pytalloc_get_ptr(py_other);
30 8697509 : if (other == NULL) {
31 0 : Py_INCREF(Py_NotImplemented);
32 0 : return Py_NotImplemented;
33 : }
34 :
35 8697509 : ret = GUID_compare(self, other);
36 :
37 8697509 : switch (op) {
38 4867 : case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
39 8692642 : case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
40 0 : case Py_LT: if (ret < 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
41 0 : case Py_GT: if (ret > 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
42 0 : case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
43 0 : case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
44 : }
45 0 : Py_INCREF(Py_NotImplemented);
46 0 : return Py_NotImplemented;
47 : }
48 : #else
49 : static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
50 : {
51 : int ret;
52 : struct GUID *self = pytalloc_get_ptr(py_self), *other;
53 : other = pytalloc_get_ptr(py_other);
54 : if (other == NULL)
55 : return -1;
56 :
57 : ret = GUID_compare(self, other);
58 : if (ret < 0) {
59 : return -1;
60 : } else if (ret > 0) {
61 : return 1;
62 : } else {
63 : return 0;
64 : }
65 : }
66 : #endif
67 :
68 740381 : static PyObject *py_GUID_str(PyObject *py_self)
69 : {
70 740381 : struct GUID *self = pytalloc_get_ptr(py_self);
71 : struct GUID_txt_buf buf;
72 740381 : PyObject *ret = PyUnicode_FromString(GUID_buf_string(self, &buf));
73 740381 : return ret;
74 : }
75 :
76 2716 : static PyObject *py_GUID_repr(PyObject *py_self)
77 : {
78 2716 : struct GUID *self = pytalloc_get_ptr(py_self);
79 : struct GUID_txt_buf buf;
80 2716 : PyObject *ret = PyUnicode_FromFormat(
81 : "GUID('%s')", GUID_buf_string(self, &buf));
82 2716 : return ret;
83 : }
84 :
85 9081094 : static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
86 : {
87 9081094 : PyObject *str = NULL;
88 : NTSTATUS status;
89 9081094 : struct GUID *guid = pytalloc_get_ptr(self);
90 9081094 : const char *kwnames[] = { "str", NULL };
91 :
92 9081094 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &str))
93 0 : return -1;
94 :
95 9081094 : if (str != NULL) {
96 : DATA_BLOB guid_val;
97 : Py_ssize_t _size;
98 :
99 9077921 : if (PyUnicode_Check(str)) {
100 8701750 : guid_val.data =
101 8701750 : discard_const_p(uint8_t,
102 : PyUnicode_AsUTF8AndSize(str, &_size));
103 376171 : } else if (PyBytes_Check(str)) {
104 376171 : guid_val.data =
105 376171 : discard_const_p(uint8_t,
106 : PyBytes_AsString(str));
107 376171 : _size = PyBytes_Size(str);
108 : } else {
109 0 : PyErr_SetString(PyExc_TypeError,
110 : "Expected a string or bytes argument to GUID()");
111 0 : return -1;
112 : }
113 9077921 : guid_val.length = _size;
114 9077921 : status = GUID_from_data_blob(&guid_val, guid);
115 9077921 : if (!NT_STATUS_IS_OK(status)) {
116 0 : PyErr_SetNTSTATUS(status);
117 0 : return -1;
118 : }
119 : }
120 :
121 9081094 : return 0;
122 : }
123 :
124 3580 : static void py_GUID_patch(PyTypeObject *type)
125 : {
126 3580 : type->tp_init = py_GUID_init;
127 3580 : type->tp_str = py_GUID_str;
128 3580 : type->tp_repr = py_GUID_repr;
129 : #if PY_MAJOR_VERSION >= 3
130 3580 : type->tp_richcompare = py_GUID_richcmp;
131 : #else
132 : type->tp_compare = py_GUID_cmp;
133 : #endif
134 3580 : }
135 :
136 : #define PY_GUID_PATCH py_GUID_patch
137 :
138 50 : static int py_policy_handle_init(PyObject *self, PyObject *args, PyObject *kwargs)
139 : {
140 50 : char *str = NULL;
141 : NTSTATUS status;
142 50 : struct policy_handle *handle = pytalloc_get_ptr(self);
143 50 : const char *kwnames[] = { "uuid", "type", NULL };
144 :
145 50 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sI", discard_const_p(char *, kwnames), &str, &handle->handle_type))
146 0 : return -1;
147 :
148 50 : if (str != NULL) {
149 0 : status = GUID_from_string(str, &handle->uuid);
150 0 : if (!NT_STATUS_IS_OK(status)) {
151 0 : PyErr_SetNTSTATUS(status);
152 0 : return -1;
153 : }
154 : }
155 :
156 50 : return 0;
157 : }
158 :
159 0 : static PyObject *py_policy_handle_repr(PyObject *py_self)
160 : {
161 0 : struct policy_handle *self = pytalloc_get_ptr(py_self);
162 : struct GUID_txt_buf buf;
163 0 : PyObject *ret = PyUnicode_FromFormat(
164 : "policy_handle(%d, '%s')",
165 : self->handle_type,
166 0 : GUID_buf_string(&self->uuid, &buf));
167 0 : return ret;
168 : }
169 :
170 0 : static PyObject *py_policy_handle_str(PyObject *py_self)
171 : {
172 0 : struct policy_handle *self = pytalloc_get_ptr(py_self);
173 : struct GUID_txt_buf buf;
174 0 : PyObject *ret = PyUnicode_FromFormat(
175 : "%d, %s",
176 : self->handle_type,
177 0 : GUID_buf_string(&self->uuid, &buf));
178 0 : return ret;
179 : }
180 :
181 3580 : static void py_policy_handle_patch(PyTypeObject *type)
182 : {
183 3580 : type->tp_init = py_policy_handle_init;
184 3580 : type->tp_repr = py_policy_handle_repr;
185 3580 : type->tp_str = py_policy_handle_str;
186 3580 : }
187 :
188 : #define PY_POLICY_HANDLE_PATCH py_policy_handle_patch
189 :
|