/
value-parser.js
634 lines (549 loc) · 17.8 KB
/
value-parser.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
const iconv = require('iconv-lite');
const sprintf = require('sprintf-js').sprintf;
const TYPE = require('./data-type').TYPE;
const guidParser = require('./guid-parser');
const readPrecision = require('./metadata-parser').readPrecision;
const readScale = require('./metadata-parser').readScale;
const readCollation = require('./metadata-parser').readCollation;
const NULL = (1 << 16) - 1;
const MAX = (1 << 16) - 1;
const THREE_AND_A_THIRD = 3 + (1 / 3);
const MONEY_DIVISOR = 10000;
const PLP_NULL = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
const UNKNOWN_PLP_LEN = Buffer.from([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
const DEFAULT_ENCODING = 'utf8';
function readTextPointerNull(parser, type, callback) {
parser.readUInt8((textPointerLength) => {
if (textPointerLength !== 0) {
// Appear to be dummy values, so consume and discard them.
parser.readBuffer(textPointerLength, () => {
parser.readBuffer(8, () => {
callback(false);
});
});
} else {
callback(true);
}
});
}
function readDataLength(parser, type, metaData, callback) {
if (metaData.isVariantValue) {
return callback(metaData.dataLength);
}
// s2.2.4.2.1
switch (type.id & 0x30) {
case 0x10: // xx01xxxx - s2.2.4.2.1.1
return callback(0);
case 0x20: // xx10xxxx - s2.2.4.2.1.3
// Variable length
if (metaData.dataLength !== MAX) {
switch (type.dataLengthLength) {
case 0:
return callback(undefined);
case 1:
return parser.readUInt8(callback);
case 2:
return parser.readUInt16LE(callback);
case 4:
return parser.readUInt32LE(callback);
default:
return parser.emit('error', new Error('Unsupported dataLengthLength ' + type.dataLengthLength + ' for data type ' + type.name));
}
} else {
return callback(undefined);
}
case 0x30:
return callback(1 << ((type.id & 0x0C) >> 2));
}
}
module.exports = valueParse;
function valueParse(parser, metaData, options, callback) {
const type = metaData.type;
switch (type.name) {
case 'Null':
return callback(null);
case 'TinyInt':
return parser.readUInt8(callback);
case 'Int':
return parser.readInt32LE(callback);
case 'SmallInt':
return parser.readInt16LE(callback);
case 'BigInt':
return parser.readBigInt64LE((value) => {
callback(value.toString());
});
case 'IntN':
return readDataLength(parser, type, metaData, (dataLength) => {
switch (dataLength) {
case 0:
return callback(null);
case 1:
return parser.readUInt8(callback);
case 2:
return parser.readInt16LE(callback);
case 4:
return parser.readInt32LE(callback);
case 8:
return parser.readBigInt64LE((value) => {
callback(value.toString());
});
default:
return parser.emit('error', new Error('Unsupported dataLength ' + dataLength + ' for IntN'));
}
});
case 'Real':
return parser.readFloatLE(callback);
case 'Float':
return parser.readDoubleLE(callback);
case 'FloatN':
return readDataLength(parser, type, metaData, (dataLength) => {
switch (dataLength) {
case 0:
return callback(null);
case 4:
return parser.readFloatLE(callback);
case 8:
return parser.readDoubleLE(callback);
default:
return parser.emit('error', new Error('Unsupported dataLength ' + dataLength + ' for FloatN'));
}
});
case 'Money':
case 'SmallMoney':
case 'MoneyN':
return readDataLength(parser, type, metaData, (dataLength) => {
switch (dataLength) {
case 0:
return callback(null);
case 4:
return parser.readInt32LE((value) => {
callback(value / MONEY_DIVISOR);
});
case 8:
return parser.readInt32LE((high) => {
parser.readUInt32LE((low) => {
callback((low + (0x100000000 * high)) / MONEY_DIVISOR);
});
});
default:
return parser.emit('error', new Error('Unsupported dataLength ' + dataLength + ' for MoneyN'));
}
});
case 'Bit':
return parser.readUInt8((value) => {
callback(!!value);
});
case 'BitN':
return readDataLength(parser, type, metaData, (dataLength) => {
switch (dataLength) {
case 0:
return callback(null);
case 1:
return parser.readUInt8((value) => {
callback(!!value);
});
default:
return parser.emit('error', new Error('Unsupported dataLength ' + dataLength + ' for BitN'));
}
});
case 'VarChar':
case 'Char':
const codepage = metaData.collation.codepage;
if (metaData.dataLength === MAX) {
return readMaxChars(parser, codepage, callback);
} else {
return readDataLength(parser, type, metaData, (dataLength) => {
readChars(parser, dataLength, codepage, NULL, callback);
});
}
case 'NVarChar':
case 'NChar':
if (metaData.dataLength === MAX) {
return readMaxNChars(parser, callback);
} else {
return readDataLength(parser, type, metaData, (dataLength) => {
readNChars(parser, dataLength, NULL, callback);
});
}
case 'VarBinary':
case 'Binary':
if (metaData.dataLength === MAX) {
return readMaxBinary(parser, callback);
} else {
return readDataLength(parser, type, metaData, (dataLength) => {
readBinary(parser, dataLength, NULL, callback);
});
}
case 'Text':
return readTextPointerNull(parser, type, (textPointerNull) => {
if (textPointerNull) {
return callback(null);
}
readDataLength(parser, type, metaData, (dataLength) => {
readChars(parser, dataLength, metaData.collation.codepage, PLP_NULL, callback);
});
});
case 'NText':
return readTextPointerNull(parser, type, (textPointerNull) => {
if (textPointerNull) {
return callback(null);
}
readDataLength(parser, type, metaData, (dataLength) => {
readNChars(parser, dataLength, PLP_NULL, callback);
});
});
case 'Image':
return readTextPointerNull(parser, type, (textPointerNull) => {
if (textPointerNull) {
return callback(null);
}
readDataLength(parser, type, metaData, (dataLength) => {
readBinary(parser, dataLength, PLP_NULL, callback);
});
});
case 'Xml':
return readDataLength(parser, type, metaData, (dataLength) => {
readMaxNChars(parser, callback);
});
case 'SmallDateTime':
return readSmallDateTime(parser, options.useUTC, callback);
case 'DateTime':
return readDateTime(parser, options.useUTC, callback);
case 'DateTimeN':
return readDataLength(parser, type, metaData, (dataLength) => {
switch (dataLength) {
case 0:
return callback(null);
case 4:
return readSmallDateTime(parser, options.useUTC, callback);
case 8:
return readDateTime(parser, options.useUTC, callback);
default:
return parser.emit('error', new Error('Unsupported dataLength ' + dataLength + ' for DateTimeN'));
}
});
case 'Time':
return readDataLength(parser, type, metaData, (dataLength) => {
if (dataLength === 0) {
return callback(null);
} else {
return readTime(parser, dataLength, metaData.scale, options.useUTC, callback);
}
});
case 'Date':
return readDataLength(parser, type, metaData, (dataLength) => {
if (dataLength === 0) {
return callback(null);
} else {
return readDate(parser, options.useUTC, callback);
}
});
case 'DateTime2':
return readDataLength(parser, type, metaData, (dataLength) => {
if (dataLength === 0) {
return callback(null);
} else {
return readDateTime2(parser, dataLength, metaData.scale, options.useUTC, callback);
}
});
case 'DateTimeOffset':
return readDataLength(parser, type, metaData, (dataLength) => {
if (dataLength === 0) {
return callback(null);
} else {
return readDateTimeOffset(parser, dataLength, metaData.scale, callback);
}
});
case 'NumericN':
case 'DecimalN':
return readDataLength(parser, type, metaData, (dataLength) => {
if (dataLength === 0) {
return callback(null);
} else {
return parser.readUInt8((sign) => {
sign = sign === 1 ? 1 : -1;
let readValue;
switch (dataLength - 1) {
case 4:
readValue = parser.readUInt32LE;
break;
case 8:
readValue = parser.readUNumeric64LE;
break;
case 12:
readValue = parser.readUNumeric96LE;
break;
case 16:
readValue = parser.readUNumeric128LE;
break;
default:
return parser.emit('error', new Error(sprintf('Unsupported numeric size %d', dataLength - 1)));
}
readValue.call(parser, (value) => {
callback((value * sign) / Math.pow(10, metaData.scale));
});
});
}
});
case 'UniqueIdentifier':
return readDataLength(parser, type, metaData, (dataLength) => {
switch (dataLength) {
case 0:
return callback(null);
case 0x10:
return parser.readBuffer(0x10, (data) => {
callback(options.lowerCaseGuids ? guidParser.arrayToLowerCaseGuid(data) : guidParser.arrayToUpperCaseGuid(data));
});
default:
return parser.emit('error', new Error(sprintf('Unsupported guid size %d', dataLength - 1)));
}
});
case 'UDT':
return readDataLength(parser, type, metaData, (dataLength) => {
readMaxBinary(parser, callback);
});
case 'Variant':
return readDataLength(parser, type, metaData, (dataLength) => {
if (dataLength === 0) {
return callback(null);
}
const valueMetaData = metaData.valueMetaData = {};
Object.defineProperty(valueMetaData, 'isVariantValue', { value: true });
return parser.readUInt8((baseType) => {
return parser.readUInt8((propBytes) => {
valueMetaData.dataLength = dataLength - propBytes - 2;
valueMetaData.type = TYPE[baseType];
return readPrecision(parser, valueMetaData.type, (precision) => {
valueMetaData.precision = precision;
return readScale(parser, valueMetaData.type, (scale) => {
valueMetaData.scale = scale;
return readCollation(parser, valueMetaData.type, (collation) => {
valueMetaData.collation = collation;
if (baseType === 0xA5 || baseType === 0xAD || baseType === 0xA7 || baseType === 0xAF || baseType === 0xE7 || baseType === 0xEF) {
return readDataLength(parser, valueMetaData.type, {}, (maxDataLength) => {
// skip the 2-byte max length sent for BIGVARCHRTYPE, BIGCHARTYPE, NVARCHARTYPE, NCHARTYPE, BIGVARBINTYPE and BIGBINARYTYPE types
// and parse based on the length of actual data
return valueParse(parser, valueMetaData, options, callback);
});
} else {
return valueParse(parser, valueMetaData, options, callback);
}
});
});
});
});
});
});
default:
return parser.emit('error', new Error(sprintf('Unrecognised type %s', type.name)));
}
}
function readBinary(parser, dataLength, nullValue, callback) {
if (dataLength === nullValue) {
return callback(null);
} else {
return parser.readBuffer(dataLength, callback);
}
}
function readChars(parser, dataLength, codepage, nullValue, callback) {
if (codepage == null) {
codepage = DEFAULT_ENCODING;
}
if (dataLength === nullValue) {
return callback(null);
} else {
return parser.readBuffer(dataLength, (data) => {
callback(iconv.decode(data, codepage));
});
}
}
function readNChars(parser, dataLength, nullValue, callback) {
if (dataLength === nullValue) {
return callback(null);
} else {
return parser.readBuffer(dataLength, (data) => {
callback(data.toString('ucs2'));
});
}
}
function readMaxBinary(parser, callback) {
return readMax(parser, callback);
}
function readMaxChars(parser, codepage, callback) {
if (codepage == null) {
codepage = DEFAULT_ENCODING;
}
readMax(parser, (data) => {
if (data) {
callback(iconv.decode(data, codepage));
} else {
callback(null);
}
});
}
function readMaxNChars(parser, callback) {
readMax(parser, (data) => {
if (data) {
callback(data.toString('ucs2'));
} else {
callback(null);
}
});
}
function readMax(parser, callback) {
parser.readBuffer(8, (type) => {
if (type.equals(PLP_NULL)) {
return callback(null);
} else if (type.equals(UNKNOWN_PLP_LEN)) {
return readMaxUnknownLength(parser, callback);
} else {
const low = type.readUInt32LE(0);
const high = type.readUInt32LE(4);
if (high >= (2 << (53 - 32))) {
console.warn('Read UInt64LE > 53 bits : high=' + high + ', low=' + low);
}
const expectedLength = low + (0x100000000 * high);
return readMaxKnownLength(parser, expectedLength, callback);
}
});
}
function readMaxKnownLength(parser, totalLength, callback) {
const data = Buffer.alloc(totalLength, 0);
let offset = 0;
function next(done) {
parser.readUInt32LE((chunkLength) => {
if (!chunkLength) {
return done();
}
parser.readBuffer(chunkLength, (chunk) => {
chunk.copy(data, offset);
offset += chunkLength;
next(done);
});
});
}
next(() => {
if (offset !== totalLength) {
parser.emit('error', new Error('Partially Length-prefixed Bytes unmatched lengths : expected ' + totalLength + ', but got ' + offset + ' bytes'));
}
callback(data);
});
}
function readMaxUnknownLength(parser, callback) {
const chunks = [];
let length = 0;
function next(done) {
parser.readUInt32LE((chunkLength) => {
if (!chunkLength) {
return done();
}
parser.readBuffer(chunkLength, (chunk) => {
chunks.push(chunk);
length += chunkLength;
next(done);
});
});
}
next(() => {
callback(Buffer.concat(chunks, length));
});
}
function readSmallDateTime(parser, useUTC, callback) {
parser.readUInt16LE((days) => {
parser.readUInt16LE((minutes) => {
let value;
if (useUTC) {
value = new Date(Date.UTC(1900, 0, 1 + days, 0, minutes));
} else {
value = new Date(1900, 0, 1 + days, 0, minutes);
}
callback(value);
});
});
}
function readDateTime(parser, useUTC, callback) {
parser.readInt32LE((days) => {
parser.readUInt32LE((threeHundredthsOfSecond) => {
const milliseconds = Math.round(threeHundredthsOfSecond * THREE_AND_A_THIRD);
let value;
if (useUTC) {
value = new Date(Date.UTC(1900, 0, 1 + days, 0, 0, 0, milliseconds));
} else {
value = new Date(1900, 0, 1 + days, 0, 0, 0, milliseconds);
}
callback(value);
});
});
}
function readTime(parser, dataLength, scale, useUTC, callback) {
let readValue;
switch (dataLength) {
case 3:
readValue = parser.readUInt24LE;
break;
case 4:
readValue = parser.readUInt32LE;
break;
case 5:
readValue = parser.readUInt40LE;
}
readValue.call(parser, (value) => {
if (scale < 7) {
for (let i = scale; i < 7; i++) {
value *= 10;
}
}
let date;
if (useUTC) {
date = new Date(Date.UTC(1970, 0, 1, 0, 0, 0, value / 10000));
} else {
date = new Date(1970, 0, 1, 0, 0, 0, value / 10000);
}
Object.defineProperty(date, 'nanosecondsDelta', {
enumerable: false,
value: (value % 10000) / Math.pow(10, 7)
});
callback(date);
});
}
function readDate(parser, useUTC, callback) {
parser.readUInt24LE((days) => {
if (useUTC) {
callback(new Date(Date.UTC(2000, 0, days - 730118)));
} else {
callback(new Date(2000, 0, days - 730118));
}
});
}
function readDateTime2(parser, dataLength, scale, useUTC, callback) {
readTime(parser, dataLength - 3, scale, useUTC, (time) => {
parser.readUInt24LE((days) => {
let date;
if (useUTC) {
date = new Date(Date.UTC(2000, 0, days - 730118, 0, 0, 0, +time));
} else {
date = new Date(2000, 0, days - 730118, time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
}
Object.defineProperty(date, 'nanosecondsDelta', {
enumerable: false,
value: time.nanosecondsDelta
});
callback(date);
});
});
}
function readDateTimeOffset(parser, dataLength, scale, callback) {
readTime(parser, dataLength - 5, scale, true, (time) => {
parser.readUInt24LE((days) => {
// offset
parser.readInt16LE(() => {
const date = new Date(Date.UTC(2000, 0, days - 730118, 0, 0, 0, +time));
Object.defineProperty(date, 'nanosecondsDelta', {
enumerable: false,
value: time.nanosecondsDelta
});
callback(date);
});
});
});
}