forked from bernerdschaefer/ssdeep_psql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssdeep_psql.c
74 lines (54 loc) · 1.93 KB
/
ssdeep_psql.c
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
#include "postgres.h"
#include "fmgr.h"
#include <fuzzy.h>
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_fuzzy_hash);
Datum pg_fuzzy_hash(PG_FUNCTION_ARGS);
Datum pg_fuzzy_hash(PG_FUNCTION_ARGS) {
text *arg = PG_GETARG_TEXT_P(0);
int arg_size = VARSIZE(arg) - VARHDRSZ;
char *hash = (char *) palloc(FUZZY_MAX_RESULT);
text *pg_hash;
int hash_length;
fuzzy_hash_buf((unsigned char *) VARDATA(arg), arg_size, hash);
hash_length = strlen(hash);
pg_hash = (text *) palloc(hash_length);
SET_VARSIZE(pg_hash, hash_length+VARHDRSZ);
memcpy(VARDATA(pg_hash), hash, hash_length);
pfree(hash);
PG_RETURN_TEXT_P(pg_hash);
}
//
// CREATE OR REPLACE FUNCTION fuzzy_hash(TEXT) RETURNS TEXT AS 'ssdeep_psql.so', 'pg_fuzzy_hash' LANGUAGE 'C';
//
PG_FUNCTION_INFO_V1(pg_fuzzy_compare);
Datum pg_fuzzy_compare(PG_FUNCTION_ARGS);
Datum pg_fuzzy_compare(PG_FUNCTION_ARGS) {
char *hash1, *hash2;
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
int32 score;
hash1 = (char *) palloc(FUZZY_MAX_RESULT);
hash2 = (char *) palloc(FUZZY_MAX_RESULT);
fuzzy_hash_buf((unsigned char *) VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ, hash1);
fuzzy_hash_buf((unsigned char *) VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ, hash2);
score = (int32) fuzzy_compare(hash1, hash2);
pfree(hash1);
pfree(hash2);
PG_RETURN_INT32(score);
}
//
// CREATE OR REPLACE FUNCTION fuzzy_compare(TEXT, TEXT) RETURNS INTEGER AS 'ssdeep_psql.so', 'pg_fuzzy_compare' LANGUAGE 'C';
//
PG_FUNCTION_INFO_V1(pg_fuzzy_hash_compare);
Datum pg_fuzzy_hash_compare(PG_FUNCTION_ARGS);
Datum pg_fuzzy_hash_compare(PG_FUNCTION_ARGS) {
text *hash1 = PG_GETARG_TEXT_P(0);
text *hash2 = PG_GETARG_TEXT_P(1);
int32 score;
score = (int32) fuzzy_compare((char *) VARDATA(hash1), (char *) VARDATA(hash2));
PG_RETURN_INT32(score);
}
//
// CREATE OR REPLACE FUNCTION fuzzy_hash_compare(TEXT, TEXT) RETURNS INTEGER AS 'ssdeep_psql.so', 'pg_fuzzy_hash_compare' LANGUAGE 'C';
//