fixed typo
[Samba/gebeck_regimport.git] / source / python / py_conv.c
blob39b20ace8612a952cbb88b363b6dd20d98bd6b07
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 s = "";
47 if (u->buffer)
48 fstr_pull(s, u);
50 item = PyString_FromString(s);
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 default:
72 break;
76 return obj;
79 /* Convert a Python dict to a structure */
81 BOOL to_struct(void *s, PyObject *dict, struct pyconv *conv)
83 PyObject *visited, *key, *value;
84 BOOL result = False;
85 int i;
87 visited = PyDict_New();
89 for (i = 0; conv[i].name; i++) {
90 PyObject *obj;
92 obj = PyDict_GetItemString(dict, conv[i].name);
94 if (!obj)
95 goto done;
97 switch (conv[i].type) {
98 case PY_UNISTR: {
99 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
100 char *s = "";
102 if (!PyString_Check(obj))
103 goto done;
105 s = PyString_AsString(obj);
106 init_unistr(u, s);
108 break;
110 case PY_UINT32: {
111 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
113 if (!PyInt_Check(obj))
114 goto done;
116 *u = PyInt_AsLong(obj);
118 break;
120 case PY_UINT16: {
121 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
123 if (!PyInt_Check(obj))
124 goto done;
126 *u = PyInt_AsLong(obj);
127 break;
129 default:
130 break;
133 /* Mark as visited */
135 PyDict_SetItemString(visited, conv[i].name,
136 PyInt_FromLong(1));
139 /* Iterate over each item in the input dictionary and see if it was
140 visited. If it wasn't then the user has added some extra crap
141 to the dictionary. */
143 i = 0;
145 while (PyDict_Next(dict, &i, &key, &value)) {
146 if (!PyDict_GetItem(visited, key))
147 goto done;
150 result = True;
152 done:
153 /* We must decrement the reference count here or the visited
154 dictionary will not be freed. */
156 Py_DECREF(visited);
158 return result;