python/py_conv.c

説明を見る。
00001 /* 
00002    Python wrappers for DCERPC/SMB client routines.
00003 
00004    Copyright (C) Tim Potter, 2002
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "py_conv.h"
00022 
00023 /* Helper for rpcstr_pull() function */
00024 
00025 static void fstr_pull(fstring str, UNISTR *uni)
00026 {
00027         rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
00028 }
00029 
00030 static void fstr_pull2(fstring str, UNISTR2 *uni)
00031 {
00032         rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
00033 }
00034 
00035 /* Convert a structure to a Python dict */
00036 
00037 PyObject *from_struct(void *s, struct pyconv *conv)
00038 {
00039         PyObject *obj, *item;
00040         int i;
00041 
00042         obj = PyDict_New();
00043 
00044         for (i = 0; conv[i].name; i++) {
00045                 switch (conv[i].type) {
00046                 case PY_UNISTR: {
00047                         UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
00048                         fstring str = "";
00049 
00050                         if (u->buffer)
00051                                 fstr_pull(str, u);
00052 
00053                         item = PyString_FromString(str);
00054                         PyDict_SetItemString(obj, conv[i].name, item);
00055 
00056                         break;
00057                 }
00058                 case PY_UNISTR2: {
00059                         UNISTR2 *u = (UNISTR2 *)((char *)s + conv[i].offset);
00060                         fstring str = "";
00061 
00062                         if (u->buffer)
00063                                 fstr_pull2(str, u);
00064 
00065                         item = PyString_FromString(str);
00066                         PyDict_SetItemString(obj, conv[i].name, item);
00067 
00068                         break;
00069                 }
00070                 case PY_UINT32: {
00071                         uint32 *u = (uint32 *)((char *)s + conv[i].offset);
00072 
00073                         item = PyInt_FromLong(*u);
00074                         PyDict_SetItemString(obj, conv[i].name, item);
00075                         
00076                         break;
00077                 }
00078                 case PY_UINT16: {
00079                         uint16 *u = (uint16 *)((char *)s + conv[i].offset);
00080 
00081                         item = PyInt_FromLong(*u);
00082                         PyDict_SetItemString(obj, conv[i].name, item);
00083 
00084                         break;
00085                 }
00086                 case PY_STRING: {
00087                         char *str = (char *)s + conv[i].offset;
00088 
00089                         item = PyString_FromString(str);
00090                         PyDict_SetItemString(obj, conv[i].name, item);
00091 
00092                         break;
00093                 }
00094                 case PY_UID: {
00095                         uid_t *uid = (uid_t *)((char *)s + conv[i].offset);
00096 
00097                         item = PyInt_FromLong(*uid);
00098                         PyDict_SetItemString(obj, conv[i].name, item);
00099 
00100                         break;
00101                 }
00102                 case PY_GID: {
00103                         gid_t *gid = (gid_t *)((char *)s + conv[i].offset);
00104 
00105                         item = PyInt_FromLong(*gid);
00106                         PyDict_SetItemString(obj, conv[i].name, item);
00107 
00108                         break;
00109                 }
00110                 default:
00111                         
00112                         break;
00113                 }
00114         }
00115 
00116         return obj;
00117 }
00118 
00119 /* Convert a Python dict to a structure */
00120 
00121 BOOL to_struct(void *s, PyObject *dict, struct pyconv *conv)
00122 {
00123         PyObject *visited, *key, *value;
00124         BOOL result = False;
00125         int i;
00126 
00127         visited = PyDict_New();
00128 
00129         for (i = 0; conv[i].name; i++) {
00130                 PyObject *obj;
00131                 
00132                 obj = PyDict_GetItemString(dict, conv[i].name);
00133 
00134                 if (!obj)
00135                         goto done;
00136                 
00137                 switch (conv[i].type) {
00138                 case PY_UNISTR: {
00139                         UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
00140                         char *str = "";
00141 
00142                         if (!PyString_Check(obj))
00143                                 goto done;
00144 
00145                         str = PyString_AsString(obj);
00146                         init_unistr(u, str);
00147                         
00148                         break;
00149                 }
00150                 case PY_UINT32: {
00151                         uint32 *u = (uint32 *)((char *)s + conv[i].offset);
00152 
00153                         if (!PyInt_Check(obj))
00154                                 goto done;
00155 
00156                         *u = PyInt_AsLong(obj);
00157 
00158                         break;
00159                 }
00160                 case PY_UINT16: {
00161                         uint16 *u = (uint16 *)((char *)s + conv[i].offset);
00162 
00163                         if (!PyInt_Check(obj)) 
00164                                 goto done;
00165 
00166                         *u = PyInt_AsLong(obj);
00167                         break;
00168                 }
00169                 default:
00170                         break;
00171                 }
00172 
00173                 /* Mark as visited */
00174 
00175                 PyDict_SetItemString(visited, conv[i].name, 
00176                                      PyInt_FromLong(1));
00177         }
00178 
00179         /* Iterate over each item in the input dictionary and see if it was
00180            visited.  If it wasn't then the user has added some extra crap
00181            to the dictionary. */
00182 
00183         i = 0;
00184 
00185         while (PyDict_Next(dict, &i, &key, &value)) {
00186                 if (!PyDict_GetItem(visited, key))
00187                         goto done;
00188         }
00189 
00190         result = True;
00191 
00192 done:
00193         /* We must decrement the reference count here or the visited
00194            dictionary will not be freed. */
00195                
00196         Py_DECREF(visited);
00197 
00198         return result;
00199 }
00200 
00201 /* Convert a NULL terminated list of NULL terminated unicode strings
00202    to a list of (char *) strings */
00203 
00204 PyObject *from_unistr_list(uint16 *dependentfiles)
00205 {
00206         PyObject *list;
00207         int offset = 0;
00208 
00209         list = PyList_New(0);
00210 
00211         while (*(dependentfiles + offset) != 0) {
00212                 fstring name;
00213                 int len;
00214 
00215                 len = rpcstr_pull(name, dependentfiles + offset,
00216                                   sizeof(fstring), -1, STR_TERMINATE);
00217 
00218                 offset += len / 2;
00219                 PyList_Append(list, PyString_FromString(name));
00220         }
00221 
00222         return list;
00223 }

Sambaに対してSat Aug 29 21:23:14 2009に生成されました。  doxygen 1.4.7