Skip to content

Commit

Permalink
Merge pull request #980 from tediousjs/arthur/jsbi
Browse files Browse the repository at this point in the history
Use `jsbi` for internal 64bit integer calculations
  • Loading branch information
arthurschreiber committed Oct 16, 2019
2 parents d83256f + b969541 commit f2336c2
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 309 deletions.
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -42,10 +42,10 @@
"dependencies": {
"@azure/ms-rest-nodeauth": "2.0.2",
"@types/node": "^12.7.11",
"big-number": "1.0.0",
"bl": "^3.0.0",
"depd": "^2.0.0",
"iconv-lite": "^0.5.0",
"jsbi": "^3.1.1",
"native-duplexpair": "^1.0.0",
"punycode": "^2.1.0",
"readable-stream": "^3.4.0",
Expand Down
28 changes: 8 additions & 20 deletions src/ntlm-payload.ts
@@ -1,9 +1,6 @@
import WritableTrackingBuffer from './tracking-buffer/writable-tracking-buffer';
import * as crypto from 'crypto';

const BigInteger = require('big-number');

const hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
import JSBI from 'jsbi';

type Options = {
domain: string,
Expand Down Expand Up @@ -111,24 +108,15 @@ class NTLMResponsePayload {
}

createTimestamp(time: number) {
const tenthsOfAMicrosecond = BigInteger(time).plus(11644473600).multiply(10000000);
const hexArray = [];

let pair = [];
while (tenthsOfAMicrosecond.val() !== '0') {
const idx = tenthsOfAMicrosecond.mod(16);
pair.unshift(hex[idx]);
if (pair.length === 2) {
hexArray.push(pair.join(''));
pair = [];
}
}
const tenthsOfAMicrosecond = JSBI.multiply(JSBI.add(JSBI.BigInt(time), JSBI.BigInt(11644473600)), JSBI.BigInt(10000000));

if (pair.length > 0) {
hexArray.push(pair[0] + '0');
}
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)));

return Buffer.from(hexArray.join(''), 'hex');
const result = Buffer.alloc(8);
result.writeUInt32LE(lo, 0);
result.writeUInt32LE(hi, 4);
return result;
}

lmv2Response(domain: string, user: string, password: string, serverNonce: Buffer, clientNonce: Buffer) {
Expand Down
14 changes: 12 additions & 2 deletions src/token/done-token-parser.ts
@@ -1,3 +1,5 @@
import JSBI from 'jsbi';

import Parser from './stream-parser';
import { ColumnMetadata } from './colmetadata-token-parser';
import { InternalConnectionOptions } from '../connection';
Expand Down Expand Up @@ -33,7 +35,7 @@ function parseToken(parser: Parser, options: InternalConnectionOptions, callback
const serverError = !!(status & STATUS.SRVERROR);

parser.readUInt16LE((curCmd) => {
(options.tdsVersion < '7_2' ? parser.readUInt32LE : parser.readUInt64LE).call(parser, (rowCount) => {
const next = (rowCount: number) => {
callback({
more: more,
sqlError: sqlError,
Expand All @@ -42,7 +44,15 @@ function parseToken(parser: Parser, options: InternalConnectionOptions, callback
rowCount: rowCountValid ? rowCount : undefined,
curCmd: curCmd
});
});
};

if (options.tdsVersion < '7_2') {
parser.readUInt32LE(next);
} else {
parser.readBigUInt64LE((rowCount) => {
next(JSBI.toNumber(rowCount));
});
}
});
});
}
Expand Down
38 changes: 38 additions & 0 deletions src/token/stream-parser.ts
@@ -1,5 +1,6 @@
import Debug from '../debug';
import { InternalConnectionOptions } from '../connection';
import JSBI from 'jsbi';

const Transform = require('readable-stream').Transform;
import { TYPE, Token, EndOfMessageToken, ColMetadataToken } from './token';
Expand Down Expand Up @@ -212,6 +213,32 @@ class Parser extends Transform {
});
}

readBigInt64LE(callback: (data: JSBI) => 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
)
);

this.position += 8;

callback(result);
});
}

readInt64LE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = Math.pow(2, 32) * this.buffer.readInt32LE(this.position + 4) + ((this.buffer[this.position + 4] & 0x80) === 0x80 ? 1 : -1) * this.buffer.readUInt32LE(this.position);
Expand All @@ -228,6 +255,17 @@ class Parser extends Transform {
});
}

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

this.position += 8;

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

readUInt64LE(callback: (data: number) => void) {
this.awaitData(8, () => {
const data = Math.pow(2, 32) * this.buffer.readUInt32LE(this.position + 4) + this.buffer.readUInt32LE(this.position);
Expand Down

0 comments on commit f2336c2

Please sign in to comment.