source: trunk/server/source4/librpc/ndr/py_misc.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 4.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include <Python.h>
20#include "librpc/gen_ndr/misc.h"
21
22#ifndef Py_RETURN_NONE
23#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
24#endif
25
26static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
27{
28 int ret;
29 struct GUID *self = py_talloc_get_ptr(py_self), *other;
30 other = py_talloc_get_ptr(py_other);
31 if (other == NULL)
32 return -1;
33
34 ret = GUID_compare(self, other);
35 if (ret < 0) {
36 return -1;
37 } else if (ret > 0) {
38 return 1;
39 } else {
40 return 0;
41 }
42}
43
44static PyObject *py_GUID_str(PyObject *py_self)
45{
46 struct GUID *self = py_talloc_get_ptr(py_self);
47 char *str = GUID_string(NULL, self);
48 PyObject *ret = PyString_FromString(str);
49 talloc_free(str);
50 return ret;
51}
52
53static PyObject *py_GUID_repr(PyObject *py_self)
54{
55 struct GUID *self = py_talloc_get_ptr(py_self);
56 char *str = GUID_string(NULL, self);
57 PyObject *ret = PyString_FromFormat("GUID('%s')", str);
58 talloc_free(str);
59 return ret;
60}
61
62static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
63{
64 PyObject *str = NULL;
65 NTSTATUS status;
66 struct GUID *guid = py_talloc_get_ptr(self);
67 const char *kwnames[] = { "str", NULL };
68
69 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &str))
70 return -1;
71
72 if (str != NULL) {
73 DATA_BLOB guid_val;
74
75 if (!PyString_Check(str)) {
76 PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()");
77 return -1;
78 }
79 guid_val.data = (uint8_t *)PyString_AsString(str);
80 guid_val.length = PyString_Size(str);
81 status = GUID_from_data_blob(&guid_val, guid);
82 if (!NT_STATUS_IS_OK(status)) {
83 PyErr_SetNTSTATUS(status);
84 return -1;
85 }
86 }
87
88 return 0;
89}
90
91static void py_GUID_patch(PyTypeObject *type)
92{
93 type->tp_init = py_GUID_init;
94 type->tp_str = py_GUID_str;
95 type->tp_repr = py_GUID_repr;
96 type->tp_compare = py_GUID_cmp;
97}
98
99#define PY_GUID_PATCH py_GUID_patch
100
101static int py_policy_handle_init(PyObject *self, PyObject *args, PyObject *kwargs)
102{
103 char *str = NULL;
104 NTSTATUS status;
105 struct policy_handle *handle = py_talloc_get_ptr(self);
106 const char *kwnames[] = { "uuid", "type", NULL };
107
108 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", discard_const_p(char *, kwnames), &str, &handle->handle_type))
109 return -1;
110
111 if (str != NULL) {
112 status = GUID_from_string(str, &handle->uuid);
113 if (!NT_STATUS_IS_OK(status)) {
114 PyErr_SetNTSTATUS(status);
115 return -1;
116 }
117 }
118
119 return 0;
120}
121
122static PyObject *py_policy_handle_repr(PyObject *py_self)
123{
124 struct policy_handle *self = py_talloc_get_ptr(py_self);
125 char *uuid_str = GUID_string(NULL, &self->uuid);
126 PyObject *ret = PyString_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str);
127 talloc_free(uuid_str);
128 return ret;
129}
130
131static PyObject *py_policy_handle_str(PyObject *py_self)
132{
133 struct policy_handle *self = py_talloc_get_ptr(py_self);
134 char *uuid_str = GUID_string(NULL, &self->uuid);
135 PyObject *ret = PyString_FromFormat("%d, %s", self->handle_type, uuid_str);
136 talloc_free(uuid_str);
137 return ret;
138}
139
140static void py_policy_handle_patch(PyTypeObject *type)
141{
142 type->tp_init = py_policy_handle_init;
143 type->tp_repr = py_policy_handle_repr;
144 type->tp_str = py_policy_handle_str;
145}
146
147#define PY_POLICY_HANDLE_PATCH py_policy_handle_patch
148
Note: See TracBrowser for help on using the repository browser.