HP2500C driver writes devmode with private data that ends
[kai/samba.git] / source3 / python / py_conv.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "Python.h"
23 #include "py_conv.h"
24
25 /* Helper for rpcstr_pull() function */
26
27 static void fstr_pull(fstring str, UNISTR *uni)
28 {
29         rpcstr_pull(str, uni->buffer, sizeof(fstring), 0, STR_TERMINATE);
30 }
31
32 /* Convert a structure to a Python dict */
33
34 PyObject *from_struct(void *s, struct pyconv *conv)
35 {
36         PyObject *obj, *item;
37         int i;
38
39         obj = PyDict_New();
40
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 = "";
46
47                         if (u->buffer)
48                                 fstr_pull(s, u);
49
50                         item = PyString_FromString(s);
51                         PyDict_SetItemString(obj, conv[i].name, item);
52
53                         break;
54                 }
55                 case PY_UINT32: {
56                         uint32 *u = (uint32 *)((char *)s + conv[i].offset);
57
58                         item = PyInt_FromLong(*u);
59                         PyDict_SetItemString(obj, conv[i].name, item);
60                         
61                         break;
62                 }
63                 case PY_UINT16: {
64                         uint16 *u = (uint16 *)((char *)s + conv[i].offset);
65
66                         item = PyInt_FromLong(*u);
67                         PyDict_SetItemString(obj, conv[i].name, item);
68
69                         break;
70                 }
71                 default:
72                         break;
73                 }
74         }
75
76         return obj;
77 }
78
79 /* Convert a Python dict to a structure */
80
81 void to_struct(void *s, PyObject *dict, struct pyconv *conv)
82 {
83         int i;
84
85         for (i = 0; conv[i].name; i++) {
86                 PyObject *obj;
87                 
88                 obj = PyDict_GetItemString(dict, conv[i].name);
89                 
90                 switch (conv[i].type) {
91                 case PY_UNISTR: {
92                         UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
93                         char *s = "";
94
95                         if (obj && PyString_Check(obj))
96                                 s = PyString_AsString(obj);
97
98                         init_unistr(u, s);
99                         
100                         break;
101                 }
102                 case PY_UINT32: {
103                         uint32 *u = (uint32 *)((char *)s + conv[i].offset);
104
105                         if (obj && PyInt_Check(obj)) 
106                                 *u = PyInt_AsLong(obj);
107                         else
108                                 *u = 0;
109
110                         break;
111                 }
112                 case PY_UINT16: {
113                         uint16 *u = (uint16 *)((char *)s + conv[i].offset);
114
115                         if (obj && PyInt_Check(obj)) 
116                                 *u = PyInt_AsLong(obj);
117                         else
118                                 *u = 0;
119
120                         break;
121                 }
122                 default:
123                         break;
124                 }
125         }
126 }