off by one bug in string length; CR 1159
[Samba.git] / source / python / py_conv.c
blobe865daf7d9c0661b9191cd5b5de18468165dbdef
1 /*
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "Python.h"
23 #include "py_conv.h"
25 /* Helper for rpcstr_pull() function */
27 static void fstr_pull(fstring str, UNISTR *uni)
29 rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
32 /* Convert a structure to a Python dict */
34 PyObject *from_struct(void *s, struct pyconv *conv)
36 PyObject *obj, *item;
37 int i;
39 obj = PyDict_New();
41 for (i = 0; conv[i].name; i++) {
42 switch (conv[i].type) {
43 case PY_UNISTR: {
44 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
45 fstring str = "";
47 if (u->buffer)
48 fstr_pull(str, u);
50 item = PyString_FromString(str);
51 PyDict_SetItemString(obj, conv[i].name, item);
53 break;
55 case PY_UINT32: {
56 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
58 item = PyInt_FromLong(*u);
59 PyDict_SetItemString(obj, conv[i].name, item);
61 break;
63 case PY_UINT16: {
64 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
66 item = PyInt_FromLong(*u);
67 PyDict_SetItemString(obj, conv[i].name, item);
69 break;
71 case PY_STRING: {
72 char *str = (char *)s + conv[i].offset;
74 item = PyString_FromString(str);
75 PyDict_SetItemString(obj, conv[i].name, item);
77 break;
79 case PY_UID: {
80 uid_t *uid = (uid_t *)((char *)s + conv[i].offset);
82 item = PyInt_FromLong(*uid);
83 PyDict_SetItemString(obj, conv[i].name, item);
85 break;
87 case PY_GID: {
88 gid_t *gid = (gid_t *)((char *)s + conv[i].offset);
90 item = PyInt_FromLong(*gid);
91 PyDict_SetItemString(obj, conv[i].name, item);
93 break;
95 default:
97 break;
101 return obj;
104 /* Convert a Python dict to a structure */
106 BOOL to_struct(void *s, PyObject *dict, struct pyconv *conv)
108 PyObject *visited, *key, *value;
109 BOOL result = False;
110 int i;
112 visited = PyDict_New();
114 for (i = 0; conv[i].name; i++) {
115 PyObject *obj;
117 obj = PyDict_GetItemString(dict, conv[i].name);
119 if (!obj)
120 goto done;
122 switch (conv[i].type) {
123 case PY_UNISTR: {
124 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
125 char *str = "";
127 if (!PyString_Check(obj))
128 goto done;
130 str = PyString_AsString(obj);
131 init_unistr(u, str);
133 break;
135 case PY_UINT32: {
136 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
138 if (!PyInt_Check(obj))
139 goto done;
141 *u = PyInt_AsLong(obj);
143 break;
145 case PY_UINT16: {
146 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
148 if (!PyInt_Check(obj))
149 goto done;
151 *u = PyInt_AsLong(obj);
152 break;
154 default:
155 break;
158 /* Mark as visited */
160 PyDict_SetItemString(visited, conv[i].name,
161 PyInt_FromLong(1));
164 /* Iterate over each item in the input dictionary and see if it was
165 visited. If it wasn't then the user has added some extra crap
166 to the dictionary. */
168 i = 0;
170 while (PyDict_Next(dict, &i, &key, &value)) {
171 if (!PyDict_GetItem(visited, key))
172 goto done;
175 result = True;
177 done:
178 /* We must decrement the reference count here or the visited
179 dictionary will not be freed. */
181 Py_DECREF(visited);
183 return result;
186 /* Convert a NULL terminated list of NULL terminated unicode strings
187 to a list of (char *) strings */
189 PyObject *from_unistr_list(uint16 *dependentfiles)
191 PyObject *list;
192 int offset = 0;
194 list = PyList_New(0);
196 while (*(dependentfiles + offset) != 0) {
197 fstring name;
198 int len;
200 len = rpcstr_pull(name, dependentfiles + offset,
201 sizeof(fstring), -1, STR_TERMINATE);
203 offset += len / 2;
204 PyList_Append(list, PyString_FromString(name));
207 return list;