r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / python / py_spoolss_drivers_conv.c
index 41ff38327e297eccd28eac1f7bde5213909f3451..6d5ed4dc11fbbdc59302b0e8fa83c2bbcc8f5566 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "python/py_spoolss.h"
@@ -78,34 +77,56 @@ struct pyconv py_DRIVER_DIRECTORY_1[] = {
        { NULL }
 };
 
-/* Convert a NULL terminated list of NULL terminated unicode strings
-   to a list of (char *) strings */
-
-static PyObject *from_dependentfiles(uint16 *dependentfiles)
+static uint16 *to_dependentfiles(PyObject *list, TALLOC_CTX *mem_ctx)
 {
-       PyObject *list;
-       int offset = 0;
-
-       list = PyList_New(0);
+       uint32 elements, size=0, pos=0, i;
+       char *str;
+       uint16 *ret = NULL;
+       PyObject *borrowedRef;
 
-       while (*(dependentfiles + offset) != 0) {
-               fstring name;
-               int len;
+       if (!PyList_Check(list)) {
+               goto done;
+       }
 
-               len = rpcstr_pull(name, dependentfiles + offset,
-                                 sizeof(fstring), -1, STR_TERMINATE);
+       /* calculate size for dependentfiles */
+       elements=PyList_Size(list);
+       for (i = 0; i < elements; i++) {
+               borrowedRef=PyList_GetItem(list, i);
+               if (!PyString_Check(borrowedRef)) 
+                       /* non string found, return error */
+                       goto done;
+               size+=PyString_Size(borrowedRef)+1;
+       }
 
-               offset += len / 2;
-               PyList_Append(list, PyString_FromString(name));
+       if (!(ret = (uint16*)_talloc(mem_ctx,((size+1)*sizeof(uint16)))))
+               goto done;
+
+       /* create null terminated sequence of null terminated strings */
+       for (i = 0; i < elements; i++) {
+               borrowedRef=PyList_GetItem(list, i);
+               str=PyString_AsString(borrowedRef);
+               do {
+                       if (pos >= size) {
+                               /* dependentfiles too small.  miscalculated? */
+                               ret = NULL;
+                               goto done;
+                       }
+                       SSVAL(&ret[pos], 0, str[0]);
+                       pos++;
+               } while (*(str++));
        }
+       /* final null */
+       ret[pos]='\0';
 
-       return list;
+done:
+       return ret;     
 }
 
 BOOL py_from_DRIVER_INFO_1(PyObject **dict, DRIVER_INFO_1 *info)
 {
        *dict = from_struct(info, py_DRIVER_INFO_1);
        PyDict_SetItemString(*dict, "level", PyInt_FromLong(1));
+
        return True;
 }
 
@@ -118,6 +139,7 @@ BOOL py_from_DRIVER_INFO_2(PyObject **dict, DRIVER_INFO_2 *info)
 {
        *dict = from_struct(info, py_DRIVER_INFO_2);
        PyDict_SetItemString(*dict, "level", PyInt_FromLong(2));
+
        return True;
 }
 
@@ -129,22 +151,42 @@ BOOL py_to_DRIVER_INFO_2(DRIVER_INFO_2 *info, PyObject *dict)
 BOOL py_from_DRIVER_INFO_3(PyObject **dict, DRIVER_INFO_3 *info)
 {
        *dict = from_struct(info, py_DRIVER_INFO_3);
+
        PyDict_SetItemString(*dict, "level", PyInt_FromLong(3));
+
        PyDict_SetItemString(
                *dict, "dependent_files", 
-               from_dependentfiles(info->dependentfiles));
+               from_unistr_list(info->dependentfiles));
 
        return True;
 }
 
-BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict)
+BOOL py_to_DRIVER_INFO_3(DRIVER_INFO_3 *info, PyObject *dict,
+                        TALLOC_CTX *mem_ctx)
 {
-       PyObject *dict_copy = PyDict_Copy(dict);
-       BOOL result;
+       PyObject *obj, *dict_copy = PyDict_Copy(dict);
+       BOOL result = False;
+
+       if (!(obj = PyDict_GetItemString(dict_copy, "dependent_files")))
+               goto done;
+
+       if (!(info->dependentfiles = to_dependentfiles(obj, mem_ctx)))
+               goto done;
+
+       PyDict_DelItemString(dict_copy, "dependent_files");
+
+       if (!(obj = PyDict_GetItemString(dict_copy, "level")) ||
+           !PyInt_Check(obj))
+               goto done;
 
        PyDict_DelItemString(dict_copy, "level");
-       result = to_struct(info, dict_copy, py_DRIVER_INFO_3);
 
+       if (!to_struct(info, dict_copy, py_DRIVER_INFO_3))
+           goto done;
+
+       result = True;
+
+done:
        Py_DECREF(dict_copy);
        return result;
 }
@@ -155,7 +197,7 @@ BOOL py_from_DRIVER_INFO_6(PyObject **dict, DRIVER_INFO_6 *info)
        PyDict_SetItemString(*dict, "level", PyInt_FromLong(6));
        PyDict_SetItemString(
                *dict, "dependent_files", 
-               from_dependentfiles (info->dependentfiles));
+               from_unistr_list(info->dependentfiles));
        return True;
 }