Trees | Indices | Help |
|
---|
|
1 # This file is part of Androguard. 2 # 3 # Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org> 4 # All rights reserved. 5 # 6 # Androguard is free software: you can redistribute it and/or modify 7 # it under the terms of the GNU Lesser 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 # Androguard 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 Lesser General Public License for more details. 15 # 16 # You should have received a copy of the GNU Lesser General Public License 17 # along with Androguard. If not, see <http://www.gnu.org/licenses/>. 18 19 import hashlib 20 21 from ctypes import cdll, c_float, c_int, c_uint, c_void_p, Structure, addressof, create_string_buffer, cast 22 23 #struct libsimilarity { 24 # void *orig; 25 # unsigned int size_orig; 26 # void *cmp; 27 # unsigned size_cmp; 28 29 # unsigned int *corig; 30 # unsigned int *ccmp; 31 # 32 # float res; 33 #};35 _fields_ = [("orig", c_void_p), 36 ("size_orig", c_uint), 37 ("cmp", c_void_p), 38 ("size_cmp", c_uint), 39 40 ("corig", c_uint), 41 ("ccmp", c_uint), 42 43 ("res", c_float), 44 ]45 46 ZLIB_COMPRESS = 0 47 BZ2_COMPRESS = 1 48 SMAZ_COMPRESS = 2 49 LZMA_COMPRESS = 3 50 XZ_COMPRESS = 4 51 SNAPPY_COMPRESS = 513954 self._u = cdll.LoadLibrary( path ) 55 56 self._u.compress.restype = c_uint 57 self._u.ncd.restype = c_int 58 self._u.ncs.restype = c_int 59 self._u.cmid.restype = c_int 60 self._u.entropy.restype = c_float 61 self._u.levenshtein.restype = c_uint 62 63 self._level = 9 64 65 self.__libsim_t = LIBSIMILARITY_T() 66 67 self.__caches = { 68 ZLIB_COMPRESS : {}, 69 BZ2_COMPRESS : {}, 70 SMAZ_COMPRESS : {}, 71 LZMA_COMPRESS : {}, 72 XZ_COMPRESS : {}, 73 SNAPPY_COMPRESS : {}, 74 } 75 76 self.set_compress_type( ZLIB_COMPRESS )77 8082 try : 83 return self.__caches[ self._type ][ hashlib.md5( s ).hexdigest() ] 84 except KeyError : 85 return c_uint( 0 )8688 h = hashlib.md5( s ).hexdigest() 89 if h not in self.__caches[ self._type ] : 90 self.__caches[ self._type ][ h ] = v91 95 99101 self.__libsim_t.orig = cast( s1, c_void_p ) 102 self.__libsim_t.size_orig = len(s1) 103 104 self.__libsim_t.cmp = cast( s2, c_void_p ) 105 self.__libsim_t.size_cmp = len(s2) 106 107 corig = self.get_in_caches(s1) 108 ccmp = self.get_in_caches(s2) 109 self.__libsim_t.corig = addressof( corig ) 110 self.__libsim_t.ccmp = addressof( ccmp ) 111 112 ret = func( self._level, addressof( self.__libsim_t ) ) 113 114 self.add_in_caches(s1, corig) 115 self.add_in_caches(s2, ccmp) 116 117 return self.__libsim_t.res118 121 124 127 131133 res = self._u.levenshtein( cast( s1, c_void_p ), len( s1 ), cast( s2, c_void_p ), len( s2 ) ) 134 return res135
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Wed May 18 15:57:05 2011 | http://epydoc.sourceforge.net |