-
Notifications
You must be signed in to change notification settings - Fork 5
/
rdiff.cc
289 lines (229 loc) · 8.01 KB
/
rdiff.cc
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
#include "rdiff.h"
using namespace v8;
// Signature
class GenerateSignatureWorker : public Nan::AsyncWorker {
public:
GenerateSignatureWorker(Nan::Callback *callback, v8::Local<v8::Value> in, v8::Local<v8::Value> out)
: Nan::AsyncWorker(callback), in(get(in)), out(get(out))
{}
~GenerateSignatureWorker() {}
void Execute() {
ret = signature(in.c_str(), out.c_str());
if (ret != 0) {
errmsg = (char *) "Error generating signature";
}
}
void HandleOKCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Null(),
Nan::New<Number>(1)
};
callback->Call(2, argv);
}
void HandleErrorCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Error(errmsg)
};
callback->Call(1, argv);
}
private:
int ret;
std::string in;
std::string out;
char *errmsg;
};
class GenerateDeltaWorker : public Nan::AsyncWorker {
public:
GenerateDeltaWorker(Nan::Callback *callback, v8::Local<v8::Value> sig_name,v8::Local<v8::Value> in, v8::Local<v8::Value> out)
: Nan::AsyncWorker(callback), sig_name(get(sig_name)), in(get(in)), out(get(out))
{}
~GenerateDeltaWorker() {}
void Execute() {
ret = delta(sig_name.c_str(), in.c_str(), out.c_str());
if (ret != 0) {
errmsg = (char *) "Error generating delta";
}
}
void HandleOKCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Null(),
Nan::New<Number>(1)
};
callback->Call(2, argv);
}
void HandleErrorCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Error(errmsg)
};
callback->Call(1, argv);
}
private:
int ret;
std::string sig_name;
std::string in;
std::string out;
char *errmsg;
};
class PatchWorker : public Nan::AsyncWorker {
public:
PatchWorker(Nan::Callback *callback, v8::Local<v8::Value> basis_name, v8::Local<v8::Value> in, v8::Local<v8::Value> out)
: Nan::AsyncWorker(callback), basis_name(get(basis_name)), in(get(in)), out(get(out))
{}
~PatchWorker() {}
void Execute() {
ret = patch(basis_name.c_str(), in.c_str(), out.c_str());
if (ret != 0) {
errmsg = (char *) "Error patching";
}
}
void HandleOKCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Null(),
Nan::New<Number>(1)
};
callback->Call(2, argv);
}
void HandleErrorCallback() {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Error(errmsg)
};
callback->Call(1, argv);
}
private:
int ret;
std::string basis_name;
std::string in;
std::string out;
char *errmsg;
};
NAN_METHOD (GenerateSignatureSync) {
int ret = signature(get(info[0]).c_str(), get(info[1]).c_str());
info.GetReturnValue().Set(ret);
}
NAN_METHOD(GenerateSignatureAsync) {
Nan::Callback *callback = new Nan::Callback(info[2].As<Function>());
Nan::AsyncQueueWorker(new GenerateSignatureWorker(callback, info[0], info[1]));
}
NAN_METHOD (GenerateDeltaSync) {
int ret = delta(get(info[0]).c_str(), get(info[1]).c_str(), get(info[2]).c_str());
info.GetReturnValue().Set(ret);
}
NAN_METHOD(GenerateDeltaAsync) {
Nan::Callback *callback = new Nan::Callback(info[3].As<Function>());
Nan::AsyncQueueWorker(new GenerateDeltaWorker(callback, info[0], info[1], info[2]));
}
NAN_METHOD (PatchSync) {
int ret = patch(get(info[0]).c_str(), get(info[1]).c_str(), get(info[2]).c_str());
info.GetReturnValue().Set(ret);
}
NAN_METHOD(PatchAsync) {
Nan::Callback *callback = new Nan::Callback(info[3].As<Function>());
Nan::AsyncQueueWorker(new PatchWorker(callback, info[0], info[1], info[2]));
}
NAN_MODULE_INIT(InitAll) {
Nan::Set(target, Nan::New<String>("signatureSync").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GenerateSignatureSync)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("signature").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GenerateSignatureAsync)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("deltaSync").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GenerateDeltaSync)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("delta").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GenerateDeltaAsync)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("patchSync").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(PatchSync)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("patch").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(PatchAsync)).ToLocalChecked());
}
NODE_MODULE(rdiff, InitAll)
static size_t block_len = RS_DEFAULT_BLOCK_LEN;
#ifdef RS_DEFAULT_STRONG_LEN
static size_t strong_len = RS_DEFAULT_STRONG_LEN;
#endif
// cleaned up for a while
// static int bzip2_level = 0;
// static int gzip_level = 0;
enum {
OPT_GZIP = 1069, OPT_BZIP2
};
FILE * rs_file_open(const char *filename, const char *mode) {
FILE * f;
int is_write;
is_write = mode[0] == 'w';
if (!filename || !strcmp("-", filename)) {
if (is_write) {
return stdout;
}
else {
return stdin;
}
}
if (!(f = fopen(filename, mode))) {
exit(RS_IO_ERROR);
}
return f;
}
int rs_file_close (FILE * f) {
if ((f == stdin) || (f == stdout)) return 0;
return fclose(f);
}
rs_result signature(const char *in, const char *out) {
FILE * basis_file, * sig_file;
rs_stats_t stats;
rs_result result;
basis_file = rs_file_open(in, "rb");
sig_file = rs_file_open(out, "wb");
#ifdef RS_DEFAULT_STRONG_LEN
result = rs_sig_file(basis_file, sig_file, block_len, strong_len, &stats);
#else
result = rs_sig_file(basis_file, sig_file, block_len, (size_t) 8, RS_MD4_SIG_MAGIC, &stats);
#endif
rs_file_close(sig_file);
rs_file_close(basis_file);
if (result != RS_DONE) {
return result;
}
return result;
}
rs_result delta(const char *sig_name, const char *in, const char *out) {
FILE *sig_file, *new_file, *delta_file;
rs_result result;
rs_signature_t *sumset;
rs_stats_t stats;
sig_file = rs_file_open(sig_name, "rb");
new_file = rs_file_open(in, "rb");
delta_file = rs_file_open(out, "wb");
result = rs_loadsig_file(sig_file, &sumset, &stats);
if (result != RS_DONE)
return result;
if ((result = rs_build_hash_table(sumset)) != RS_DONE)
return result;
result = rs_delta_file(sumset, new_file, delta_file, &stats);
rs_free_sumset(sumset);
rs_file_close(delta_file);
rs_file_close(new_file);
rs_file_close(sig_file);
return result;
}
rs_result patch(const char* basis_name, const char *in, const char *out) {
/* patch BASIS [DELTA [NEWFILE]] */
FILE *basis_file, *delta_file, *new_file;
rs_stats_t stats;
rs_result result;
basis_file = rs_file_open(basis_name, "rb");
delta_file = rs_file_open(in, "rb");
new_file = rs_file_open(out, "wb");
result = rs_patch_file(basis_file, delta_file, new_file, &stats);
rs_file_close(new_file);
rs_file_close(delta_file);
rs_file_close(basis_file);
return result;
}
inline std::string get(const v8::Local<v8::Value> &value) {
return std::string(*v8::String::Utf8Value(value));
}