r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[jra/samba/.git] / source3 / python / py_spoolss_drivers_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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "python/py_spoolss.h"
21 #include "python/py_conv.h"
22
23 /* Structure/hash conversions */
24
25 struct pyconv py_DRIVER_INFO_1[] = {
26         { "name", PY_UNISTR, offsetof(DRIVER_INFO_1, name) },
27         { NULL }
28 };
29
30 struct pyconv py_DRIVER_INFO_2[] = {
31         { "version", PY_UINT32, offsetof(DRIVER_INFO_2, version) },
32         { "name", PY_UNISTR, offsetof(DRIVER_INFO_2, name) },
33         { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_2, architecture) },
34         { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_2, driverpath) },
35         { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_2, datafile) },
36         { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_2, configfile) },
37         { NULL }
38 };
39
40 struct pyconv py_DRIVER_INFO_3[] = {
41         { "version", PY_UINT32, offsetof(DRIVER_INFO_3, version) },
42         { "name", PY_UNISTR, offsetof(DRIVER_INFO_3, name) },
43         { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_3, architecture) },
44         { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_3, driverpath) },
45         { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_3, datafile) },
46         { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_3, configfile) },
47         { "help_file", PY_UNISTR, offsetof(DRIVER_INFO_3, helpfile) },
48         { "monitor_name", PY_UNISTR, offsetof(DRIVER_INFO_3, monitorname) },
49         { "default_datatype", PY_UNISTR, offsetof(DRIVER_INFO_3, defaultdatatype) },
50         { NULL }
51 };
52
53 struct pyconv py_DRIVER_INFO_6[] = {
54         { "version", PY_UINT32, offsetof(DRIVER_INFO_6, version) },
55         { "name", PY_UNISTR, offsetof(DRIVER_INFO_6, name) },
56         { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_6, architecture) },
57         { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_6, driverpath) },
58         { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_6, datafile) },
59         { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_6, configfile) },
60         { "help_file", PY_UNISTR, offsetof(DRIVER_INFO_6, helpfile) },
61         { "monitor_name", PY_UNISTR, offsetof(DRIVER_INFO_6, monitorname) },
62         { "default_datatype", PY_UNISTR, offsetof(DRIVER_INFO_6, defaultdatatype) },
63         /* driver_date */
64         { "padding", PY_UINT32, offsetof(DRIVER_INFO_6, padding) },
65         { "driver_version_low", PY_UINT32, offsetof(DRIVER_INFO_6, driver_version_low) },
66         { "driver_version_high", PY_UINT32, offsetof(DRIVER_INFO_6, driver_version_high) },
67         { "mfg_name", PY_UNISTR, offsetof(DRIVER_INFO_6, mfgname) },
68         { "oem_url", PY_UNISTR, offsetof(DRIVER_INFO_6, oem_url) },
69         { "hardware_id", PY_UNISTR, offsetof(DRIVER_INFO_6, hardware_id) },
70         { "provider", PY_UNISTR, offsetof(DRIVER_INFO_6, provider) },
71         
72         { NULL }
73 };
74
75 struct pyconv py_DRIVER_DIRECTORY_1[] = {
76         { "name", PY_UNISTR, offsetof(DRIVER_DIRECTORY_1, name) },
77         { NULL }
78 };
79
80 static uint16 *to_dependentfiles(PyObject *list, TALLOC_CTX *mem_ctx)
81 {
82         uint32 elements, size=0, pos=0, i;
83         char *str;
84         uint16 *ret = NULL;
85         PyObject *borrowedRef;
86
87         if (!PyList_Check(list)) {
88                 goto done;
89         }
90
91         /* calculate size for dependentfiles */
92         elements=PyList_Size(list);
93         for (i = 0; i < elements; i++) {
94                 borrowedRef=PyList_GetItem(list, i);
95                 if (!PyString_Check(borrowedRef)) 
96                         /* non string found, return error */
97                         goto done;
98                 size+=PyString_Size(borrowedRef)+1;
99         }
100
101         if (!(ret = (uint16*)_talloc(mem_ctx,((size+1)*sizeof(uint16)))))
102                 goto done;
103
104         /* create null terminated sequence of null terminated strings */
105         for (i = 0; i < elements; i++) {
106                 borrowedRef=PyList_GetItem(list, i);
107                 str=PyString_AsString(borrowedRef);
108                 do {
109                         if (pos >= size) {
110                                 /* dependentfiles too small.  miscalculated? */
111                                 ret = NULL;
112                                 goto done;
113                         }
114                         SSVAL(&ret[pos], 0, str[0]);
115                         pos++;
116                 } while (*(str++));
117         }
118         /* final null */
119         ret[pos]='\0';
120
121 done:
122         return ret;     
123 }
124
125 BOOL py_from_DRIVER_INFO_1(PyObject **dict, DRIVER_INFO_1 *info)
126 {
127         *dict = from_struct(info, py_DRIVER_INFO_1);
128         PyDict_SetItemString(*dict, "level", PyInt_FromLong(1));
129
130         return True;
131 }
132
133 BOOL py_to_DRIVER_INFO_1(DRIVER_INFO_1 *info, PyObject *dict)
134 {
135         return False;
136 }
137
138 BOOL py_from_DRIVER_INFO_2(PyObject **dict, DRIVER_INFO_2 *info)
139 {
140         *dict = from_struct(info, py_DRIVER_INFO_2);
141         PyDict_SetItemString(*dict, "level", PyInt_FromLong(2));
142
143         return True;
144 }
145
146 BOOL py_to_DRIVER_INFO_2(DRIVER_INFO_2 *info, PyObject *dict)
147 {
148         return False;
149 }
150
151 BOOL py_from_DRIVER_INFO_3(PyObject **dict, DRIVER_INFO_3 *info)
152 {
153         *dict = from_struct(info, py_DRIVER_INFO_3);
154
155         PyDict_SetItemString(*dict, "level", PyInt_FromLong(3));
156
157         PyDict_SetItemString(
158                 *dict, "dependent_files", 
159                 from_unistr_list(info->dependentfiles));
160
161         return True;
162 }
163
164 BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict,
165                          TALLOC_CTX *mem_ctx)
166 {
167         PyObject *obj, *dict_copy = PyDict_Copy(dict);
168         BOOL result = False;
169
170         if (!(obj = PyDict_GetItemString(dict_copy, "dependent_files")))
171                 goto done;
172
173         if (!(info->dependentfiles = to_dependentfiles(obj, mem_ctx)))
174                 goto done;
175
176         PyDict_DelItemString(dict_copy, "dependent_files");
177
178         if (!(obj = PyDict_GetItemString(dict_copy, "level")) ||
179             !PyInt_Check(obj))
180                 goto done;
181
182         PyDict_DelItemString(dict_copy, "level");
183
184         if (!to_struct(info, dict_copy, py_DRIVER_INFO_3))
185             goto done;
186
187         result = True;
188
189 done:
190         Py_DECREF(dict_copy);
191         return result;
192 }
193
194 BOOL py_from_DRIVER_INFO_6(PyObject **dict, DRIVER_INFO_6 *info)
195 {
196         *dict = from_struct(info, py_DRIVER_INFO_6);
197         PyDict_SetItemString(*dict, "level", PyInt_FromLong(6));
198         PyDict_SetItemString(
199                 *dict, "dependent_files", 
200                 from_unistr_list(info->dependentfiles));
201         return True;
202 }
203
204 BOOL py_to_DRIVER_INFO_6(DRIVER_INFO_6 *info, PyObject *dict)
205 {
206         return False;
207 }
208
209 BOOL py_from_DRIVER_DIRECTORY_1(PyObject **dict, DRIVER_DIRECTORY_1 *info)
210 {
211         *dict = from_struct(info, py_DRIVER_DIRECTORY_1);
212         PyDict_SetItemString(*dict, "level", PyInt_FromLong(1));
213         return True;
214 }
215
216 BOOL py_to_DRIVER_DIRECTORY_1(DRIVER_DIRECTORY_1 *info, PyObject *dict)
217 {
218         return False;
219 }