2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
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.
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.
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/>.
22 /* Helper for rpcstr_pull() function */
24 static void fstr_pull(fstring str, UNISTR *uni)
26 rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
29 static void fstr_pull2(fstring str, UNISTR2 *uni)
31 rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
34 /* Convert a structure to a Python dict */
36 PyObject *from_struct(void *s, struct pyconv *conv)
43 for (i = 0; conv[i].name; i++) {
44 switch (conv[i].type) {
46 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
52 item = PyString_FromString(str);
53 PyDict_SetItemString(obj, conv[i].name, item);
58 UNISTR2 *u = (UNISTR2 *)((char *)s + conv[i].offset);
64 item = PyString_FromString(str);
65 PyDict_SetItemString(obj, conv[i].name, item);
70 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
72 item = PyInt_FromLong(*u);
73 PyDict_SetItemString(obj, conv[i].name, item);
78 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
80 item = PyInt_FromLong(*u);
81 PyDict_SetItemString(obj, conv[i].name, item);
86 char *str = (char *)s + conv[i].offset;
88 item = PyString_FromString(str);
89 PyDict_SetItemString(obj, conv[i].name, item);
94 uid_t *uid = (uid_t *)((char *)s + conv[i].offset);
96 item = PyInt_FromLong(*uid);
97 PyDict_SetItemString(obj, conv[i].name, item);
102 gid_t *gid = (gid_t *)((char *)s + conv[i].offset);
104 item = PyInt_FromLong(*gid);
105 PyDict_SetItemString(obj, conv[i].name, item);
118 /* Convert a Python dict to a structure */
120 BOOL to_struct(void *s, PyObject *dict, struct pyconv *conv)
122 PyObject *visited, *key, *value;
126 visited = PyDict_New();
128 for (i = 0; conv[i].name; i++) {
131 obj = PyDict_GetItemString(dict, conv[i].name);
136 switch (conv[i].type) {
138 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
141 if (!PyString_Check(obj))
144 str = PyString_AsString(obj);
150 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
152 if (!PyInt_Check(obj))
155 *u = PyInt_AsLong(obj);
160 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
162 if (!PyInt_Check(obj))
165 *u = PyInt_AsLong(obj);
172 /* Mark as visited */
174 PyDict_SetItemString(visited, conv[i].name,
178 /* Iterate over each item in the input dictionary and see if it was
179 visited. If it wasn't then the user has added some extra crap
180 to the dictionary. */
184 while (PyDict_Next(dict, &i, &key, &value)) {
185 if (!PyDict_GetItem(visited, key))
192 /* We must decrement the reference count here or the visited
193 dictionary will not be freed. */
200 /* Convert a NULL terminated list of NULL terminated unicode strings
201 to a list of (char *) strings */
203 PyObject *from_unistr_list(uint16 *dependentfiles)
208 list = PyList_New(0);
210 while (*(dependentfiles + offset) != 0) {
214 len = rpcstr_pull(name, dependentfiles + offset,
215 sizeof(fstring), -1, STR_TERMINATE);
218 PyList_Append(list, PyString_FromString(name));