2002-03-26 14:07:45 +03:00
/*
Python wrappers for DCERPC / SMB client routines .
Copyright ( C ) Tim Potter , 2002
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
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
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 0213 9 , USA .
*/
# include "py_conv.h"
/* Helper for rpcstr_pull() function */
static void fstr_pull ( fstring str , UNISTR * uni )
{
2002-05-07 11:00:14 +04:00
rpcstr_pull ( str , uni - > buffer , sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
2002-03-26 14:07:45 +03:00
}
2003-02-20 01:47:49 +03:00
static void fstr_pull2 ( fstring str , UNISTR2 * uni )
{
rpcstr_pull ( str , uni - > buffer , sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
}
2002-03-26 14:07:45 +03:00
/* Convert a structure to a Python dict */
PyObject * from_struct ( void * s , struct pyconv * conv )
{
PyObject * obj , * item ;
int i ;
obj = PyDict_New ( ) ;
for ( i = 0 ; conv [ i ] . name ; i + + ) {
switch ( conv [ i ] . type ) {
case PY_UNISTR : {
UNISTR * u = ( UNISTR * ) ( ( char * ) s + conv [ i ] . offset ) ;
2002-11-29 04:10:10 +03:00
fstring str = " " ;
2002-03-26 14:07:45 +03:00
if ( u - > buffer )
2002-11-29 05:58:59 +03:00
fstr_pull ( str , u ) ;
2002-03-26 14:07:45 +03:00
2002-11-29 04:10:10 +03:00
item = PyString_FromString ( str ) ;
2002-03-26 14:07:45 +03:00
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
2003-02-20 01:47:49 +03:00
case PY_UNISTR2 : {
UNISTR2 * u = ( UNISTR2 * ) ( ( char * ) s + conv [ i ] . offset ) ;
fstring str = " " ;
if ( u - > buffer )
fstr_pull2 ( str , u ) ;
item = PyString_FromString ( str ) ;
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
2002-03-26 14:07:45 +03:00
case PY_UINT32 : {
uint32 * u = ( uint32 * ) ( ( char * ) s + conv [ i ] . offset ) ;
item = PyInt_FromLong ( * u ) ;
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
case PY_UINT16 : {
uint16 * u = ( uint16 * ) ( ( char * ) s + conv [ i ] . offset ) ;
item = PyInt_FromLong ( * u ) ;
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
2002-09-11 08:54:20 +04:00
case PY_STRING : {
char * str = ( char * ) s + conv [ i ] . offset ;
item = PyString_FromString ( str ) ;
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
case PY_UID : {
uid_t * uid = ( uid_t * ) ( ( char * ) s + conv [ i ] . offset ) ;
item = PyInt_FromLong ( * uid ) ;
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
case PY_GID : {
gid_t * gid = ( gid_t * ) ( ( char * ) s + conv [ i ] . offset ) ;
item = PyInt_FromLong ( * gid ) ;
PyDict_SetItemString ( obj , conv [ i ] . name , item ) ;
break ;
}
2002-03-26 14:07:45 +03:00
default :
2002-09-11 08:54:20 +04:00
2002-03-26 14:07:45 +03:00
break ;
}
}
return obj ;
}
/* Convert a Python dict to a structure */
2002-05-17 06:24:06 +04:00
BOOL to_struct ( void * s , PyObject * dict , struct pyconv * conv )
2002-03-26 14:07:45 +03:00
{
2002-05-20 12:04:02 +04:00
PyObject * visited , * key , * value ;
BOOL result = False ;
2002-03-26 14:07:45 +03:00
int i ;
2002-05-20 12:04:02 +04:00
visited = PyDict_New ( ) ;
2002-03-26 14:07:45 +03:00
for ( i = 0 ; conv [ i ] . name ; i + + ) {
PyObject * obj ;
obj = PyDict_GetItemString ( dict , conv [ i ] . name ) ;
2002-05-17 06:24:06 +04:00
if ( ! obj )
2002-05-20 12:04:02 +04:00
goto done ;
2002-03-26 14:07:45 +03:00
switch ( conv [ i ] . type ) {
case PY_UNISTR : {
UNISTR * u = ( UNISTR * ) ( ( char * ) s + conv [ i ] . offset ) ;
2002-11-29 04:10:10 +03:00
char * str = " " ;
2002-03-26 14:07:45 +03:00
2002-05-27 10:33:33 +04:00
if ( ! PyString_Check ( obj ) )
goto done ;
2002-03-26 14:07:45 +03:00
2002-11-29 04:10:10 +03:00
str = PyString_AsString ( obj ) ;
init_unistr ( u , str ) ;
2002-03-26 14:07:45 +03:00
break ;
}
case PY_UINT32 : {
uint32 * u = ( uint32 * ) ( ( char * ) s + conv [ i ] . offset ) ;
2002-05-27 10:33:33 +04:00
if ( ! PyInt_Check ( obj ) )
goto done ;
* u = PyInt_AsLong ( obj ) ;
2002-03-26 14:07:45 +03:00
break ;
}
case PY_UINT16 : {
uint16 * u = ( uint16 * ) ( ( char * ) s + conv [ i ] . offset ) ;
2002-05-27 10:33:33 +04:00
if ( ! PyInt_Check ( obj ) )
goto done ;
2002-03-26 14:07:45 +03:00
2002-05-27 10:33:33 +04:00
* u = PyInt_AsLong ( obj ) ;
2002-03-26 14:07:45 +03:00
break ;
}
default :
break ;
}
2002-05-20 12:04:02 +04:00
/* Mark as visited */
PyDict_SetItemString ( visited , conv [ i ] . name ,
PyInt_FromLong ( 1 ) ) ;
2002-03-26 14:07:45 +03:00
}
2002-05-17 06:24:06 +04:00
2002-05-20 12:04:02 +04:00
/* Iterate over each item in the input dictionary and see if it was
visited . If it wasn ' t then the user has added some extra crap
to the dictionary . */
i = 0 ;
while ( PyDict_Next ( dict , & i , & key , & value ) ) {
if ( ! PyDict_GetItem ( visited , key ) )
goto done ;
}
result = True ;
done :
/* We must decrement the reference count here or the visited
dictionary will not be freed . */
Py_DECREF ( visited ) ;
return result ;
2002-03-26 14:07:45 +03:00
}
2002-11-07 04:12:24 +03:00
/* Convert a NULL terminated list of NULL terminated unicode strings
to a list of ( char * ) strings */
PyObject * from_unistr_list ( uint16 * dependentfiles )
{
PyObject * list ;
int offset = 0 ;
list = PyList_New ( 0 ) ;
while ( * ( dependentfiles + offset ) ! = 0 ) {
fstring name ;
int len ;
len = rpcstr_pull ( name , dependentfiles + offset ,
sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
offset + = len / 2 ;
PyList_Append ( list , PyString_FromString ( name ) ) ;
}
return list ;
}