1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
import binascii
from cffi import FFI
from ssdeep.__about__ import __version__
cdef = """
static const long FUZZY_FLAG_ELIMSEQ;
static const long FUZZY_FLAG_NOTRUNC;
static const long FUZZY_MAX_RESULT;
struct fuzzy_state;
struct fuzzy_state *fuzzy_new(void);
int fuzzy_update(
struct fuzzy_state *,
const unsigned char *,
size_t
);
int fuzzy_digest(
const struct fuzzy_state *,
char *,
unsigned int
);
void fuzzy_free(struct fuzzy_state *);
int fuzzy_hash_buf(
const unsigned char *,
uint32_t,
char *
);
int fuzzy_hash_file(
FILE *,
char *
);
int fuzzy_hash_stream(
FILE *,
char *
);
int fuzzy_hash_filename(
const char *,
char *
);
int fuzzy_compare(
const char *,
const char *
);
static const long ssdeep_HAS_STATEFUL_HASHING;
"""
source = """
#include "fuzzy.h"
#ifndef FUZZY_FLAG_ELIMSEQ
static const long ssdeep_HAS_STATEFUL_HASHING = 0;
struct fuzzy_state {};
const long FUZZY_FLAG_ELIMSEQ = 0;
const long FUZZY_FLAG_NOTRUNC = 0;
int (*fuzzy_digest)(
const struct fuzzy_state *,
char *,
unsigned int
) = NULL;
int (*fuzzy_free)(struct fuzzy_state *) = NULL;
int (*fuzzy_new)(void) = NULL;
int (*fuzzy_update)(
struct fuzzy_state *,
const unsigned char *,
size_t
) = NULL;
int (*fuzzy_hash_stream)(
FILE *,
char *
) = NULL;
#else
static const long ssdeep_HAS_STATEFUL_HASHING = 1;
#endif
"""
CONDITIONAL_NAMES = {
"ssdeep_HAS_STATEFUL_HASHING": (
"FUZZY_FLAG_ELIMSEQ",
"FUZZY_FLAG_NOTRUNC",
"fuzzy_digest",
"fuzzy_free",
"fuzzy_new",
"fuzzy_update",
"fuzzy_hash_stream"
)
}
class Binding(object):
def __init__(self, extra_objects=None, include_dirs=None, libraries=None):
self.ffi = FFI()
self.ffi.cdef(cdef)
self._lib = None
if extra_objects is None:
extra_objects = []
self._extra_objects = extra_objects
if include_dirs is None:
include_dirs = []
self._include_dirs = include_dirs
if libraries is None:
libraries = ["fuzzy"]
self._libraries = libraries
def verify(self):
self._lib = self.ffi.verify(
source,
ext_package="ssdeep",
extra_objects=self._extra_objects,
include_dirs=self._include_dirs,
modulename=_create_modulename(cdef, source, __version__),
libraries=self._libraries,
)
for condition, names in CONDITIONAL_NAMES.items():
if getattr(self._lib, condition):
continue
for name in names:
delattr(self._lib, name)
@property
def lib(self):
if self._lib is None:
self.verify()
return self._lib
def _create_modulename(cdef, source, sys_version):
key = "\x00".join([sys_version[:3], source, cdef])
key = key.encode("utf-8")
k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff)
k1 = k1.lstrip("0x").rstrip("L")
k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
k2 = k2.lstrip("0").rstrip("L")
return "_ssdeep_cffi_{0}{1}".format(k1, k2)
|