Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jsbi for internal 64bit integer calculations #980

Merged
merged 5 commits into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If more than Number.MAX_SAFE_INTEGER (9007199254740991) rows are transferred, this conversion might result in an incorrect rowcount. I'm not sure if that's something we have to care about - this limit seems hardly reachable. 🤷‍♂

});
}
});
});
}
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