-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdhash_psql.c
229 lines (188 loc) · 5.54 KB
/
sdhash_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
////////////////////////////////////////////////////////////////////////////////
// Copyright: (C) 2013
// Author : Andrei Costin ([email protected]) ([email protected])
// Date : 20130907
// Desc : Implements interfacing from PostgreSQL function into the
// sdhash C++-based functions
// Credits : Based on https://github.com/bernerdschaefer/ssdeep_psql
// License : GPLv3
////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include "postgres.h"
#include "fmgr.h"
#include "./utils/builtins.h"
// These includes are adapted from "sdhash-src/sdhash.cc" file. Keep in sync with future versions of "sdhash-src/sdhash.cc"
#include "sdbf/sdbf_class.h"
#include "sdbf/sdbf_defines.h"
#include "sdbf/sdbf_set.h"
#define ERROR_INT32 -1
#define ERROR_TEXT ""
#ifdef __cplusplus
extern "C" {
#endif
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_sdhash_hash);
//Datum pg_sdhash_hash(PG_FUNCTION_ARGS);
Datum pg_sdhash_hash(PG_FUNCTION_ARGS) {
try
{
text *arg = PG_GETARG_TEXT_P(0);
int arg_size = VARSIZE(arg) - VARHDRSZ;
text *pg_hash;
int hash_length;
// 0 means we don't want dd_block mode, rather we want stream mode
sdbf *hash_sdbf = new sdbf((char *) "sdbf_from_stream", (char *) VARDATA(arg), (uint32_t) 0, (uint64_t) arg_size, (index_info *)NULL);
std::string hash_str = hash_sdbf->to_string();
hash_length = hash_str.length();
pg_hash = (text *) palloc(hash_length);
SET_VARSIZE(pg_hash, hash_length+VARHDRSZ);
memcpy(VARDATA(pg_hash), hash_str.c_str(), hash_length);
delete hash_sdbf;
PG_RETURN_TEXT_P(pg_hash);
}
catch(exception& e)
{
PG_RETURN_TEXT_P(cstring_to_text(ERROR_TEXT));
}
catch(int e)
{
PG_RETURN_TEXT_P(cstring_to_text(ERROR_TEXT));
}
}
//
// CREATE OR REPLACE FUNCTION sdhash_hash(TEXT) RETURNS TEXT AS 'sdhash_psql.so', 'pg_sdhash_hash' LANGUAGE 'c';
//
PG_FUNCTION_INFO_V1(pg_sdhash_compare);
//Datum pg_sdhash_compare(PG_FUNCTION_ARGS);
Datum pg_sdhash_compare(PG_FUNCTION_ARGS) {
try
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
int arg_size1 = VARSIZE(arg1) - VARHDRSZ;
int arg_size2 = VARSIZE(arg2) - VARHDRSZ;
int32 score;
// 0 means we don't want dd_block mode, rather we want stream mode
sdbf *hash_sdbf1 = new sdbf((char *) "sdbf_from_stream", (char *) VARDATA(arg1), (uint32_t) 0, (uint64_t) arg_size1, (index_info *)NULL);
sdbf *hash_sdbf2 = new sdbf((char *) "sdbf_from_stream", (char *) VARDATA(arg2), (uint32_t) 0, (uint64_t) arg_size2, (index_info *)NULL);
// Keep 0 in sync with sdhash.cc: sdbf_sys.sample_size
score = (int32) hash_sdbf1->compare(hash_sdbf2, 0);
delete hash_sdbf1;
delete hash_sdbf2;
PG_RETURN_INT32(score);
}
catch(exception& e)
{
PG_RETURN_INT32(ERROR_INT32);
}
catch(int e)
{
PG_RETURN_INT32(e);
}
}
//
// CREATE OR REPLACE FUNCTION sdhash_compare(TEXT, TEXT) RETURNS INTEGER AS 'sdhash_psql.so', 'pg_sdhash_compare' LANGUAGE 'c';
//
PG_FUNCTION_INFO_V1(pg_sdhash_hash_compare);
//Datum pg_sdhash_hash_compare(PG_FUNCTION_ARGS);
Datum pg_sdhash_hash_compare(PG_FUNCTION_ARGS) {
char *str1 = NULL;
char *str2 = NULL;
sdbf *_sdbf1 = NULL;
sdbf *_sdbf2 = NULL;
try
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
int arg_size1 = VARSIZE(arg1) - VARHDRSZ;
int arg_size2 = VARSIZE(arg2) - VARHDRSZ;
int32 score;
str1 = (char *) malloc(arg_size1 + 1);
memcpy(str1, (char *) VARDATA(arg1), arg_size1);
str1[arg_size1] = '\0';
str2 = (char *) malloc(arg_size2 + 1);
memcpy(str2, (char *) VARDATA(arg2), arg_size2);
str2[arg_size2] = '\0';
_sdbf1 = new sdbf(str1);
_sdbf2 = new sdbf(str2);
score = (int32) _sdbf1->compare(_sdbf2, 0);
// Cleanup the memory
if (str1)
{
free(str1);
str1 = NULL;
}
if (str2)
{
free(str2);
str2 = NULL;
}
if (_sdbf1)
{
delete _sdbf1;
_sdbf1 = NULL;
}
if (_sdbf2)
{
delete _sdbf2;
_sdbf2 = NULL;
}
PG_RETURN_INT32(score);
}
catch(exception& e)
{
// Cleanup the memory
if (str1)
{
free(str1);
str1 = NULL;
}
if (str2)
{
free(str2);
str2 = NULL;
}
if (_sdbf1)
{
delete _sdbf1;
_sdbf1 = NULL;
}
if (_sdbf2)
{
delete _sdbf2;
_sdbf2 = NULL;
}
PG_RETURN_INT32(ERROR_INT32);
}
catch(int e)
{
// Cleanup the memory
if (str1)
{
free(str1);
str1 = NULL;
}
if (str2)
{
free(str2);
str2 = NULL;
}
if (_sdbf1)
{
delete _sdbf1;
_sdbf1 = NULL;
}
if (_sdbf2)
{
delete _sdbf2;
_sdbf2 = NULL;
}
PG_RETURN_INT32(e);
}
}
//
// CREATE OR REPLACE FUNCTION sdhash_hash_compare(TEXT, TEXT) RETURNS INTEGER AS 'sdhash_psql.so', 'pg_sdhash_hash_compare' LANGUAGE 'c';
//
#ifdef __cplusplus
}; /* extern "C" */
#endif