Skip to content

Commit

Permalink
feat: replace JSBI with native BigInt (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSun90 authored Aug 17, 2022
1 parent 5015634 commit ece396a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 75 deletions.
7 changes: 3 additions & 4 deletions src/ntlm-payload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import WritableTrackingBuffer from './tracking-buffer/writable-tracking-buffer';
import * as crypto from 'crypto';
import JSBI from 'jsbi';
import md4 from 'js-md4';

interface Options {
Expand Down Expand Up @@ -109,10 +108,10 @@ class NTLMResponsePayload {
}

createTimestamp(time: number) {
const tenthsOfAMicrosecond = JSBI.multiply(JSBI.add(JSBI.BigInt(time), JSBI.BigInt(11644473600)), JSBI.BigInt(10000000));
const tenthsOfAMicrosecond = (BigInt(time) + BigInt(11644473600)) * BigInt(10000000);

const lo = JSBI.toNumber(JSBI.bitwiseAnd(tenthsOfAMicrosecond, JSBI.BigInt(0xffffffff)));
const hi = JSBI.toNumber(JSBI.bitwiseAnd(JSBI.signedRightShift(tenthsOfAMicrosecond, JSBI.BigInt(32)), JSBI.BigInt(0xffffffff)));
const lo = Number(tenthsOfAMicrosecond & BigInt(0xffffffff));
const hi = Number((tenthsOfAMicrosecond >> BigInt(32)) & BigInt(0xffffffff));

const result = Buffer.alloc(8);
result.writeUInt32LE(lo, 0);
Expand Down
4 changes: 1 addition & 3 deletions src/token/done-token-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import JSBI from 'jsbi';

import Parser, { ParserOptions } from './stream-parser';
import { DoneToken, DoneInProcToken, DoneProcToken } from './token';

Expand Down Expand Up @@ -48,7 +46,7 @@ function parseToken(parser: Parser, options: ParserOptions, callback: (data: Tok
parser.readUInt32LE(next);
} else {
parser.readBigUInt64LE((rowCount) => {
next(JSBI.toNumber(rowCount));
next(Number(rowCount));
});
}
});
Expand Down
34 changes: 6 additions & 28 deletions src/token/stream-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Debug from '../debug';
import { InternalConnectionOptions } from '../connection';
import JSBI from 'jsbi';

import { TYPE, Token, ColMetadataToken } from './token';

Expand Down Expand Up @@ -256,29 +255,11 @@ class Parser {
});
}

readBigInt64LE(callback: (data: JSBI) => void) {
readBigInt64LE(callback: (data: bigint) => void) {
this.awaitData(8, () => {
const result = JSBI.add(
JSBI.leftShift(
JSBI.BigInt(
this.buffer[this.position + 4] +
this.buffer[this.position + 5] * 2 ** 8 +
this.buffer[this.position + 6] * 2 ** 16 +
(this.buffer[this.position + 7] << 24) // Overflow
),
JSBI.BigInt(32)
),
JSBI.BigInt(
this.buffer[this.position] +
this.buffer[this.position + 1] * 2 ** 8 +
this.buffer[this.position + 2] * 2 ** 16 +
this.buffer[this.position + 3] * 2 ** 24
)
);

const data = this.buffer.readBigInt64LE(this.position);
this.position += 8;

callback(result);
callback(data);
});
}

Expand All @@ -298,14 +279,11 @@ class Parser {
});
}

readBigUInt64LE(callback: (data: JSBI) => void) {
readBigUInt64LE(callback: (data: bigint) => void) {
this.awaitData(8, () => {
const low = JSBI.BigInt(this.buffer.readUInt32LE(this.position));
const high = JSBI.BigInt(this.buffer.readUInt32LE(this.position + 4));

const data = this.buffer.readBigUInt64LE(this.position);
this.position += 8;

callback(JSBI.add(low, JSBI.leftShift(high, JSBI.BigInt(32))));
callback(data);
});
}

Expand Down
50 changes: 15 additions & 35 deletions src/tracking-buffer/writable-tracking-buffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import JSBI from 'jsbi';

const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
const UNKNOWN_PLP_LEN = Buffer.from([0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
Expand Down Expand Up @@ -106,36 +104,26 @@ class WritableTrackingBuffer {
this.position += length;
}

writeBigInt64LE(value: JSBI) {
this.writeBigU_Int64LE(value);
writeBigInt64LE(value: bigint) {
const length = 8;
this.makeRoomFor(length);
this.buffer.writeBigInt64LE(value, this.position);
this.position += length;
}

private writeBigU_Int64LE(value: JSBI) {
this.makeRoomFor(8);

let lo = JSBI.toNumber(JSBI.bitwiseAnd(value, JSBI.BigInt(0xffffffff)));

this.buffer[this.position++] = lo;
lo = lo >> 8;
this.buffer[this.position++] = lo;
lo = lo >> 8;
this.buffer[this.position++] = lo;
lo = lo >> 8;
this.buffer[this.position++] = lo;

let hi = JSBI.toNumber(JSBI.bitwiseAnd(JSBI.signedRightShift(value, JSBI.BigInt(32)), JSBI.BigInt(0xffffffff)));
writeInt64LE(value: number) {
this.writeBigInt64LE(BigInt(value));
}

this.buffer[this.position++] = hi;
hi = hi >> 8;
this.buffer[this.position++] = hi;
hi = hi >> 8;
this.buffer[this.position++] = hi;
hi = hi >> 8;
this.buffer[this.position++] = hi;
writeUInt64LE(value: number) {
this.writeBigUInt64LE(BigInt(value));
}

writeInt64LE(value: number) {
this.writeBigInt64LE(JSBI.BigInt(value));
writeBigUInt64LE(value: bigint) {
const length = 8;
this.makeRoomFor(length);
this.buffer.writeBigUInt64LE(value, this.position);
this.position += length;
}

writeUInt32BE(value: number) {
Expand All @@ -151,14 +139,6 @@ class WritableTrackingBuffer {
this.writeUInt8(Math.floor(value * SHIFT_RIGHT_32));
}

writeUInt64LE(value: number) {
this.writeBigUInt64LE(JSBI.BigInt(value));
}

writeBigUInt64LE(value: JSBI) {
this.writeBigU_Int64LE(value);
}

writeInt8(value: number) {
const length = 1;
this.makeRoomFor(length);
Expand Down
9 changes: 4 additions & 5 deletions test/unit/tracking-buffer/writable-tracking-buffer-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const TrackingBuffer = require('../../../src/tracking-buffer/writable-tracking-buffer');
const assert = require('chai').assert;
const JSBI = require('jsbi');

function assertBuffer(actual, expected) {
actual = actual.data;
Expand Down Expand Up @@ -116,18 +115,18 @@ describe('Wrtiable Tracking Buffer', () => {
assertBuffer(buffer, [0x03, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00]);
});

it('should write 64-bit signed JSBIs', () => {
it('should write 64-bit signed `BigInt`s', () => {
const buffer = new TrackingBuffer(8);

buffer.writeBigInt64LE(JSBI.BigInt('0x0807060504030201'));
buffer.writeBigInt64LE(BigInt('0x0807060504030201'));

assertBuffer(buffer, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
});

it('should write 64-bit unsigned JSBIs', () => {
it('should write 64-bit unsigned `BigInt`s', () => {
const buffer = new TrackingBuffer(8);

buffer.writeBigUInt64LE(JSBI.BigInt('0xdecafafecacefade'));
buffer.writeBigUInt64LE(BigInt('0xdecafafecacefade'));

assertBuffer(buffer, [0xde, 0xfa, 0xce, 0xca, 0xfe, 0xfa, 0xca, 0xde]);
});
Expand Down

0 comments on commit ece396a

Please sign in to comment.