-
Notifications
You must be signed in to change notification settings - Fork 22
/
pyrabin.c
331 lines (278 loc) · 8.58 KB
/
pyrabin.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* @file pyrabin.c
* @author Wei-Ning Huang (AZ) <[email protected]>
*
* Copyright (C) 2013 - Wei-Ning Huang (AZ) <[email protected]>
* All Rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <Python.h>
#include <openssl/sha.h>
#include "utils.h"
#include "rabin_polynomial.h"
#define READ_BUF_SIZE 1048576
struct module_state {
PyObject *RabinError;
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
extern PyTypeObject RabinType;
extern uint64_t rabin_polynomial_prime;
extern unsigned int rabin_sliding_window_size;
extern unsigned int rabin_polynomial_max_block_size;
extern unsigned int rabin_polynomial_min_block_size;
extern unsigned int rabin_polynomial_average_block_size;
static PyObject* set_prime(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, "K", &rabin_polynomial_prime)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* set_window_size(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, "I", &rabin_sliding_window_size)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* set_max_block_size(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, "I", &rabin_polynomial_max_block_size)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* set_min_block_size(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, "I", &rabin_polynomial_min_block_size)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* set_average_block_size(PyObject* self, PyObject* args)
{
unsigned int prev;
if (!PyArg_ParseTuple(args, "I", &prev)) {
return NULL;
}
if (prev < rabin_polynomial_min_block_size ||
prev > rabin_polynomial_max_block_size) {
struct module_state *st = GETSTATE(self);
PyErr_SetString(st->RabinError,
"average block size should between min and max block size");
return NULL;
}
rabin_polynomial_average_block_size = prev;
Py_INCREF(Py_None);
return Py_None;
}
static void to_hex_digest(char* digest, char* hex_digest)
{
int i, j;
for(i = j = 0; i < SHA_DIGEST_LENGTH; ++i) {
char c;
c = (digest[i] >> 4) & 0xf;
c = (c > 9) ? c + 'a'- 10 : c + '0';
hex_digest[j++] = c;
c = (digest[i] & 0xf);
c = (c > 9) ? c + 'a' - 10 : c + '0';
hex_digest[j++] = c;
}
hex_digest[2 * SHA_DIGEST_LENGTH] = 0;
}
static PyObject* get_file_fingerprints(PyObject* self, PyObject* args)
{
const char *filename;
if (!PyArg_ParseTuple(args, "s", &filename)) {
return NULL;
}
FILE* fp = fopen(filename, "rb");
if (!fp) {
struct module_state *st = GETSTATE(self);
return PyErr_SetFromErrnoWithFilename(st->RabinError, filename);
}
struct rabin_polynomial* head = get_file_rabin_polys(fp);
if (head == NULL) {
return PyList_New(0);
}
fclose(fp);
PyObject* list = rabin_polynomial_to_PyList(head);
free_rabin_fingerprint_list(head);
return list;
}
static PyObject* split_file_by_fingerprints(PyObject* self, PyObject* args)
{
struct module_state *st = GETSTATE(self);
const char *filename;
if (!PyArg_ParseTuple(args, "s", &filename)) {
return NULL;
}
FILE* fp = fopen(filename, "rb");
if (!fp) {
return PyErr_SetFromErrnoWithFilename(st->RabinError, filename);
}
struct rabin_polynomial* head = get_file_rabin_polys(fp);
if (head == NULL) {
PyErr_SetString(st->RabinError, "get_file_rabin_polys()");
return NULL;
}
struct rabin_polynomial* curr = head;
char outfile[BUFSIZ];
char* buffer = malloc(READ_BUF_SIZE);
uint64_t offset = 0;
size_t read_size = 0;
char digest[SHA_DIGEST_LENGTH];
char hex_digest[SHA_DIGEST_LENGTH * 2 + 5];
SHA_CTX ctx;
PyObject* list = NULL;
PyObject* tuple = NULL;
if (!(list = PyList_New(0))) {
return NULL;
}
while (curr) {
if ((offset = fseek(fp, curr->start, SEEK_SET) == -1)) {
fclose(fp);
return PyErr_SetFromErrnoWithFilename(st->RabinError, filename);
}
/* Save chunk to temporarily file */
snprintf(outfile, BUFSIZ, ".%llx.tmp", (unsigned long long) curr->polynomial);
SHA1_Init(&ctx);
FILE* ofp = fopen(outfile, "wb");
if (!ofp) {
fclose(fp);
return PyErr_SetFromErrnoWithFilename(st->RabinError, filename);
}
size_t remain_read = curr->length;
size_t bytes_read = 0;
do {
read_size = (curr->length < READ_BUF_SIZE)? curr->length: READ_BUF_SIZE;
bytes_read = fread(buffer, 1, read_size, fp);
SHA1_Update(&ctx, buffer, bytes_read);
size_t total_written = 0;
size_t bytes_written = 0;
do {
bytes_written = fwrite(buffer, 1, read_size, ofp);
if (bytes_written == 0 && ferror(ofp)) {
fclose(fp);
fclose(ofp);
return PyErr_SetFromErrnoWithFilename(st->RabinError, outfile);
}
total_written += bytes_written;
} while (total_written != bytes_read);
remain_read -= bytes_read;
} while (remain_read > 0);
fclose(ofp);
/* Rename chunk using SHA-1 sum as filename */
SHA1_Final((unsigned char *) digest, &ctx);
to_hex_digest(digest, hex_digest);
strncat(hex_digest, ".blk", SHA_DIGEST_LENGTH * 2 + 5);
rename(outfile, hex_digest);
tuple = Py_BuildValue("KiKs", curr->start, curr->length,
curr->polynomial, hex_digest);
/* PyObject_Print(tuple, stdout, 0); printf("\n"); */
PyList_Append(list, tuple);
Py_DECREF(tuple);
curr = curr->next_polynomial;
}
free_rabin_fingerprint_list(head);
fclose(fp);
return list;
}
static PyMethodDef PyRabinMethods[] = {
{"set_prime", (PyCFunction)set_prime,
METH_VARARGS, "Set Rabin polynomial prime"},
{"set_window_size", (PyCFunction)set_window_size,
METH_VARARGS, "Set Rabin polynomial sliding window size"},
{"set_max_block_size", (PyCFunction)set_max_block_size,
METH_VARARGS, "Set Rabin polynomial max block size"},
{"set_min_block_size", (PyCFunction)set_min_block_size,
METH_VARARGS, "Set Rabin polynomial min block size"},
{"set_average_block_size", (PyCFunction)set_average_block_size,
METH_VARARGS, "Set Rabin polynomial average block size"},
{"get_file_fingerprints", (PyCFunction)get_file_fingerprints,
METH_VARARGS, "Get Rabin fingerprint of a file"},
{"get_file_fingerprints", (PyCFunction)get_file_fingerprints,
METH_VARARGS, "Get Rabin fingerprint of a file"},
{"split_file_by_fingerprints", (PyCFunction)split_file_by_fingerprints,
METH_VARARGS, "Split a file by fingerprints"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static int rabin_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->RabinError);
return 0;
}
static int rabin_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->RabinError);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"rabin",
NULL,
sizeof(struct module_state),
PyRabinMethods,
NULL,
rabin_traverse,
rabin_clear,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC
PyInit_rabin(void)
#else
#define INITERROR return
void
initrabin(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("rabin", PyRabinMethods);
#endif
if (module == NULL)
INITERROR;
struct module_state *st = GETSTATE(module);
// Initialize defaults
initialize_rabin_polynomial_defaults();
// Initialize rabin.Rabin
if (PyType_Ready(&RabinType) < 0) {
fprintf(stderr, "Invalid PyTypeObject `RabinType'\n");
INITERROR;
}
Py_INCREF(&RabinType);
PyModule_AddObject(module, "Rabin", (PyObject*)&RabinType);
// Initialize RabinError
st->RabinError = PyErr_NewException("rabin.error", NULL, NULL);
if (st->RabinError == NULL) {
Py_DECREF(module);
INITERROR;
}
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}