Skip to content

Commit 95449ef

Browse files
committed
buffer: use a default offset
If none is provided, use zero as a default offset for all read/write operations on the buffer.
1 parent d1156da commit 95449ef

8 files changed

Lines changed: 107 additions & 55 deletions

lib/internal/buffer.js

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ERR_OUT_OF_RANGE
88
} = require('internal/errors').codes;
99
const { setupBufferJS } = binding;
10+
const { endianness } = require('os');
1011

1112
// Remove from the binding so that function is only available as exported here.
1213
// (That is, for internal use only.)
@@ -19,8 +20,7 @@ const float64Array = new Float64Array(1);
1920
const uInt8Float64Array = new Uint8Array(float64Array.buffer);
2021

2122
// Check endianness.
22-
float32Array[0] = -1;
23-
const bigEndian = uInt8Float32Array[3] === 0;
23+
const bigEndian = endianness === 'BE';
2424

2525
function checkBounds(buf, offset, byteLength) {
2626
checkNumberType(offset);
@@ -57,6 +57,8 @@ function boundsError(value, length, type) {
5757

5858
// Read integers.
5959
function readUIntLE(offset, byteLength) {
60+
if (offset === undefined)
61+
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
6062
if (byteLength === 6)
6163
return readUInt48LE(this, offset);
6264
if (byteLength === 5)
@@ -73,7 +75,7 @@ function readUIntLE(offset, byteLength) {
7375
boundsError(byteLength, 6, 'byteLength');
7476
}
7577

76-
function readUInt48LE(buf, offset) {
78+
function readUInt48LE(buf, offset = 0) {
7779
checkNumberType(offset);
7880
const first = buf[offset];
7981
const last = buf[offset + 5];
@@ -87,7 +89,7 @@ function readUInt48LE(buf, offset) {
8789
(buf[++offset] + last * 2 ** 8) * 2 ** 32;
8890
}
8991

90-
function readUInt40LE(buf, offset) {
92+
function readUInt40LE(buf, offset = 0) {
9193
checkNumberType(offset);
9294
const first = buf[offset];
9395
const last = buf[offset + 4];
@@ -101,7 +103,7 @@ function readUInt40LE(buf, offset) {
101103
last * 2 ** 32;
102104
}
103105

104-
function readUInt32LE(offset) {
106+
function readUInt32LE(offset = 0) {
105107
checkNumberType(offset);
106108
const first = this[offset];
107109
const last = this[offset + 3];
@@ -114,7 +116,7 @@ function readUInt32LE(offset) {
114116
last * 2 ** 24;
115117
}
116118

117-
function readUInt24LE(buf, offset) {
119+
function readUInt24LE(buf, offset = 0) {
118120
checkNumberType(offset);
119121
const first = buf[offset];
120122
const last = buf[offset + 2];
@@ -124,7 +126,7 @@ function readUInt24LE(buf, offset) {
124126
return first + buf[++offset] * 2 ** 8 + last * 2 ** 16;
125127
}
126128

127-
function readUInt16LE(offset) {
129+
function readUInt16LE(offset = 0) {
128130
checkNumberType(offset);
129131
const first = this[offset];
130132
const last = this[offset + 1];
@@ -134,7 +136,7 @@ function readUInt16LE(offset) {
134136
return first + last * 2 ** 8;
135137
}
136138

137-
function readUInt8(offset) {
139+
function readUInt8(offset = 0) {
138140
checkNumberType(offset);
139141
const val = this[offset];
140142
if (val === undefined)
@@ -144,6 +146,8 @@ function readUInt8(offset) {
144146
}
145147

146148
function readUIntBE(offset, byteLength) {
149+
if (offset === undefined)
150+
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
147151
if (byteLength === 6)
148152
return readUInt48BE(this, offset);
149153
if (byteLength === 5)
@@ -160,7 +164,7 @@ function readUIntBE(offset, byteLength) {
160164
boundsError(byteLength, 6, 'byteLength');
161165
}
162166

163-
function readUInt48BE(buf, offset) {
167+
function readUInt48BE(buf, offset = 0) {
164168
checkNumberType(offset);
165169
const first = buf[offset];
166170
const last = buf[offset + 5];
@@ -174,7 +178,7 @@ function readUInt48BE(buf, offset) {
174178
last;
175179
}
176180

177-
function readUInt40BE(buf, offset) {
181+
function readUInt40BE(buf, offset = 0) {
178182
checkNumberType(offset);
179183
const first = buf[offset];
180184
const last = buf[offset + 4];
@@ -188,7 +192,7 @@ function readUInt40BE(buf, offset) {
188192
last;
189193
}
190194

191-
function readUInt32BE(offset) {
195+
function readUInt32BE(offset = 0) {
192196
checkNumberType(offset);
193197
const first = this[offset];
194198
const last = this[offset + 3];
@@ -201,7 +205,7 @@ function readUInt32BE(offset) {
201205
last;
202206
}
203207

204-
function readUInt24BE(buf, offset) {
208+
function readUInt24BE(buf, offset = 0) {
205209
checkNumberType(offset);
206210
const first = buf[offset];
207211
const last = buf[offset + 2];
@@ -211,7 +215,7 @@ function readUInt24BE(buf, offset) {
211215
return first * 2 ** 16 + buf[++offset] * 2 ** 8 + last;
212216
}
213217

214-
function readUInt16BE(offset) {
218+
function readUInt16BE(offset = 0) {
215219
checkNumberType(offset);
216220
const first = this[offset];
217221
const last = this[offset + 1];
@@ -222,6 +226,8 @@ function readUInt16BE(offset) {
222226
}
223227

224228
function readIntLE(offset, byteLength) {
229+
if (offset === undefined)
230+
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
225231
if (byteLength === 6)
226232
return readInt48LE(this, offset);
227233
if (byteLength === 5)
@@ -238,7 +244,7 @@ function readIntLE(offset, byteLength) {
238244
boundsError(byteLength, 6, 'byteLength');
239245
}
240246

241-
function readInt48LE(buf, offset) {
247+
function readInt48LE(buf, offset = 0) {
242248
checkNumberType(offset);
243249
const first = buf[offset];
244250
const last = buf[offset + 5];
@@ -253,7 +259,7 @@ function readInt48LE(buf, offset) {
253259
buf[++offset] * 2 ** 24;
254260
}
255261

256-
function readInt40LE(buf, offset) {
262+
function readInt40LE(buf, offset = 0) {
257263
checkNumberType(offset);
258264
const first = buf[offset];
259265
const last = buf[offset + 4];
@@ -267,7 +273,7 @@ function readInt40LE(buf, offset) {
267273
buf[++offset] * 2 ** 24;
268274
}
269275

270-
function readInt32LE(offset) {
276+
function readInt32LE(offset = 0) {
271277
checkNumberType(offset);
272278
const first = this[offset];
273279
const last = this[offset + 3];
@@ -280,7 +286,7 @@ function readInt32LE(offset) {
280286
(last << 24); // Overflow
281287
}
282288

283-
function readInt24LE(buf, offset) {
289+
function readInt24LE(buf, offset = 0) {
284290
checkNumberType(offset);
285291
const first = buf[offset];
286292
const last = buf[offset + 2];
@@ -291,7 +297,7 @@ function readInt24LE(buf, offset) {
291297
return val | (val & 2 ** 23) * 0x1fe;
292298
}
293299

294-
function readInt16LE(offset) {
300+
function readInt16LE(offset = 0) {
295301
checkNumberType(offset);
296302
const first = this[offset];
297303
const last = this[offset + 1];
@@ -302,7 +308,7 @@ function readInt16LE(offset) {
302308
return val | (val & 2 ** 15) * 0x1fffe;
303309
}
304310

305-
function readInt8(offset) {
311+
function readInt8(offset = 0) {
306312
checkNumberType(offset);
307313
const val = this[offset];
308314
if (val === undefined)
@@ -312,6 +318,8 @@ function readInt8(offset) {
312318
}
313319

314320
function readIntBE(offset, byteLength) {
321+
if (offset === undefined)
322+
throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined);
315323
if (byteLength === 6)
316324
return readInt48BE(this, offset);
317325
if (byteLength === 5)
@@ -328,7 +336,7 @@ function readIntBE(offset, byteLength) {
328336
boundsError(byteLength, 6, 'byteLength');
329337
}
330338

331-
function readInt48BE(buf, offset) {
339+
function readInt48BE(buf, offset = 0) {
332340
checkNumberType(offset);
333341
const first = buf[offset];
334342
const last = buf[offset + 5];
@@ -343,7 +351,7 @@ function readInt48BE(buf, offset) {
343351
last;
344352
}
345353

346-
function readInt40BE(buf, offset) {
354+
function readInt40BE(buf, offset = 0) {
347355
checkNumberType(offset);
348356
const first = buf[offset];
349357
const last = buf[offset + 4];
@@ -357,7 +365,7 @@ function readInt40BE(buf, offset) {
357365
last;
358366
}
359367

360-
function readInt32BE(offset) {
368+
function readInt32BE(offset = 0) {
361369
checkNumberType(offset);
362370
const first = this[offset];
363371
const last = this[offset + 3];
@@ -370,7 +378,7 @@ function readInt32BE(offset) {
370378
last;
371379
}
372380

373-
function readInt24BE(buf, offset) {
381+
function readInt24BE(buf, offset = 0) {
374382
checkNumberType(offset);
375383
const first = buf[offset];
376384
const last = buf[offset + 2];
@@ -381,7 +389,7 @@ function readInt24BE(buf, offset) {
381389
return val | (val & 2 ** 23) * 0x1fe;
382390
}
383391

384-
function readInt16BE(offset) {
392+
function readInt16BE(offset = 0) {
385393
checkNumberType(offset);
386394
const first = this[offset];
387395
const last = this[offset + 1];
@@ -393,7 +401,7 @@ function readInt16BE(offset) {
393401
}
394402

395403
// Read floats
396-
function readFloatBackwards(offset) {
404+
function readFloatBackwards(offset = 0) {
397405
checkNumberType(offset);
398406
const first = this[offset];
399407
const last = this[offset + 3];
@@ -407,7 +415,7 @@ function readFloatBackwards(offset) {
407415
return float32Array[0];
408416
}
409417

410-
function readFloatForwards(offset) {
418+
function readFloatForwards(offset = 0) {
411419
checkNumberType(offset);
412420
const first = this[offset];
413421
const last = this[offset + 3];
@@ -421,7 +429,7 @@ function readFloatForwards(offset) {
421429
return float32Array[0];
422430
}
423431

424-
function readDoubleBackwards(offset) {
432+
function readDoubleBackwards(offset = 0) {
425433
checkNumberType(offset);
426434
const first = this[offset];
427435
const last = this[offset + 7];
@@ -439,7 +447,7 @@ function readDoubleBackwards(offset) {
439447
return float64Array[0];
440448
}
441449

442-
function readDoubleForwards(offset) {
450+
function readDoubleForwards(offset = 0) {
443451
checkNumberType(offset);
444452
const first = this[offset];
445453
const last = this[offset + 7];
@@ -522,7 +530,7 @@ function writeU_Int32LE(buf, value, offset, min, max) {
522530
return offset;
523531
}
524532

525-
function writeUInt32LE(value, offset) {
533+
function writeUInt32LE(value, offset = 0) {
526534
return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
527535
}
528536

@@ -547,7 +555,7 @@ function writeU_Int16LE(buf, value, offset, min, max) {
547555
return offset;
548556
}
549557

550-
function writeUInt16LE(value, offset) {
558+
function writeUInt16LE(value, offset = 0) {
551559
return writeU_Int16LE(this, value, offset, 0, 0xffff);
552560
}
553561

@@ -565,7 +573,7 @@ function writeU_Int8(buf, value, offset, min, max) {
565573
return offset + 1;
566574
}
567575

568-
function writeUInt8(value, offset) {
576+
function writeUInt8(value, offset = 0) {
569577
return writeU_Int8(this, value, offset, 0, 0xff);
570578
}
571579

@@ -632,7 +640,7 @@ function writeU_Int32BE(buf, value, offset, min, max) {
632640
return offset + 4;
633641
}
634642

635-
function writeUInt32BE(value, offset) {
643+
function writeUInt32BE(value, offset = 0) {
636644
return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
637645
}
638646

@@ -657,7 +665,7 @@ function writeU_Int16BE(buf, value, offset, min, max) {
657665
return offset;
658666
}
659667

660-
function writeUInt16BE(value, offset) {
668+
function writeUInt16BE(value, offset = 0) {
661669
return writeU_Int16BE(this, value, offset, 0, 0xffffffff);
662670
}
663671

@@ -678,15 +686,15 @@ function writeIntLE(value, offset, byteLength) {
678686
boundsError(byteLength, 6, 'byteLength');
679687
}
680688

681-
function writeInt32LE(value, offset) {
689+
function writeInt32LE(value, offset = 0) {
682690
return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
683691
}
684692

685-
function writeInt16LE(value, offset) {
693+
function writeInt16LE(value, offset = 0) {
686694
return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
687695
}
688696

689-
function writeInt8(value, offset) {
697+
function writeInt8(value, offset = 0) {
690698
return writeU_Int8(this, value, offset, -0x80, 0x7f);
691699
}
692700

@@ -707,16 +715,16 @@ function writeIntBE(value, offset, byteLength) {
707715
boundsError(byteLength, 6, 'byteLength');
708716
}
709717

710-
function writeInt32BE(value, offset) {
718+
function writeInt32BE(value, offset = 0) {
711719
return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
712720
}
713721

714-
function writeInt16BE(value, offset) {
722+
function writeInt16BE(value, offset = 0) {
715723
return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
716724
}
717725

718726
// Write floats.
719-
function writeDoubleForwards(val, offset) {
727+
function writeDoubleForwards(val, offset = 0) {
720728
val = +val;
721729
checkBounds(this, offset, 7);
722730

@@ -732,7 +740,7 @@ function writeDoubleForwards(val, offset) {
732740
return offset;
733741
}
734742

735-
function writeDoubleBackwards(val, offset) {
743+
function writeDoubleBackwards(val, offset = 0) {
736744
val = +val;
737745
checkBounds(this, offset, 7);
738746

@@ -748,7 +756,7 @@ function writeDoubleBackwards(val, offset) {
748756
return offset;
749757
}
750758

751-
function writeFloatForwards(val, offset) {
759+
function writeFloatForwards(val, offset = 0) {
752760
val = +val;
753761
checkBounds(this, offset, 3);
754762

@@ -760,7 +768,7 @@ function writeFloatForwards(val, offset) {
760768
return offset;
761769
}
762770

763-
function writeFloatBackwards(val, offset) {
771+
function writeFloatBackwards(val, offset = 0) {
764772
val = +val;
765773
checkBounds(this, offset, 3);
766774

0 commit comments

Comments
 (0)