2011-11-10 2 views
0

fuzzy_compare関数が正しい比較値を返すことに問題があります。これは、0〜100を返すが、常に(c_char_p)とcreate_string_buffer()にキャスト、ファジーがout1.valueと比較呼び出す0ssdeepのfuzzy_compareは常にctypesのPythonからロードされた0を返します

from ctypes import * 
fuzzy = CDLL('fuzzy.dll') 
out1 = create_string_buffer('\x00'*512) 
out2 = create_string_buffer('\x00'*512) 
print fuzzy.fuzzy_hash_buf('hashme', len('hashme'), out1) 
print fuzzy.fuzzy_hash_buf('hashme2', len('hashme2'), out2) 
print out1.value 
print out2.value 
print fuzzy.fuzzy_compare(out1, out2) 
# output 
# 0 
# 0 
# 3:cA:x <-- correct hash 
# 3:cy:R <-- correct hash 
# 0  <-- fuzzy_compare returning 0... 

私が試した返しますが、それは常に私が見てきた0を返すべきですそれはデバッガで(ファジー・コンパイル関数のbpをセットして、値を正しく渡しているので、なぜそれが常に0を返すのか分かりません。)

答えて

0

付属のsample.cをコンパイルしました。私はPythonで似たようなことを試みました。非常に大きな入力バッファ(0x50000)を使用し、16バイトを変更するだけで、99に近い一致が得られることに注意してください。

import ctypes 
from random import randrange, seed 

seed(42) 

SPAMSUM_LENGTH = 64 
FUZZY_MAX_RESULT = SPAMSUM_LENGTH + SPAMSUM_LENGTH // 2 + 20 
SIZE = 0x50000 

fuzzy = ctypes.CDLL('fuzzy.dll') 

fuzzy_hash_buf = fuzzy.fuzzy_hash_buf 
fuzzy_hash_buf.restype = ctypes.c_int 
fuzzy_hash_buf.argtypes = [ 
    ctypes.c_char_p, #buf 
    ctypes.c_uint32, #buf_len 
    ctypes.c_char_p, #result 
] 
fuzzy_compare = fuzzy.fuzzy_compare 
fuzzy_compare.restype = ctypes.c_int 
fuzzy_compare.argtypes = [ 
    ctypes.c_char_p, #sig1 
    ctypes.c_char_p, #sig2 
] 

out1 = ctypes.create_string_buffer('\x00' * FUZZY_MAX_RESULT) 
out2 = ctypes.create_string_buffer('\x00' * FUZZY_MAX_RESULT) 

in1 = ''.join(chr(randrange(256)) for x in xrange(SIZE)) 
in2 = ''.join(c if (i < 0x100 or i >= 0x110) else '\x25' 
       for i, c in enumerate(in1)) 

print fuzzy_hash_buf(in1, len(in1), out1) 
print fuzzy_hash_buf(in2, len(in2), out2) 
print out1.value 
print out2.value 
print fuzzy_compare(out1, out2) 

出力:

0 
0 
6144:rR1yoHH0XI3VFNdCRXmg0BqMFKDLA6sPaVujZAQhO5HQXNHtQyr4zgytywlfj:qYFF5CRXmg9IK4ouqQAHu+y8zgS7 
6144:LR1yoHH0XI3VFNdCRXmg0BqMFKDLA6sPaVujZAQhO5HQXNHtQyr4zgytywlfj:KYFF5CRXmg9IK4ouqQAHu+y8zgS7 
99 
+0

あなたは私のヒーローです。魅力のように働く、私は地獄ctypesがどのように動作するかを研究すべきだと思う! – omgpants

関連する問題