-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
437 lines (388 loc) · 14.3 KB
/
index.js
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"use strict";
var y = require('./build/Release/yencode.node');
var toBuffer = Buffer.alloc ? Buffer.from : Buffer;
var bufferSlice = Buffer.prototype.readBigInt64BE ? Buffer.prototype.subarray : Buffer.prototype.slice;
var nl = toBuffer([13, 10]);
var RE_BADCHAR = /\r\n\0/g;
var RE_YPROP = /([a-z_][a-z_0-9]*)=/;
var RE_NUMBER = /^\d+$/;
var decodePrev = ['\r\n', '=', '\r', '', '\r\n.', '\r\n.\r', '\r\n='];
var DecoderError = function(code, message) {
var err = new Error(message);
err.code = code;
return err;
}
var DecoderWarning = function(code, message) {
if(!this) return new DecoderWarning(code, message);
this.code = code;
this.message = message;
}
DecoderWarning.prototype = { toString: function() { return this.message; } };
var bufferFind, bufferFindRev;
if(Buffer.prototype.indexOf)
bufferFind = function(buf, search, start) {
return buf.indexOf(search, start, module.exports.encoding);
};
else
bufferFind = function(buf, search, start) {
if(!Buffer.isBuffer(search))
search = toBuffer(search, module.exports.encoding);
if(search.length == 0) return -1;
start = start|0;
if(search.length > buf.length-start) return -1;
for(var i = start; i < buf.length - search.length + 1; i++) {
var match = true;
for(var j = 0; j < search.length; j++) {
if(buf[i+j] != search[j]) {
match = false;
break;
}
}
if(match) return i;
}
return -1;
};
if(Buffer.prototype.lastIndexOf)
bufferFindRev = function(buf, search) {
return buf.lastIndexOf(search, -1, module.exports.encoding);
};
else
bufferFindRev = function(buf, search) {
if(!Buffer.isBuffer(search))
search = toBuffer(search, module.exports.encoding);
if(search.length == 0) return -1;
if(search.length > buf.length) return -1;
for(var i = buf.length-search.length; i >= 0; i--) {
var match = true;
for(var j = 0; j < search.length; j++) {
if(buf[i+j] != search[j]) {
match = false;
break;
}
}
if(match) return i;
}
return -1;
};
var decoderParseLines = function(lines, ydata) {
var warnings = [];
for(var i=0; i<lines.length; i++) {
var yprops = {};
var line = lines[i].substring(2); // cut off '=y'
// parse tag
var p = line.indexOf(' ');
var tag = (p<0 ? line : line.substring(0, p));
line = line.substring(tag.length+1).trim();
// parse props
var m = line.match(RE_YPROP);
while(m) {
if(m.index != 0) {
warnings.push(DecoderWarning('ignored_line_data', 'Unknown additional data ignored: "' + line.substring(0, m.index) + '"'));
}
var prop = m[1], val;
var valPos = m.index + m[0].length;
if(tag == 'begin' && prop == 'name') {
// special treatment of filename - the value is the rest of the line (can include spaces)
val = line.substring(valPos);
line = '';
} else {
p = line.indexOf(' ', valPos);
val = (p<0 ? line.substring(valPos) : line.substring(valPos, p));
line = line.substring(valPos + val.length +1);
}
if(prop in yprops) {
warnings.push(DecoderWarning('duplicate_property', 'Duplicate property encountered: `' + prop + '`'));
}
yprops[prop] = val;
m = line.match(RE_YPROP);
}
if(line != '') {
warnings.push(DecoderWarning('ignored_line_data', 'Unknown additional end-of-line data ignored: "' + line + '"'));
}
if(tag in ydata) {
warnings.push(DecoderWarning('duplicate_line', 'Duplicate line encountered: `' + tag + '`'));
}
ydata[tag] = yprops;
}
return warnings;
};
var propIsNotValidNumber = function(prop) {
return prop && !RE_NUMBER.test(prop);
};
module.exports = {
encoding: 'utf8',
encode: y.encode,
encodeTo: y.encodeTo,
decode: y.decode,
decodeTo: y.decodeTo,
decodeChunk: function(data, output, prev) {
// both output and prev are optional
if(typeof output !== 'undefined' && !Buffer.isBuffer(output)) {
prev = output;
output = null;
}
if(prev === null || typeof prev === 'undefined')
prev = '\r\n';
if(Buffer.isBuffer(prev)) prev = prev.toString();
prev = prev.slice(-4); // only care about the last 4 chars of previous state
if(prev == '\r\n.=') prev = '\r\n='; // aliased after dot stripped
if(data.length == 0) return {
read: 0,
written: 0,
output: toBuffer([]),
ended: 0,
state: prev
};
var state = decodePrev.indexOf(prev);
if(state < 0) {
for(var l=-3; l<0; i++) {
state = decodePrev.indexOf(prev.slice(l));
if(state >= 0) break;
}
if(state < 0) state = decodePrev.indexOf('');
}
var ret = y.decodeIncr(data, state, output);
return {
read: ret.read,
written: ret.written,
output: ret.output || output,
ended: !!ret.ended,
state: [decodePrev[ret.state], '\r\n=y', '\r\n.\r\n'][ret.ended]
};
},
minSize: function(length, line_size) {
if(!length) return 0;
return length // no characters escaped
+ 2 * Math.floor(length / (line_size||128)); // newlines
},
maxSize: function(length, line_size, esc_ratio) {
if(!length) return 0;
if(!esc_ratio && esc_ratio !== 0)
esc_ratio = 2;
else
esc_ratio++;
if(esc_ratio < 1 || esc_ratio > 2)
throw new Error('yEnc escape ratio must be between 0 and 1');
return Math.ceil(length*esc_ratio) // all characters escaped
+ 2 * Math.floor((length*esc_ratio) / (line_size||128)) // newlines, considering the possibility of all chars escaped
+ 2 // allocation for offset and that a newline may occur early
+ 64 // extra space just in case things go awry... just kidding, it's just extra padding to make SIMD logic easier
;
},
crc32: y.crc32,
crc32_combine: y.crc32_combine,
crc32_zeroes: y.crc32_zeroes,
crc32_multiply: y.crc32_multiply,
crc32_shift: y.crc32_shift,
post: function(filename, data, line_size) {
if(!line_size) line_size = 128;
if(!Buffer.isBuffer(data)) data = toBuffer(data);
filename = toBuffer(filename.replace(RE_BADCHAR, '').substring(0, 256), exports.encoding);
var e = encodeCrc(data, line_size);
return Buffer.concat([
toBuffer('=ybegin line='+line_size+' size='+data.length+' name='),
filename, nl,
e.output,
toBuffer('\r\n=yend size='+data.length+' crc32=' + e.crc32.toString('hex'))
]);
},
multi_post: function(filename, size, parts, line_size) {
return new YEncoder(filename, size, parts, line_size);
},
from_post: function(data, isRaw) {
if(!Buffer.isBuffer(data))
throw new TypeError('Expected string or Buffer');
var ret = {};
// find '=ybegin' to know where the yEnc data starts
var yencStart;
if(bufferSlice.call(data, 0, 8).toString('hex') == '3d79626567696e20' /*=ybegin */) {
// common case: starts right at the beginning
yencStart = 0;
} else {
// otherwise, we have to search for the beginning marker
yencStart = bufferFind(data, '\r\n=ybegin ');
if(yencStart < 0)
return DecoderError('no_start_found', 'yEnc start marker not found');
yencStart += 2;
}
ret.yencStart = yencStart;
// find all start lines
var lines = [];
var sp = yencStart;
var p = bufferFind(data, '\r\n', yencStart+8);
while(p > 0) {
var line = bufferSlice.call(data, sp, p).toString(this.encoding).trim();
lines.push(line);
sp = p+2;
if(line.substring(0, 6) == '=yend ') { // no data in post
ret.yencEnd = sp;
break;
}
if(data[sp] != 0x3d /*=*/ || data[sp+1] != 0x79 /*y*/) {
ret.dataStart = sp;
break;
}
p = bufferFind(data, '\r\n', sp);
}
if(!ret.dataStart && !ret.yencEnd) // reached end of data but '=yend' not found
return DecoderError('no_end_found', 'yEnd end marker not found');
var ydata = {};
var warnings = decoderParseLines(lines, ydata);
if(!ret.yencEnd) {
var yencEnd = bufferFindRev(bufferSlice.call(data, ret.dataStart), '\r\n=yend ');
if(yencEnd < 0)
return DecoderError('no_end_found', 'yEnd end marker not found');
yencEnd += ret.dataStart;
ret.dataEnd = yencEnd;
p = bufferFind(data, '\r\n', yencEnd+8);
if(p < 0) {
warnings.push(DecoderWarning('missing_yend_newline', 'No line terminator found for =yend line'));
p = data.length;
ret.yencEnd = p;
} else
ret.yencEnd = p+2;
var endLine = bufferSlice.call(data, yencEnd+2, p).toString(this.encoding).trim();
warnings = warnings.concat(decoderParseLines([endLine], ydata));
}
ret.props = ydata;
// check properties
if(!ydata.begin.line || !ydata.begin.size || !('name' in ydata.begin)) // required properties, according to yEnc 1.2 spec
return DecoderError('missing_required_properties', 'Could not find line/size/name properties on ybegin line');
if(!ydata.end.size)
return DecoderError('missing_required_properties', 'Could not find size properties on yend line');
// check numerical fields
var NUMERICAL_FIELDS = {
begin: ['line', 'total', 'part', 'size'],
part: ['begin', 'end'],
end: ['size', 'part']
};
for(var tag in NUMERICAL_FIELDS) {
if(!ydata[tag]) continue;
NUMERICAL_FIELDS[tag].forEach(function(key) {
if(!ydata[tag][key]) return;
if(!RE_NUMBER.test(ydata[tag][key]))
warnings.push(DecoderWarning('invalid_prop_'+key, '`'+key+'` is not a number'));
else if(ydata[tag][key] === '0' && key != 'size') // most fields have to be at least one
warnings.push(DecoderWarning('zero_prop_'+key, '`'+key+'` cannot be 0'));
});
}
if(ydata.begin.part && ydata.end.part && ydata.begin.part != ydata.end.part)
warnings.push(DecoderWarning('part_number_mismatch', 'Part number specified in begin and end do not match'));
else if(ydata.begin.total && ((ydata.begin.part && +ydata.begin.total < +ydata.begin.part) || (ydata.end.part && +ydata.begin.total < +ydata.end.part)))
warnings.push(DecoderWarning('part_number_exceeds_total', 'Specified part number exceeds specified total'));
var expectedSize = +ydata.end.size;
if(ydata.part && ydata.part.begin && ydata.part.end) {
var partBegin = +ydata.part.begin;
var partEnd = +ydata.part.end;
if(partBegin > partEnd)
warnings.push(DecoderWarning('invalid_part_range', 'begin offset cannot exceed end offset'));
else if(expectedSize != partEnd-partBegin +1)
warnings.push(DecoderWarning('size_mismatch_part_range', 'Specified size does not match part range'));
else if(partEnd > +ydata.begin.size)
warnings.push(DecoderWarning('part_range_exceeds_size', 'Specified part range exceeds total file size'));
} else if(+ydata.begin.total > 1 || +ydata.begin.part > 1 || +ydata.end.part > 1)
warnings.push(DecoderWarning('missing_part_range', 'Part range not specified for multi-part post'));
['pcrc32','crc32'].forEach(function(prop) {
if(ydata.end[prop] && !/^[a-fA-F0-9]{8}$/.test(ydata.end[prop]))
warnings.push(DecoderWarning('invalid_prop_'+prop, '`'+prop+'` is not a valid CRC32 value'));
});
if(!ydata.begin.part && ydata.begin.size != ydata.end.size)
warnings.push(DecoderWarning('size_mismatch', 'Size specified in begin and end do not match'));
else if(+ydata.begin.size < +ydata.end.size)
warnings.push(DecoderWarning('size_mismatch', 'Size specified for part exceeds size specified for whole file'));
if(ret.dataStart) {
ret.data = y.decode(bufferSlice.call(data, ret.dataStart, ret.dataEnd), !!isRaw);
ret.crc32 = y.crc32(ret.data);
var hexCrc = ret.crc32.toString('hex');
if(expectedSize != ret.data.length)
warnings.push(DecoderWarning('data_size_mismatch', 'Decoded data length doesn\'t match size specified in yend'));
if(ydata.end.pcrc32 && hexCrc != ydata.end.pcrc32.toLowerCase())
warnings.push(DecoderWarning('pcrc32_mismatch', 'Specified pcrc32 is invalid'));
if(ydata.end.crc32 && !ydata.part && hexCrc != ydata.end.crc32.toLowerCase())
// if single part, check CRC32 as well
warnings.push(DecoderWarning('crc32_mismatch', 'Specified crc32 is invalid'));
} else {
// empty article
if(expectedSize != 0)
warnings.push(DecoderWarning('data_size_mismatch', 'Decoded data length doesn\'t match size specified in yend'));
if(ydata.end.pcrc32 && ydata.end.pcrc32 != '00000000')
warnings.push(DecoderWarning('pcrc32_mismatch', 'Specified pcrc32 is invalid'));
if(ydata.end.crc32 && !ydata.part && ydata.end.crc32 != '00000000')
warnings.push(DecoderWarning('crc32_mismatch', 'Specified crc32 is invalid'));
}
if(warnings.length)
ret.warnings = warnings;
return ret;
}
};
function YEncoder(filename, size, parts, line_size) {
if(!line_size) line_size = 128;
this.size = size;
this.parts = parts;
this.line_size = line_size;
this.part = 0;
this.pos = 0;
this.crc = toBuffer([0,0,0,0]);
filename = toBuffer(filename.replace(RE_BADCHAR, '').substring(0, 256), exports.encoding);
if(parts > 1) {
this.yInfo = Buffer.concat([
toBuffer(' total='+parts+' line='+line_size+' size='+size+' name='),
filename, nl
]);
} else {
this.yInfo = Buffer.concat([
toBuffer('=ybegin line='+line_size+' size='+size+' name='),
filename, nl
]);
this.encode = this._encodeSingle;
}
}
var singleEncodeError = function() {
throw new Error('Exceeded number of specified yEnc parts');
};
YEncoder.prototype = {
encode: function(data) {
if(!Buffer.isBuffer(data)) data = toBuffer(data);
this.part++;
if(this.part > this.parts)
throw new Error('Exceeded number of specified yEnc parts');
var end = this.pos + data.length;
if(end > this.size)
throw new Error('Exceeded total file size');
var yInfo = this.yInfo;
var crc = y.crc32(data), fullCrc = '';
this.crc = y.crc32_combine(this.crc, crc, data.length);
if(this.part == this.parts) {
// final part treated slightly differently
if(end != this.size)
throw new Error('File size doesn\'t match total data length');
fullCrc = ' crc32='+this.crc.toString('hex');
this.yInfo = null;
}
var ret = Buffer.concat([
toBuffer('=ybegin part='+this.part),
yInfo,
toBuffer('=ypart begin='+( this.pos+1 )+' end='+end+'\r\n'),
y.encode(data, this.line_size),
toBuffer('\r\n=yend size='+data.length+' part='+this.part+' pcrc32='+crc.toString('hex')+fullCrc)
]);
this.pos = end;
return ret;
},
_encodeSingle: function(data) {
if(!Buffer.isBuffer(data)) data = toBuffer(data);
if(this.size != data.length)
throw new Error('File size doesn\'t match total data length');
this.encode = singleEncodeError;
this.part = 1;
this.pos = data.length;
this.crc = y.crc32(data);
var yInfo = this.yInfo;
this.yInfo = null;
return Buffer.concat([
yInfo,
y.encode(data, this.line_size),
toBuffer('\r\n=yend size='+data.length+' crc32=' + this.crc.toString('hex'))
]);
}
};