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.
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
)
41 for (i
= 0; conv
[i
].name
; i
++) {
42 switch (conv
[i
].type
) {
44 UNISTR
*u
= (UNISTR
*)((char *)s
+ conv
[i
].offset
);
50 item
= PyString_FromString(s
);
51 PyDict_SetItemString(obj
, conv
[i
].name
, item
);
56 uint32
*u
= (uint32
*)((char *)s
+ conv
[i
].offset
);
58 item
= PyInt_FromLong(*u
);
59 PyDict_SetItemString(obj
, conv
[i
].name
, item
);
64 uint16
*u
= (uint16
*)((char *)s
+ conv
[i
].offset
);
66 item
= PyInt_FromLong(*u
);
67 PyDict_SetItemString(obj
, conv
[i
].name
, item
);
79 /* Convert a Python dict to a structure */
81 BOOL
to_struct(void *s
, PyObject
*dict
, struct pyconv
*conv
)
83 PyObject
*visited
, *key
, *value
;
87 visited
= PyDict_New();
89 for (i
= 0; conv
[i
].name
; i
++) {
92 obj
= PyDict_GetItemString(dict
, conv
[i
].name
);
97 switch (conv
[i
].type
) {
99 UNISTR
*u
= (UNISTR
*)((char *)s
+ conv
[i
].offset
);
102 if (!PyString_Check(obj
))
105 s
= PyString_AsString(obj
);
111 uint32
*u
= (uint32
*)((char *)s
+ conv
[i
].offset
);
113 if (!PyInt_Check(obj
))
116 *u
= PyInt_AsLong(obj
);
121 uint16
*u
= (uint16
*)((char *)s
+ conv
[i
].offset
);
123 if (!PyInt_Check(obj
))
126 *u
= PyInt_AsLong(obj
);
133 /* Mark as visited */
135 PyDict_SetItemString(visited
, conv
[i
].name
,
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. */
145 while (PyDict_Next(dict
, &i
, &key
, &value
)) {
146 if (!PyDict_GetItem(visited
, key
))
153 /* We must decrement the reference count here or the visited
154 dictionary will not be freed. */