UNPKG

25.8 kBJavaScriptView Raw
1/* eslint-disable require-buffer */
2'use strict';
3
4const binding = process.binding('buffer');
5const internalUtil = require('internal/util');
6const bindingObj = {};
7
8exports.Buffer = Buffer;
9exports.SlowBuffer = SlowBuffer;
10exports.INSPECT_MAX_BYTES = 50;
11exports.kMaxLength = binding.kMaxLength;
12
13
14Buffer.poolSize = 8 * 1024;
15var poolSize, poolOffset, allocPool;
16
17
18binding.setupBufferJS(Buffer.prototype, bindingObj);
19const flags = bindingObj.flags;
20const kNoZeroFill = 0;
21
22
23function createPool() {
24 poolSize = Buffer.poolSize;
25 if (poolSize > 0)
26 flags[kNoZeroFill] = 1;
27 allocPool = new Uint8Array(poolSize);
28 Object.setPrototypeOf(allocPool, Buffer.prototype);
29 poolOffset = 0;
30}
31createPool();
32
33
34function alignPool() {
35 // Ensure aligned slices
36 if (poolOffset & 0x7) {
37 poolOffset |= 0x7;
38 poolOffset++;
39 }
40}
41
42
43function Buffer(arg) {
44 // Common case.
45 if (typeof arg === 'number') {
46 // If less than zero, or NaN.
47 if (arg < 0 || arg !== arg)
48 arg = 0;
49 return allocate(arg);
50 }
51
52 // Slightly less common case.
53 if (typeof arg === 'string') {
54 return fromString(arg, arguments[1]);
55 }
56
57 // Unusual.
58 return fromObject(arg);
59}
60
61Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
62Object.setPrototypeOf(Buffer, Uint8Array);
63
64
65function SlowBuffer(length) {
66 if (+length != length)
67 length = 0;
68 if (length > 0)
69 flags[kNoZeroFill] = 1;
70 const ui8 = new Uint8Array(+length);
71 Object.setPrototypeOf(ui8, Buffer.prototype);
72 return ui8;
73}
74
75Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
76Object.setPrototypeOf(SlowBuffer, Uint8Array);
77
78
79function allocate(size) {
80 if (size === 0) {
81 const ui8 = new Uint8Array(size);
82 Object.setPrototypeOf(ui8, Buffer.prototype);
83 return ui8;
84 }
85 if (size < (Buffer.poolSize >>> 1)) {
86 if (size > (poolSize - poolOffset))
87 createPool();
88 var b = allocPool.slice(poolOffset, poolOffset + size);
89 poolOffset += size;
90 alignPool();
91 return b;
92 } else {
93 // Even though this is checked above, the conditional is a safety net and
94 // sanity check to prevent any subsequent typed array allocation from not
95 // being zero filled.
96 if (size > 0)
97 flags[kNoZeroFill] = 1;
98 const ui8 = new Uint8Array(size);
99 Object.setPrototypeOf(ui8, Buffer.prototype);
100 return ui8;
101 }
102}
103
104
105function fromString(string, encoding) {
106 if (typeof encoding !== 'string' || encoding === '')
107 encoding = 'utf8';
108
109 var length = byteLength(string, encoding);
110 if (length >= (Buffer.poolSize >>> 1))
111 return binding.createFromString(string, encoding);
112
113 if (length > (poolSize - poolOffset))
114 createPool();
115 var actual = allocPool.write(string, poolOffset, encoding);
116 var b = allocPool.slice(poolOffset, poolOffset + actual);
117 poolOffset += actual;
118 alignPool();
119 return b;
120}
121
122
123function fromObject(obj) {
124 if (obj instanceof Buffer) {
125 var b = allocate(obj.length);
126 obj.copy(b, 0, 0, obj.length);
127 return b;
128 }
129
130 if (Array.isArray(obj)) {
131 var length = obj.length;
132 var b = allocate(length);
133 for (var i = 0; i < length; i++)
134 b[i] = obj[i] & 255;
135 return b;
136 }
137
138 if (obj == null) {
139 throw new TypeError('must start with number, buffer, array or string');
140 }
141
142 if (obj instanceof ArrayBuffer) {
143 return binding.createFromArrayBuffer(obj);
144 }
145
146 if (obj.buffer instanceof ArrayBuffer || obj.length) {
147 var length;
148 if (typeof obj.length !== 'number' || obj.length !== obj.length)
149 length = 0;
150 else
151 length = obj.length;
152 var b = allocate(length);
153 for (var i = 0; i < length; i++) {
154 b[i] = obj[i] & 255;
155 }
156 return b;
157 }
158
159 if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
160 var array = obj.data;
161 var b = allocate(array.length);
162 for (var i = 0; i < array.length; i++)
163 b[i] = array[i] & 255;
164 return b;
165 }
166
167 throw new TypeError('must start with number, buffer, array or string');
168}
169
170
171// Static methods
172
173Buffer.isBuffer = function isBuffer(b) {
174 return b instanceof Buffer;
175};
176
177
178Buffer.compare = function compare(a, b) {
179 if (!(a instanceof Buffer) ||
180 !(b instanceof Buffer)) {
181 throw new TypeError('Arguments must be Buffers');
182 }
183
184 if (a === b) {
185 return 0;
186 }
187
188 return binding.compare(a, b);
189};
190
191
192Buffer.isEncoding = function(encoding) {
193 var loweredCase = false;
194 for (;;) {
195 switch (encoding) {
196 case 'hex':
197 case 'utf8':
198 case 'utf-8':
199 case 'ascii':
200 case 'binary':
201 case 'base64':
202 case 'ucs2':
203 case 'ucs-2':
204 case 'utf16le':
205 case 'utf-16le':
206 case 'raw':
207 return true;
208
209 default:
210 if (loweredCase)
211 return false;
212 encoding = ('' + encoding).toLowerCase();
213 loweredCase = true;
214 }
215 }
216};
217
218
219Buffer.concat = function(list, length) {
220 if (!Array.isArray(list))
221 throw new TypeError('list argument must be an Array of Buffers.');
222
223 if (list.length === 0)
224 return new Buffer(0);
225
226 if (length === undefined) {
227 length = 0;
228 for (var i = 0; i < list.length; i++)
229 length += list[i].length;
230 } else {
231 length = length >>> 0;
232 }
233
234 var buffer = new Buffer(length);
235 var pos = 0;
236 for (var i = 0; i < list.length; i++) {
237 var buf = list[i];
238 buf.copy(buffer, pos);
239 pos += buf.length;
240 }
241
242 return buffer;
243};
244
245
246function base64ByteLength(str, bytes) {
247 // Handle padding
248 if (str.charCodeAt(bytes - 1) === 0x3D)
249 bytes--;
250 if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D)
251 bytes--;
252
253 // Base64 ratio: 3/4
254 return (bytes * 3) >>> 2;
255}
256
257
258function byteLength(string, encoding) {
259 if (typeof string !== 'string')
260 string = '' + string;
261
262 var len = string.length;
263 if (len === 0)
264 return 0;
265
266 // Use a for loop to avoid recursion
267 var loweredCase = false;
268 for (;;) {
269 switch (encoding) {
270 case 'ascii':
271 case 'binary':
272 // Deprecated
273 case 'raw':
274 case 'raws':
275 return len;
276
277 case 'utf8':
278 case 'utf-8':
279 return binding.byteLengthUtf8(string);
280
281 case 'ucs2':
282 case 'ucs-2':
283 case 'utf16le':
284 case 'utf-16le':
285 return len * 2;
286
287 case 'hex':
288 return len >>> 1;
289
290 case 'base64':
291 return base64ByteLength(string, len);
292
293 default:
294 // The C++ binding defaulted to UTF8, we should too.
295 if (loweredCase)
296 return binding.byteLengthUtf8(string);
297
298 encoding = ('' + encoding).toLowerCase();
299 loweredCase = true;
300 }
301 }
302}
303
304Buffer.byteLength = byteLength;
305
306
307// For backwards compatibility.
308Object.defineProperty(Buffer.prototype, 'parent', {
309 enumerable: true,
310 get: function() {
311 if (!(this instanceof Buffer))
312 return undefined;
313 if (this.byteLength === 0 ||
314 this.byteLength === this.buffer.byteLength) {
315 return undefined;
316 }
317 return this.buffer;
318 }
319});
320Object.defineProperty(Buffer.prototype, 'offset', {
321 enumerable: true,
322 get: function() {
323 if (!(this instanceof Buffer))
324 return undefined;
325 return this.byteOffset;
326 }
327});
328
329
330function slowToString(encoding, start, end) {
331 var loweredCase = false;
332
333 start = start >>> 0;
334 end = end === undefined || end === Infinity ? this.length : end >>> 0;
335
336 if (!encoding) encoding = 'utf8';
337 if (start < 0) start = 0;
338 if (end > this.length) end = this.length;
339 if (end <= start) return '';
340
341 while (true) {
342 switch (encoding) {
343 case 'hex':
344 return this.hexSlice(start, end);
345
346 case 'utf8':
347 case 'utf-8':
348 return this.utf8Slice(start, end);
349
350 case 'ascii':
351 return this.asciiSlice(start, end);
352
353 case 'binary':
354 return this.binarySlice(start, end);
355
356 case 'base64':
357 return this.base64Slice(start, end);
358
359 case 'ucs2':
360 case 'ucs-2':
361 case 'utf16le':
362 case 'utf-16le':
363 return this.ucs2Slice(start, end);
364
365 default:
366 if (loweredCase)
367 throw new TypeError('Unknown encoding: ' + encoding);
368 encoding = (encoding + '').toLowerCase();
369 loweredCase = true;
370 }
371 }
372}
373
374
375Buffer.prototype.toString = function() {
376 if (arguments.length === 0) {
377 var result = this.utf8Slice(0, this.length);
378 } else {
379 var result = slowToString.apply(this, arguments);
380 }
381 if (result === undefined)
382 throw new Error('toString failed');
383 return result;
384};
385
386
387Buffer.prototype.equals = function equals(b) {
388 if (!(b instanceof Buffer))
389 throw new TypeError('Argument must be a Buffer');
390
391 if (this === b)
392 return true;
393
394 return binding.compare(this, b) === 0;
395};
396
397
398// Inspect
399Buffer.prototype.inspect = function inspect() {
400 var str = '';
401 var max = exports.INSPECT_MAX_BYTES;
402 if (this.length > 0) {
403 str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
404 if (this.length > max)
405 str += ' ... ';
406 }
407 return '<' + this.constructor.name + ' ' + str + '>';
408};
409
410
411Buffer.prototype.compare = function compare(b) {
412 if (!(b instanceof Buffer))
413 throw new TypeError('Argument must be a Buffer');
414
415 if (this === b)
416 return 0;
417
418 return binding.compare(this, b);
419};
420
421function slowIndexOf(buffer, val, byteOffset, encoding) {
422 var loweredCase = false;
423 for (;;) {
424 switch (encoding) {
425 case 'utf8':
426 case 'utf-8':
427 case 'ucs2':
428 case 'ucs-2':
429 case 'utf16le':
430 case 'utf-16le':
431 case 'binary':
432 return binding.indexOfString(buffer, val, byteOffset, encoding);
433
434 case 'base64':
435 case 'ascii':
436 case 'hex':
437 return binding.indexOfBuffer(
438 buffer, Buffer(val, encoding), byteOffset, encoding);
439
440 default:
441 if (loweredCase) {
442 throw new TypeError('Unknown encoding: ' + encoding);
443 }
444
445 encoding = ('' + encoding).toLowerCase();
446 loweredCase = true;
447 }
448 }
449}
450
451Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
452 if (byteOffset > 0x7fffffff)
453 byteOffset = 0x7fffffff;
454 else if (byteOffset < -0x80000000)
455 byteOffset = -0x80000000;
456 byteOffset >>= 0;
457
458 if (typeof val === 'string') {
459 if (encoding === undefined) {
460 return binding.indexOfString(this, val, byteOffset, encoding);
461 }
462 return slowIndexOf(this, val, byteOffset, encoding);
463 } else if (val instanceof Buffer) {
464 return binding.indexOfBuffer(this, val, byteOffset, encoding);
465 } else if (typeof val === 'number') {
466 return binding.indexOfNumber(this, val, byteOffset);
467 }
468
469 throw new TypeError('val must be string, number or Buffer');
470};
471
472
473Buffer.prototype.fill = function fill(val, start, end) {
474 start = start >> 0;
475 end = (end === undefined) ? this.length : end >> 0;
476
477 if (start < 0 || end > this.length)
478 throw new RangeError('out of range index');
479 if (end <= start)
480 return this;
481
482 if (typeof val !== 'string') {
483 val = val >>> 0;
484 } else if (val.length === 1) {
485 var code = val.charCodeAt(0);
486 if (code < 256)
487 val = code;
488 }
489
490 binding.fill(this, val, start, end);
491
492 return this;
493};
494
495
496// XXX remove in v0.13
497Buffer.prototype.get = internalUtil.deprecate(function get(offset) {
498 offset = ~~offset;
499 if (offset < 0 || offset >= this.length)
500 throw new RangeError('index out of range');
501 return this[offset];
502}, 'Buffer.get is deprecated. Use array indexes instead.');
503
504
505// XXX remove in v0.13
506Buffer.prototype.set = internalUtil.deprecate(function set(offset, v) {
507 offset = ~~offset;
508 if (offset < 0 || offset >= this.length)
509 throw new RangeError('index out of range');
510 return this[offset] = v;
511}, 'Buffer.set is deprecated. Use array indexes instead.');
512
513
514// TODO(trevnorris): fix these checks to follow new standard
515// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
516var writeWarned = false;
517const writeMsg = 'Buffer.write(string, encoding, offset, length) is ' +
518 'deprecated. Use write(string[, offset[, length]]' +
519 '[, encoding]) instead.';
520Buffer.prototype.write = function(string, offset, length, encoding) {
521 // Buffer#write(string);
522 if (offset === undefined) {
523 encoding = 'utf8';
524 length = this.length;
525 offset = 0;
526
527 // Buffer#write(string, encoding)
528 } else if (length === undefined && typeof offset === 'string') {
529 encoding = offset;
530 length = this.length;
531 offset = 0;
532
533 // Buffer#write(string, offset[, length][, encoding])
534 } else if (isFinite(offset)) {
535 offset = offset >>> 0;
536 if (isFinite(length)) {
537 length = length >>> 0;
538 if (encoding === undefined)
539 encoding = 'utf8';
540 } else {
541 encoding = length;
542 length = undefined;
543 }
544
545 // XXX legacy write(string, encoding, offset, length) - remove in v0.13
546 } else {
547 writeWarned = internalUtil.printDeprecationMessage(writeMsg, writeWarned);
548 var swap = encoding;
549 encoding = offset;
550 offset = length >>> 0;
551 length = swap;
552 }
553
554 var remaining = this.length - offset;
555 if (length === undefined || length > remaining)
556 length = remaining;
557
558 if (string.length > 0 && (length < 0 || offset < 0))
559 throw new RangeError('attempt to write outside buffer bounds');
560
561 if (!encoding)
562 encoding = 'utf8';
563
564 var loweredCase = false;
565 for (;;) {
566 switch (encoding) {
567 case 'hex':
568 return this.hexWrite(string, offset, length);
569
570 case 'utf8':
571 case 'utf-8':
572 return this.utf8Write(string, offset, length);
573
574 case 'ascii':
575 return this.asciiWrite(string, offset, length);
576
577 case 'binary':
578 return this.binaryWrite(string, offset, length);
579
580 case 'base64':
581 // Warning: maxLength not taken into account in base64Write
582 return this.base64Write(string, offset, length);
583
584 case 'ucs2':
585 case 'ucs-2':
586 case 'utf16le':
587 case 'utf-16le':
588 return this.ucs2Write(string, offset, length);
589
590 default:
591 if (loweredCase)
592 throw new TypeError('Unknown encoding: ' + encoding);
593 encoding = ('' + encoding).toLowerCase();
594 loweredCase = true;
595 }
596 }
597};
598
599
600Buffer.prototype.toJSON = function() {
601 return {
602 type: 'Buffer',
603 data: Array.prototype.slice.call(this, 0)
604 };
605};
606
607
608// TODO(trevnorris): currently works like Array.prototype.slice(), which
609// doesn't follow the new standard for throwing on out of range indexes.
610Buffer.prototype.slice = function slice(start, end) {
611 const buffer = this.subarray(start, end);
612 Object.setPrototypeOf(buffer, Buffer.prototype);
613 return buffer;
614};
615
616
617function checkOffset(offset, ext, length) {
618 if (offset + ext > length)
619 throw new RangeError('index out of range');
620}
621
622
623Buffer.prototype.readUIntLE = function(offset, byteLength, noAssert) {
624 offset = offset >>> 0;
625 byteLength = byteLength >>> 0;
626 if (!noAssert)
627 checkOffset(offset, byteLength, this.length);
628
629 var val = this[offset];
630 var mul = 1;
631 var i = 0;
632 while (++i < byteLength && (mul *= 0x100))
633 val += this[offset + i] * mul;
634
635 return val;
636};
637
638
639Buffer.prototype.readUIntBE = function(offset, byteLength, noAssert) {
640 offset = offset >>> 0;
641 byteLength = byteLength >>> 0;
642 if (!noAssert)
643 checkOffset(offset, byteLength, this.length);
644
645 var val = this[offset + --byteLength];
646 var mul = 1;
647 while (byteLength > 0 && (mul *= 0x100))
648 val += this[offset + --byteLength] * mul;
649
650 return val;
651};
652
653
654Buffer.prototype.readUInt8 = function(offset, noAssert) {
655 offset = offset >>> 0;
656 if (!noAssert)
657 checkOffset(offset, 1, this.length);
658 return this[offset];
659};
660
661
662Buffer.prototype.readUInt16LE = function(offset, noAssert) {
663 offset = offset >>> 0;
664 if (!noAssert)
665 checkOffset(offset, 2, this.length);
666 return this[offset] | (this[offset + 1] << 8);
667};
668
669
670Buffer.prototype.readUInt16BE = function(offset, noAssert) {
671 offset = offset >>> 0;
672 if (!noAssert)
673 checkOffset(offset, 2, this.length);
674 return (this[offset] << 8) | this[offset + 1];
675};
676
677
678Buffer.prototype.readUInt32LE = function(offset, noAssert) {
679 offset = offset >>> 0;
680 if (!noAssert)
681 checkOffset(offset, 4, this.length);
682
683 return ((this[offset]) |
684 (this[offset + 1] << 8) |
685 (this[offset + 2] << 16)) +
686 (this[offset + 3] * 0x1000000);
687};
688
689
690Buffer.prototype.readUInt32BE = function(offset, noAssert) {
691 offset = offset >>> 0;
692 if (!noAssert)
693 checkOffset(offset, 4, this.length);
694
695 return (this[offset] * 0x1000000) +
696 ((this[offset + 1] << 16) |
697 (this[offset + 2] << 8) |
698 this[offset + 3]);
699};
700
701
702Buffer.prototype.readIntLE = function(offset, byteLength, noAssert) {
703 offset = offset >>> 0;
704 byteLength = byteLength >>> 0;
705 if (!noAssert)
706 checkOffset(offset, byteLength, this.length);
707
708 var val = this[offset];
709 var mul = 1;
710 var i = 0;
711 while (++i < byteLength && (mul *= 0x100))
712 val += this[offset + i] * mul;
713 mul *= 0x80;
714
715 if (val >= mul)
716 val -= Math.pow(2, 8 * byteLength);
717
718 return val;
719};
720
721
722Buffer.prototype.readIntBE = function(offset, byteLength, noAssert) {
723 offset = offset >>> 0;
724 byteLength = byteLength >>> 0;
725 if (!noAssert)
726 checkOffset(offset, byteLength, this.length);
727
728 var i = byteLength;
729 var mul = 1;
730 var val = this[offset + --i];
731 while (i > 0 && (mul *= 0x100))
732 val += this[offset + --i] * mul;
733 mul *= 0x80;
734
735 if (val >= mul)
736 val -= Math.pow(2, 8 * byteLength);
737
738 return val;
739};
740
741
742Buffer.prototype.readInt8 = function(offset, noAssert) {
743 offset = offset >>> 0;
744 if (!noAssert)
745 checkOffset(offset, 1, this.length);
746 var val = this[offset];
747 return !(val & 0x80) ? val : (0xff - val + 1) * -1;
748};
749
750
751Buffer.prototype.readInt16LE = function(offset, noAssert) {
752 offset = offset >>> 0;
753 if (!noAssert)
754 checkOffset(offset, 2, this.length);
755 var val = this[offset] | (this[offset + 1] << 8);
756 return (val & 0x8000) ? val | 0xFFFF0000 : val;
757};
758
759
760Buffer.prototype.readInt16BE = function(offset, noAssert) {
761 offset = offset >>> 0;
762 if (!noAssert)
763 checkOffset(offset, 2, this.length);
764 var val = this[offset + 1] | (this[offset] << 8);
765 return (val & 0x8000) ? val | 0xFFFF0000 : val;
766};
767
768
769Buffer.prototype.readInt32LE = function(offset, noAssert) {
770 offset = offset >>> 0;
771 if (!noAssert)
772 checkOffset(offset, 4, this.length);
773
774 return (this[offset]) |
775 (this[offset + 1] << 8) |
776 (this[offset + 2] << 16) |
777 (this[offset + 3] << 24);
778};
779
780
781Buffer.prototype.readInt32BE = function(offset, noAssert) {
782 offset = offset >>> 0;
783 if (!noAssert)
784 checkOffset(offset, 4, this.length);
785
786 return (this[offset] << 24) |
787 (this[offset + 1] << 16) |
788 (this[offset + 2] << 8) |
789 (this[offset + 3]);
790};
791
792
793Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
794 offset = offset >>> 0;
795 if (!noAssert)
796 checkOffset(offset, 4, this.length);
797 return binding.readFloatLE(this, offset);
798};
799
800
801Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
802 offset = offset >>> 0;
803 if (!noAssert)
804 checkOffset(offset, 4, this.length);
805 return binding.readFloatBE(this, offset);
806};
807
808
809Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
810 offset = offset >>> 0;
811 if (!noAssert)
812 checkOffset(offset, 8, this.length);
813 return binding.readDoubleLE(this, offset);
814};
815
816
817Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
818 offset = offset >>> 0;
819 if (!noAssert)
820 checkOffset(offset, 8, this.length);
821 return binding.readDoubleBE(this, offset);
822};
823
824
825function checkInt(buffer, value, offset, ext, max, min) {
826 if (!(buffer instanceof Buffer))
827 throw new TypeError('buffer must be a Buffer instance');
828 if (value > max || value < min)
829 throw new TypeError('value is out of bounds');
830 if (offset + ext > buffer.length)
831 throw new RangeError('index out of range');
832}
833
834
835Buffer.prototype.writeUIntLE = function(value, offset, byteLength, noAssert) {
836 value = +value;
837 offset = offset >>> 0;
838 byteLength = byteLength >>> 0;
839 if (!noAssert) {
840 const maxBytes = Math.pow(2, 8 * byteLength) - 1;
841 checkInt(this, value, offset, byteLength, maxBytes, 0);
842 }
843
844 var mul = 1;
845 var i = 0;
846 this[offset] = value;
847 while (++i < byteLength && (mul *= 0x100))
848 this[offset + i] = (value / mul) >>> 0;
849
850 return offset + byteLength;
851};
852
853
854Buffer.prototype.writeUIntBE = function(value, offset, byteLength, noAssert) {
855 value = +value;
856 offset = offset >>> 0;
857 byteLength = byteLength >>> 0;
858 if (!noAssert) {
859 const maxBytes = Math.pow(2, 8 * byteLength) - 1;
860 checkInt(this, value, offset, byteLength, maxBytes, 0);
861 }
862
863 var i = byteLength - 1;
864 var mul = 1;
865 this[offset + i] = value;
866 while (--i >= 0 && (mul *= 0x100))
867 this[offset + i] = (value / mul) >>> 0;
868
869 return offset + byteLength;
870};
871
872
873Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
874 value = +value;
875 offset = offset >>> 0;
876 if (!noAssert)
877 checkInt(this, value, offset, 1, 0xff, 0);
878 this[offset] = value;
879 return offset + 1;
880};
881
882
883Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
884 value = +value;
885 offset = offset >>> 0;
886 if (!noAssert)
887 checkInt(this, value, offset, 2, 0xffff, 0);
888 this[offset] = value;
889 this[offset + 1] = (value >>> 8);
890 return offset + 2;
891};
892
893
894Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
895 value = +value;
896 offset = offset >>> 0;
897 if (!noAssert)
898 checkInt(this, value, offset, 2, 0xffff, 0);
899 this[offset] = (value >>> 8);
900 this[offset + 1] = value;
901 return offset + 2;
902};
903
904
905Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
906 value = +value;
907 offset = offset >>> 0;
908 if (!noAssert)
909 checkInt(this, value, offset, 4, 0xffffffff, 0);
910 this[offset + 3] = (value >>> 24);
911 this[offset + 2] = (value >>> 16);
912 this[offset + 1] = (value >>> 8);
913 this[offset] = value;
914 return offset + 4;
915};
916
917
918Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
919 value = +value;
920 offset = offset >>> 0;
921 if (!noAssert)
922 checkInt(this, value, offset, 4, 0xffffffff, 0);
923 this[offset] = (value >>> 24);
924 this[offset + 1] = (value >>> 16);
925 this[offset + 2] = (value >>> 8);
926 this[offset + 3] = value;
927 return offset + 4;
928};
929
930
931Buffer.prototype.writeIntLE = function(value, offset, byteLength, noAssert) {
932 value = +value;
933 offset = offset >>> 0;
934 if (!noAssert) {
935 checkInt(this,
936 value,
937 offset,
938 byteLength,
939 Math.pow(2, 8 * byteLength - 1) - 1,
940 -Math.pow(2, 8 * byteLength - 1));
941 }
942
943 var i = 0;
944 var mul = 1;
945 var sub = value < 0 ? 1 : 0;
946 this[offset] = value;
947 while (++i < byteLength && (mul *= 0x100))
948 this[offset + i] = ((value / mul) >> 0) - sub;
949
950 return offset + byteLength;
951};
952
953
954Buffer.prototype.writeIntBE = function(value, offset, byteLength, noAssert) {
955 value = +value;
956 offset = offset >>> 0;
957 if (!noAssert) {
958 checkInt(this,
959 value,
960 offset,
961 byteLength,
962 Math.pow(2, 8 * byteLength - 1) - 1,
963 -Math.pow(2, 8 * byteLength - 1));
964 }
965
966 var i = byteLength - 1;
967 var mul = 1;
968 var sub = value < 0 ? 1 : 0;
969 this[offset + i] = value;
970 while (--i >= 0 && (mul *= 0x100))
971 this[offset + i] = ((value / mul) >> 0) - sub;
972
973 return offset + byteLength;
974};
975
976
977Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
978 value = +value;
979 offset = offset >>> 0;
980 if (!noAssert)
981 checkInt(this, value, offset, 1, 0x7f, -0x80);
982 this[offset] = value;
983 return offset + 1;
984};
985
986
987Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
988 value = +value;
989 offset = offset >>> 0;
990 if (!noAssert)
991 checkInt(this, value, offset, 2, 0x7fff, -0x8000);
992 this[offset] = value;
993 this[offset + 1] = (value >>> 8);
994 return offset + 2;
995};
996
997
998Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
999 value = +value;
1000 offset = offset >>> 0;
1001 if (!noAssert)
1002 checkInt(this, value, offset, 2, 0x7fff, -0x8000);
1003 this[offset] = (value >>> 8);
1004 this[offset + 1] = value;
1005 return offset + 2;
1006};
1007
1008
1009Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
1010 value = +value;
1011 offset = offset >>> 0;
1012 if (!noAssert)
1013 checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
1014 this[offset] = value;
1015 this[offset + 1] = (value >>> 8);
1016 this[offset + 2] = (value >>> 16);
1017 this[offset + 3] = (value >>> 24);
1018 return offset + 4;
1019};
1020
1021
1022Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
1023 value = +value;
1024 offset = offset >>> 0;
1025 if (!noAssert)
1026 checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
1027 this[offset] = (value >>> 24);
1028 this[offset + 1] = (value >>> 16);
1029 this[offset + 2] = (value >>> 8);
1030 this[offset + 3] = value;
1031 return offset + 4;
1032};
1033
1034
1035function checkFloat(buffer, value, offset, ext) {
1036 if (!(buffer instanceof Buffer))
1037 throw new TypeError('buffer must be a Buffer instance');
1038 if (offset + ext > buffer.length)
1039 throw new RangeError('index out of range');
1040}
1041
1042
1043Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) {
1044 val = +val;
1045 offset = offset >>> 0;
1046 if (!noAssert)
1047 checkFloat(this, val, offset, 4);
1048 binding.writeFloatLE(this, val, offset);
1049 return offset + 4;
1050};
1051
1052
1053Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) {
1054 val = +val;
1055 offset = offset >>> 0;
1056 if (!noAssert)
1057 checkFloat(this, val, offset, 4);
1058 binding.writeFloatBE(this, val, offset);
1059 return offset + 4;
1060};
1061
1062
1063Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) {
1064 val = +val;
1065 offset = offset >>> 0;
1066 if (!noAssert)
1067 checkFloat(this, val, offset, 8);
1068 binding.writeDoubleLE(this, val, offset);
1069 return offset + 8;
1070};
1071
1072
1073Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
1074 val = +val;
1075 offset = offset >>> 0;
1076 if (!noAssert)
1077 checkFloat(this, val, offset, 8);
1078 binding.writeDoubleBE(this, val, offset);
1079 return offset + 8;
1080};