Skip to content

Commit a35ae85

Browse files
committed
Replace magic values for data length with constants
also support 4byte encoding of length in some places
1 parent 0f609d9 commit a35ae85

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

lib/protocol/Writer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,21 +621,21 @@ function createReadStreamError() {
621621
function createBinaryOutBuffer(type, value) {
622622
var length = value.length;
623623
var buffer;
624-
if (length <= 245) {
624+
if (length <= common.DATA_LENGTH_MAX1BYTE_LENGTH) {
625625
buffer = new Buffer(2 + length);
626626
buffer[0] = type;
627627
buffer[1] = length;
628628
value.copy(buffer, 2);
629-
} else if (length <= 32767) {
629+
} else if (length <= common.DATA_LENGTH_MAX2BYTE_LENGTH) {
630630
buffer = new Buffer(4 + length);
631631
buffer[0] = type;
632-
buffer[1] = 246;
632+
buffer[1] = common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR;
633633
buffer.writeInt16LE(length, 2);
634634
value.copy(buffer, 4);
635635
} else {
636636
buffer = new Buffer(6 + length);
637637
buffer[0] = type;
638-
buffer[1] = 247;
638+
buffer[1] = common.DATA_LENGTH_4BYTE_LENGTH_INDICATOR;
639639
buffer.writeInt32LE(length, 2);
640640
value.copy(buffer, 6);
641641
}

lib/protocol/common/Constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module.exports = {
2626
MAX_PACKET_SIZE: Math.pow(2, 17),
2727
MAX_RESULT_SET_SIZE: Math.pow(2, 20),
2828
EMPTY_BUFFER: new Buffer(0),
29+
DATA_LENGTH_MAX1BYTE_LENGTH: 245,
30+
DATA_LENGTH_MAX2BYTE_LENGTH: 32767,
31+
DATA_LENGTH_2BYTE_LENGTH_INDICATOR: 246,
32+
DATA_LENGTH_4BYTE_LENGTH_INDICATOR: 247,
2933
DEFAULT_CONNECT_OPTIONS: [{
3034
name: ConnectOption.CLIENT_LOCALE,
3135
value: 'en_US',

lib/protocol/data/Fields.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// language governing permissions and limitations under the License.
1414
'use strict';
1515

16+
const common = require('../../protocol/common');
1617
var util = require('../../util');
1718

1819
exports.read = read;
@@ -36,9 +37,16 @@ function read(part) {
3637
for (var i = 0; i < numberOfFields; i++) {
3738
fieldLength = buffer[offset];
3839
offset += 1;
39-
if (fieldLength > 245) {
40-
fieldLength = buffer.readUInt16LE(offset);
41-
offset += 2;
40+
if (fieldLength > common.DATA_LENGTH_MAX1BYTE_LENGTH) {
41+
if (fieldLength == common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR) {
42+
fieldLength = buffer.readUInt16LE(offset);
43+
offset += 2;
44+
} else if (fieldLength == common.DATA_LENGTH_4BYTE_LENGTH_INDICATOR) {
45+
fieldLength = buffer.readUInt32LE(offset);
46+
offset += 4;
47+
} else {
48+
throw Error("Unsupported length indicator: " + fieldLength);
49+
}
4250
}
4351
fields.push(buffer.slice(offset, offset + fieldLength));
4452
offset += fieldLength;
@@ -76,14 +84,19 @@ function write(part, fields) {
7684
data = new Buffer(field, 'ascii');
7785
}
7886
fieldLength = data.length;
79-
if (fieldLength <= 245) {
87+
if (fieldLength <= common.DATA_LENGTH_MAX1BYTE_LENGTH) {
8088
buffer[offset] = fieldLength;
8189
offset += 1;
82-
} else {
83-
buffer[offset] = 0xf6;
90+
} else if (fieldLength <= common.DATA_LENGTH_MAX2BYTE_LENGTH) {
91+
buffer[offset] = common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR;
8492
offset += 1;
8593
buffer.writeUInt16LE(fieldLength, offset);
8694
offset += 2;
95+
} else {
96+
buffer[offset] = common.DATA_LENGTH_4BYTE_LENGTH_INDICATOR;
97+
offset += 1;
98+
buffer.writeInt32LE(fieldLength, offset);
99+
offset += 4;
87100
}
88101
data.copy(buffer, offset);
89102
offset += fieldLength;
@@ -98,10 +111,12 @@ function getByteLength(fields) {
98111
var fieldLength;
99112
for (var i = 0; i < fields.length; i++) {
100113
fieldLength = getByteLengthOfField(fields[i]);
101-
if (fieldLength <= 245) {
114+
if (fieldLength <= common.DATA_LENGTH_MAX1BYTE_LENGTH) {
102115
byteLength += fieldLength + 1;
103-
} else {
116+
} else if (fieldLength <= common.DATA_LENGTH_MAX2BYTE_LENGTH) {
104117
byteLength += fieldLength + 3;
118+
} else {
119+
byteLength += fieldLength + 5;
105120
}
106121
}
107122
return byteLength;

lib/protocol/data/TextList.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'use strict';
1515

1616
var util = require('../../util');
17+
var common = require('../common');
1718

1819
exports.write = write;
1920
exports.getByteLength = getByteLength;
@@ -34,11 +35,11 @@ function write(part, fields) {
3435
data = util.convert.encode(field, part.useCesu8);
3536

3637
fieldLength = data.length;
37-
if (fieldLength <= 245) {
38+
if (fieldLength <= common.DATA_LENGTH_MAX1BYTE_LENGTH) {
3839
buffer[offset] = fieldLength;
3940
offset += 1;
4041
} else {
41-
buffer[offset] = 0xf6;
42+
buffer[offset] = common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR;
4243
offset += 1;
4344
buffer.writeUInt16LE(fieldLength, offset);
4445
offset += 2;
@@ -57,7 +58,7 @@ function getByteLength(fields, useCesu8) {
5758
var fieldLength;
5859
for (var i = 0; i < fields.length; i++) {
5960
fieldLength = getByteLengthOfField(fields[i], useCesu8);
60-
if (fieldLength <= 245) {
61+
if (fieldLength <= common.DATA_LENGTH_MAX1BYTE_LENGTH) {
6162
byteLength += fieldLength + 1;
6263
} else {
6364
byteLength += fieldLength + 3;

test/data.Fields.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ describe('Data', function () {
4646
];
4747
var largeBuffer = Buffer.concat([
4848
new Buffer([2, 0]),
49-
new Buffer([246, 0, 1]),
49+
new Buffer([lib.common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR, 0, 1]),
5050
largeFields[0],
51-
new Buffer([246, 0, 1]),
51+
new Buffer([lib.common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR, 0, 1]),
5252
largeFields[1]
5353
]);
5454

test/lib.Writer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,18 @@ describe('Lib', function () {
216216
value = lorem.SHORT.toString('ascii');
217217
writer.setValues([value]);
218218
writer.length.should.equal(length + 4);
219-
writer._buffers[0][1].should.equal(246);
219+
writer._buffers[0][1].should.equal(lib.common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR);
220220
// long
221221
length = lorem.LONG.length;
222222
value = lorem.LONG.toString('ascii');
223223
writer.setValues([value]);
224224
writer.length.should.equal(length + 6);
225-
writer._buffers[0][1].should.equal(247);
225+
writer._buffers[0][1].should.equal(lib.common.DATA_LENGTH_4BYTE_LENGTH_INDICATOR);
226226
// unicode char
227227
value = 'ä';
228228
length = value.length;
229229
writer.setValues([value]);
230-
writer.length.should.equal(4);
230+
writer.length.should.equal(4);
231231
writer._buffers[0][2].should.equal(0xC3);
232232
writer._buffers[0][3].should.equal(0xA4);
233233
});
@@ -246,13 +246,13 @@ describe('Lib', function () {
246246
length = value.length;
247247
writer.setValues([value]);
248248
writer.length.should.equal(length + 4);
249-
writer._buffers[0][1].should.equal(246);
249+
writer._buffers[0][1].should.equal(lib.common.DATA_LENGTH_2BYTE_LENGTH_INDICATOR);
250250
// long
251251
value = lorem.LONG;
252252
length = value.length;
253253
writer.setValues([value]);
254254
writer.length.should.equal(length + 6);
255-
writer._buffers[0][1].should.equal(247);
255+
writer._buffers[0][1].should.equal(lib.common.DATA_LENGTH_4BYTE_LENGTH_INDICATOR);
256256
});
257257

258258
it('should get Parameters where buffer excatly fits', function (

0 commit comments

Comments
 (0)