/
packet.js
515 lines (454 loc) · 13 KB
/
packet.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
'use strict';
const Errors = require('../misc/errors');
const Iconv = require('iconv-lite');
const Long = require('long');
const moment = require('moment-timezone');
/**
* Object to easily parse buffer.
*
*/
class Packet {
constructor(buf, pos, end) {
this.buf = buf;
this.pos = pos;
this.end = end;
}
skip(n) {
this.pos += n;
}
readGeometry(dataTypeName) {
const geoBuf = this.readBufferLengthEncoded();
if (geoBuf === null || geoBuf.length === 0) {
if (dataTypeName) {
switch (dataTypeName) {
case 'point':
return { type: 'Point' };
case 'linestring':
return { type: 'LineString' };
case 'polygon':
return { type: 'Polygon' };
case 'multipoint':
return { type: 'MultiPoint' };
case 'multilinestring':
return { type: 'MultiLineString' };
case 'multipolygon':
return { type: 'MultiPolygon' };
default:
return { type: dataTypeName };
}
}
return null;
}
let geoPos = 4;
return readGeometryObject(false);
function parseCoordinates(byteOrder) {
geoPos += 16;
const x = byteOrder ? geoBuf.readDoubleLE(geoPos - 16) : geoBuf.readDoubleBE(geoPos - 16);
const y = byteOrder ? geoBuf.readDoubleLE(geoPos - 8) : geoBuf.readDoubleBE(geoPos - 8);
return [x, y];
}
function readGeometryObject(inner) {
const byteOrder = geoBuf[geoPos++];
const wkbType = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
geoPos += 4;
switch (wkbType) {
case 1: //wkbPoint
const coords = parseCoordinates(byteOrder);
if (inner) return coords;
return {
type: 'Point',
coordinates: coords
};
case 2: //wkbLineString
const pointNumber = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
geoPos += 4;
let coordinates = [];
for (let i = 0; i < pointNumber; i++) {
coordinates.push(parseCoordinates(byteOrder));
}
if (inner) return coordinates;
return {
type: 'LineString',
coordinates: coordinates
};
case 3: //wkbPolygon
let polygonCoordinates = [];
const numRings = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
geoPos += 4;
for (let ring = 0; ring < numRings; ring++) {
const pointNumber = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
geoPos += 4;
let linesCoordinates = [];
for (let i = 0; i < pointNumber; i++) {
linesCoordinates.push(parseCoordinates(byteOrder));
}
polygonCoordinates.push(linesCoordinates);
}
if (inner) return polygonCoordinates;
return {
type: 'Polygon',
coordinates: polygonCoordinates
};
case 4: //wkbMultiPoint
return {
type: 'MultiPoint',
coordinates: parseGeomArray(byteOrder, true)
};
case 5: //wkbMultiLineString
return {
type: 'MultiLineString',
coordinates: parseGeomArray(byteOrder, true)
};
case 6: //wkbMultiPolygon
return {
type: 'MultiPolygon',
coordinates: parseGeomArray(byteOrder, true)
};
case 7: //wkbGeometryCollection
return {
type: 'GeometryCollection',
geometries: parseGeomArray(byteOrder, false)
};
}
return null;
}
function parseGeomArray(byteOrder, inner) {
let coordinates = [];
const number = byteOrder ? geoBuf.readInt32LE(geoPos) : geoBuf.readInt32BE(geoPos);
geoPos += 4;
for (let i = 0; i < number; i++) {
coordinates.push(readGeometryObject(inner));
}
return coordinates;
}
}
peek() {
return this.buf[this.pos];
}
remaining() {
return this.end - this.pos > 0;
}
readUInt8() {
return this.buf[this.pos++];
}
readUInt16() {
return this.buf[this.pos++] + (this.buf[this.pos++] << 8);
}
readUInt24() {
return this.buf[this.pos++] + (this.buf[this.pos++] << 8) + (this.buf[this.pos++] << 16);
}
readUInt32() {
return (
this.buf[this.pos++] +
(this.buf[this.pos++] << 8) +
(this.buf[this.pos++] << 16) +
this.buf[this.pos++] * 0x1000000
);
}
readInt32() {
return (
this.buf[this.pos++] +
(this.buf[this.pos++] << 8) +
(this.buf[this.pos++] << 16) +
(this.buf[this.pos++] << 24)
);
}
readInt32LE() {
return (
(this.buf[this.pos++] << 24) +
(this.buf[this.pos++] << 16) +
(this.buf[this.pos++] << 8) +
this.buf[this.pos++]
);
}
readInt64() {
// could use readBigInt64LE when support would be 10.20+
const val =
this.buf[this.pos + 4] +
this.buf[this.pos + 5] * 2 ** 8 +
this.buf[this.pos + 6] * 2 ** 16 +
(this.buf[this.pos + 7] << 24);
const vv =
(BigInt(val) << BigInt(32)) +
BigInt(
this.buf[this.pos] +
this.buf[this.pos + 1] * 2 ** 8 +
this.buf[this.pos + 2] * 2 ** 16 +
this.buf[this.pos + 3] * 2 ** 24
);
this.pos += 8;
return vv;
}
readUnsignedLength() {
const type = this.buf[this.pos++] & 0xff;
switch (type) {
case 0xfb:
return null;
case 0xfc:
return this.readUInt16();
case 0xfd:
return this.readUInt24();
case 0xfe:
// limitation to BigInt signed value
return Number(this.readInt64());
default:
return type;
}
}
readBuffer(len) {
this.pos += len;
return this.buf.slice(this.pos - len, this.pos);
}
readBufferRemaining() {
let b = this.buf.slice(this.pos, this.end);
this.pos = this.end;
return b;
}
readBufferLengthEncoded() {
const len = this.readUnsignedLength();
if (len === null) return null;
this.pos += len;
return this.buf.slice(this.pos - len, this.pos);
}
readStringNullEnded() {
let initialPosition = this.pos;
let cnt = 0;
while (this.remaining() > 0 && this.buf[this.pos++] !== 0) {
cnt++;
}
return this.buf.toString('utf8', initialPosition, initialPosition + cnt);
}
readSignedLength() {
const type = this.buf[this.pos++];
switch (type) {
case 0xfb:
return null;
case 0xfc:
return this.readUInt16();
case 0xfd:
return this.readUInt24();
case 0xfe:
return Number(this.readInt64());
default:
return type;
}
}
readSignedLengthBigInt() {
const type = this.buf[this.pos++];
switch (type) {
case 0xfb:
return null;
case 0xfc:
return BigInt(this.readUInt16());
case 0xfd:
return BigInt(this.readUInt24());
case 0xfe:
return this.readInt64();
default:
return BigInt(type);
}
}
readAsciiStringLengthEncoded() {
const len = this.readUnsignedLength();
if (len === null) return null;
this.pos += len;
return this.buf.toString('ascii', this.pos - len, this.pos);
}
readStringLength() {
throw new Error(
'code is normally superseded by Node encoder or Iconv depending on charset used'
);
}
readStringLengthEncoded(encoding) {
const len = this.readUnsignedLength();
if (len === null) return null;
this.pos += len;
if (Buffer.isEncoding(encoding)) {
return this.buf.toString(encoding, this.pos - len, this.pos);
}
return Iconv.decode(this.buf.slice(this.pos - len, this.pos), encoding);
}
readLongLengthEncoded(supportBigInt, supportBigNumbers, bigNumberStrings, unsigned) {
const len = this.readUnsignedLength();
if (len === null) return null;
if (supportBigInt) {
const str = this.buf.toString('ascii', this.pos, this.pos + len);
this.pos += len;
return BigInt(str);
}
let result = 0;
let negate = false;
let begin = this.pos;
//minus sign
if (len > 0 && this.buf[begin] === 45) {
negate = true;
begin++;
}
for (; begin < this.pos + len; begin++) {
result = result * 10 + (this.buf[begin] - 48);
}
let val = negate ? -1 * result : result;
this.pos += len;
if (!Number.isSafeInteger(val)) {
const str = this.buf.toString('ascii', this.pos - len, this.pos);
if (bigNumberStrings) return str;
if (supportBigNumbers) {
return Long.fromString(str, unsigned, 10);
}
}
return val;
}
readDecimalLengthEncoded(bigNumberStrings) {
const len = this.readUnsignedLength();
if (len === null) return null;
this.pos += len;
let str = this.buf.toString('ascii', this.pos - len, this.pos);
return bigNumberStrings ? str : +str;
}
readDate() {
const len = this.readUnsignedLength();
if (len === null) return null;
let res = [];
let value = 0;
let initPos = this.pos;
this.pos += len;
while (initPos < this.pos) {
const char = this.buf[initPos++];
if (char === 45) {
//minus separator
res.push(value);
value = 0;
} else {
value = value * 10 + char - 48;
}
}
res.push(value);
//handle zero-date as null
if (res[0] === 0 && res[1] === 0 && res[2] === 0) return null;
return new Date(res[0], res[1] - 1, res[2]);
}
readDateTime(opts) {
const len = this.readUnsignedLength();
if (len === null) return null;
this.pos += len;
const str = this.buf.toString('ascii', this.pos - len, this.pos);
if (str.startsWith('0000-00-00 00:00:00')) return null;
if (opts.tz) {
return new Date(
moment.tz(str, opts.tz).clone().tz(opts._localTz).format('YYYY-MM-DD HH:mm:ss.SSSSSS')
);
}
return new Date(str);
}
readIntLengthEncoded() {
const len = this.readUnsignedLength();
if (len === null) return null;
let result = 0;
let negate = false;
let begin = this.pos;
if (len > 0 && this.buf[begin] === 45) {
//minus sign
negate = true;
begin++;
}
for (; begin < this.pos + len; begin++) {
result = result * 10 + (this.buf[begin] - 48);
}
this.pos += len;
return negate ? -1 * result : result;
}
readFloatLengthCoded() {
const len = this.readUnsignedLength();
if (len === null) return null;
this.pos += len;
return +this.buf.toString('ascii', this.pos - len, this.pos);
}
skipLengthCodedNumber() {
const type = this.buf[this.pos++] & 0xff;
switch (type) {
case 251:
return;
case 252:
this.pos +=
2 + (0xffff & ((this.buf[this.pos] & 0xff) + ((this.buf[this.pos + 1] & 0xff) << 8)));
return;
case 253:
this.pos +=
3 +
(0xffffff &
((this.buf[this.pos] & 0xff) +
((this.buf[this.pos + 1] & 0xff) << 8) +
((this.buf[this.pos + 2] & 0xff) << 16)));
return;
case 254:
this.pos +=
8 +
((this.buf[this.pos] & 0xff) +
((this.buf[this.pos + 1] & 0xff) << 8) +
((this.buf[this.pos + 2] & 0xff) << 16) +
((this.buf[this.pos + 3] & 0xff) << 24) +
((this.buf[this.pos + 4] & 0xff) << 32) +
((this.buf[this.pos + 5] & 0xff) << 40) +
((this.buf[this.pos + 6] & 0xff) << 48) +
((this.buf[this.pos + 7] & 0xff) << 56));
return;
default:
this.pos += type;
return;
}
}
positionFromEnd(num) {
this.pos = this.end - num;
}
/**
* For testing purpose only
*/
_toBuf() {
return this.buf.slice(this.pos, this.end);
}
forceOffset(off) {
this.pos = off;
}
length() {
return this.end - this.pos;
}
subPacketLengthEncoded() {
const len = this.readUnsignedLength();
this.skip(len);
return new Packet(this.buf, this.pos - len, this.pos);
}
/**
* Parse ERR_Packet : https://mariadb.com/kb/en/library/err_packet/
*
* @param info current connection info
* @param sql command sql
* @param stack additional stack trace
* @returns {Error}
*/
readError(info, sql, stack) {
this.skip(1);
let errorCode = this.readUInt16();
let sqlState = '';
if (this.peek() === 0x23) {
this.skip(6);
sqlState = this.buf.toString('utf8', this.pos - 5, this.pos);
}
let msg = this.buf.toString('utf8', this.pos, this.end);
let fatal = sqlState.startsWith('08') || sqlState === '70100';
if (fatal) {
const packetMsgs = info.getLastPackets();
if (packetMsgs !== '')
return Errors.createError(
msg + '\nlast received packets:\n' + packetMsgs,
sql,
fatal,
info,
sqlState,
errorCode,
stack
);
}
return Errors.createError(msg, sql, fatal, info, sqlState, errorCode, stack);
}
}
module.exports = Packet;