Commit 4980b2f7 authored by y0no's avatar y0no
Browse files

Improve compareSignature algorithm

parent a6354fd1
......@@ -56,21 +56,29 @@ public class FingerprintMgr {
}
public static DeviceSignature getOrCreateSignature(DeviceInfo device) {
int bestMatch = 0;
DeviceSignature bestMatchSig = null;
/* Try a quick fingerprinting */
/* Compare with existing fingerprints */
for (Map.Entry<String, DeviceSignature> entry : m_oSignatures.entrySet()) {
String signature = entry.getKey();
DeviceSignature model = entry.getValue();
if (compareSignature(ADParser.toRecords(device.getM_bData()),
ADParser.toRecords(signature))) {
Log.d(TAG, "Signature already known : " + model.getName());
device.setM_oSignature(model);
int res = compareSignature(ADParser.toRecords(device.getM_bData()),
ADParser.toRecords(signature));
return model;
if (res > bestMatch) {
bestMatch = res;
bestMatchSig = model;
}
}
if (bestMatchSig != null) {
Log.d(TAG, "Signature already known : "+bestMatchSig.getName()+" ("+bestMatch+"%)");
device.setM_oSignature(bestMatchSig);
return bestMatchSig;
}
String sigString = ADParser.toSignature(device.getM_bData());
DeviceSignature signature = new DeviceSignature(DEVICE_TYPE.UNKNOWN, device.getDeviceName());
addSignature(sigString, signature);
......@@ -78,22 +86,23 @@ public class FingerprintMgr {
return signature;
}
private static boolean compareSignature(ArrayList<ADParser.Record> records,
ArrayList<ADParser.Record> signature) {
boolean type_found = false;
private static int compareSignature(ArrayList<ADParser.Record> records,
ArrayList<ADParser.Record> sigRecords) {
int lenSig = sigRecords.size();
int match = 0;
for (ADParser.Record record: signature) {
for (ADParser.Record record: sigRecords) {
for (ADParser.Record devRecord: records) {
if (record.getType() == devRecord.getType()) {
if (Arrays.equals(record.getData(),
Arrays.copyOfRange(devRecord.getData(), 0, record.getData().length)
)) {
return true;
match += 1;
}
}
}
}
return false;
return match*100/lenSig;
}
public static void loadInitSignature() {
......
......@@ -66,6 +66,10 @@ public class ADParser {
public static String toSignature(byte[] recordData) {
ArrayList<Record> records = toRecords(recordData);
return toSignature(records);
}
public static String toSignature(ArrayList<Record> records) {
ArrayList<String> sigData = new ArrayList<>();
for (ADParser.Record record: records) {
sigData.add(Conversion.bytesToHexString(new byte[]{record.getType()}) + ":" + Conversion.bytesToHexString(record.getData()));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment