<s> import os <EOL> import sys <EOL> import glob <EOL> import ctypes <EOL> import ctypes . util <EOL> import binwalk . core . common <EOL> from binwalk . core . compat import * <EOL> class Function ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . name = None <EOL> self . type = int <EOL> for ( k , v ) in iterator ( kwargs ) : <EOL> setattr ( self , k , v ) <EOL> class FunctionHandler ( object ) : <EOL> '''<STR_LIT>''' <EOL> PY2CTYPES = { <EOL> bytes : ctypes . c_char_p , <EOL> str : ctypes . c_char_p , <EOL> int : ctypes . c_int , <EOL> float : ctypes . c_float , <EOL> bool : ctypes . c_int , <EOL> None : ctypes . c_int , <EOL> } <EOL> RETVAL_CONVERTERS = { <EOL> None : int , <EOL> int : int , <EOL> float : float , <EOL> bool : bool , <EOL> str : bytes2str , <EOL> bytes : str2bytes , <EOL> } <EOL> def __init__ ( self , library , function ) : <EOL> '''<STR_LIT>''' <EOL> self . name = function . name <EOL> self . retype = function . type <EOL> self . function = getattr ( library , self . name ) <EOL> if has_key ( self . PY2CTYPES , self . retype ) : <EOL> self . function . restype = self . PY2CTYPES [ self . retype ] <EOL> self . retval_converter = self . RETVAL_CONVERTERS [ self . retype ] <EOL> else : <EOL> self . function . restype = self . retype <EOL> self . retval_converter = None <EOL> def run ( self , * args ) : <EOL> '''<STR_LIT>''' <EOL> args = list ( args ) <EOL> for i in range ( <NUM_LIT:0> , len ( args ) ) : <EOL> if isinstance ( args [ i ] , str ) : <EOL> args [ i ] = str2bytes ( args [ i ] ) <EOL> retval = self . function ( * args ) <EOL> if self . retval_converter is not None : <EOL> retval = self . retval_converter ( retval ) <EOL> return retval <EOL> class Library ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , library , functions ) : <EOL> '''<STR_LIT>''' <EOL> self . settings = binwalk . core . settings . Settings ( ) <EOL> self . library = ctypes . cdll . LoadLibrary ( self . find_library ( library ) ) <EOL> if not self . library : <EOL> raise Exception ( "<STR_LIT>" % library ) <EOL> for function in functions : <EOL> f = FunctionHandler ( self . library , function ) <EOL> setattr ( self , function . name , f . run ) <EOL> def find_library ( self , libraries ) : <EOL> '''<STR_LIT>''' <EOL> lib_path = None <EOL> prefix = binwalk . core . common . get_libs_path ( ) <EOL> if isinstance ( libraries , str ) : <EOL> libraries = [ libraries ] <EOL> for library in libraries : <EOL> system_paths = { <EOL> '<STR_LIT>' : [ os . path . join ( prefix , '<STR_LIT>' % library ) , '<STR_LIT>' % library ] , <EOL> '<STR_LIT>' : [ os . path . join ( prefix , '<STR_LIT>' % library ) , '<STR_LIT>' % library ] , <EOL> '<STR_LIT:win32>' : [ os . path . join ( prefix , '<STR_LIT>' % library ) , '<STR_LIT>' % library ] , <EOL> '<STR_LIT>' : [ os . path . join ( prefix , '<STR_LIT>' % library ) , <EOL> '<STR_LIT>' % library , <EOL> '<STR_LIT>' % library , <EOL> ] + glob . glob ( '<STR_LIT>' % ( library , library ) ) , <EOL> } <EOL> for i in range ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> system_paths [ '<STR_LIT>' % i ] = system_paths [ '<STR_LIT>' ] <EOL> for path in system_paths [ sys . platform ] : <EOL> binwalk . core . common . debug ( "<STR_LIT>" % path ) <EOL> if os . path . exists ( path ) : <EOL> lib_path = path <EOL> break <EOL> if not lib_path : <EOL> lib_path = ctypes . util . find_library ( library ) <EOL> if lib_path : <EOL> binwalk . core . common . debug ( "<STR_LIT>" % ( library , lib_path ) ) <EOL> break <EOL> else : <EOL> binwalk . core . common . debug ( "<STR_LIT>" % library ) <EOL> if not lib_path : <EOL> raise Exception ( "<STR_LIT>" % str ( libraries ) ) <EOL> return lib_path </s>
