1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var buffer = process.binding('buffer');
23 var smalloc = process.binding('smalloc');
24 var util = require('util');
25 var alloc = smalloc.alloc;
26 var truncate = smalloc.truncate;
27 var sliceOnto = smalloc.sliceOnto;
28 var kMaxLength = smalloc.kMaxLength;
31 exports.Buffer = Buffer;
32 exports.SlowBuffer = SlowBuffer;
33 exports.INSPECT_MAX_BYTES = 50;
36 Buffer.poolSize = 8 * 1024;
37 var poolSize, poolOffset, allocPool;
40 function createPool() {
41 poolSize = Buffer.poolSize;
42 allocPool = alloc({}, poolSize);
48 function Buffer(subject, encoding) {
49 if (!util.isBuffer(this))
50 return new Buffer(subject, encoding);
52 if (util.isNumber(subject))
53 this.length = subject > 0 ? subject >>> 0 : 0;
54 else if (util.isString(subject))
55 this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
56 else if (util.isObject(subject)) {
57 if (subject.type === 'Buffer' && util.isArray(subject.data))
58 subject = subject.data;
60 this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0;
62 throw new TypeError('must start with number, buffer, array or string');
64 if (this.length > kMaxLength) {
65 throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
66 'size: 0x' + kMaxLength.toString(16) + ' bytes');
69 this.parent = undefined;
70 if (this.length <= (Buffer.poolSize >>> 1) && this.length > 0) {
71 if (this.length > poolSize - poolOffset)
73 this.parent = sliceOnto(allocPool,
76 poolOffset + this.length);
77 poolOffset += this.length;
79 alloc(this, this.length);
82 if (!util.isNumber(subject)) {
83 if (util.isString(subject)) {
84 // In the case of base64 it's possible that the size of the buffer
85 // allocated was slightly too large. In this case we need to rewrite
86 // the length to the actual length written.
87 var len = this.write(subject, encoding);
89 // Buffer was truncated after decode, realloc internal ExternalArray
90 if (len !== this.length) {
92 truncate(this, this.length);
95 if (util.isBuffer(subject))
96 subject.copy(this, 0, 0, this.length);
97 else if (util.isNumber(subject.length) || util.isArray(subject))
98 for (var i = 0; i < this.length; i++)
105 function SlowBuffer(length) {
106 length = length >>> 0;
107 if (length > kMaxLength) {
108 throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
109 'size: 0x' + kMaxLength.toString(16) + ' bytes');
111 var b = new NativeBuffer(length);
117 // Bypass all checks for instantiating unallocated Buffer required for
118 // Objects created in C++. Significantly faster than calling the Buffer
120 function NativeBuffer(length) {
121 this.length = length >>> 0;
122 // Set this to keep the object map the same.
123 this.parent = undefined;
125 NativeBuffer.prototype = Buffer.prototype;
128 // add methods to Buffer prototype
129 buffer.setupBufferJS(NativeBuffer, internal);
134 Buffer.isBuffer = function isBuffer(b) {
135 return util.isBuffer(b);
139 Buffer.compare = function compare(a, b) {
140 if (!(a instanceof Buffer) ||
141 !(b instanceof Buffer))
142 throw new TypeError('Arguments must be Buffers');
144 return internal.compare(a, b);
148 Buffer.isEncoding = function(encoding) {
149 switch ((encoding + '').toLowerCase()) {
169 Buffer.concat = function(list, length) {
170 if (!util.isArray(list))
171 throw new TypeError('Usage: Buffer.concat(list[, length])');
173 if (util.isUndefined(length)) {
175 for (var i = 0; i < list.length; i++)
176 length += list[i].length;
178 length = length >>> 0;
181 if (list.length === 0)
182 return new Buffer(0);
183 else if (list.length === 1)
186 var buffer = new Buffer(length);
188 for (var i = 0; i < list.length; i++) {
190 buf.copy(buffer, pos);
198 Buffer.byteLength = function(str, enc) {
211 ret = str.length * 2;
214 ret = str.length >>> 1;
217 ret = internal.byteLength(str, enc);
223 // toString(encoding, start=0, end=buffer.length)
224 Buffer.prototype.toString = function(encoding, start, end) {
225 var loweredCase = false;
228 end = util.isUndefined(end) || end === Infinity ? this.length : end >>> 0;
230 if (!encoding) encoding = 'utf8';
231 if (start < 0) start = 0;
232 if (end > this.length) end = this.length;
233 if (end <= start) return '';
238 return this.hexSlice(start, end);
242 return this.utf8Slice(start, end);
245 return this.asciiSlice(start, end);
248 return this.binarySlice(start, end);
251 return this.base64Slice(start, end);
257 return this.ucs2Slice(start, end);
261 throw new TypeError('Unknown encoding: ' + encoding);
262 encoding = (encoding + '').toLowerCase();
269 Buffer.prototype.equals = function equals(b) {
270 if (!(b instanceof Buffer))
271 throw new TypeError('Argument must be a Buffer');
273 return internal.compare(this, b) === 0;
278 Buffer.prototype.inspect = function inspect() {
280 var max = exports.INSPECT_MAX_BYTES;
281 if (this.length > 0) {
282 str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
283 if (this.length > max)
286 return '<' + this.constructor.name + ' ' + str + '>';
290 Buffer.prototype.compare = function compare(b) {
291 if (!(b instanceof Buffer))
292 throw new TypeError('Argument must be a Buffer');
294 return internal.compare(this, b);
298 Buffer.prototype.fill = function fill(val, start, end) {
300 end = (end === undefined) ? this.length : end >> 0;
302 if (start < 0 || end > this.length)
303 throw new RangeError('out of range index');
307 if (typeof val !== 'string') {
309 } else if (val.length === 1) {
310 var code = val.charCodeAt(0);
315 internal.fill(this, val, start, end);
321 // XXX remove in v0.13
322 Buffer.prototype.get = util.deprecate(function get(offset) {
324 if (offset < 0 || offset >= this.length)
325 throw new RangeError('index out of range');
327 }, '.get() is deprecated. Access using array indexes instead.');
330 // XXX remove in v0.13
331 Buffer.prototype.set = util.deprecate(function set(offset, v) {
333 if (offset < 0 || offset >= this.length)
334 throw new RangeError('index out of range');
335 return this[offset] = v;
336 }, '.set() is deprecated. Set using array indexes instead.');
339 // TODO(trevnorris): fix these checks to follow new standard
340 // write(string, offset = 0, length = buffer.length, encoding = 'utf8')
341 var writeWarned = false;
342 var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
343 ' Use write(string[, offset[, length]][, encoding]) instead.';
344 Buffer.prototype.write = function(string, offset, length, encoding) {
345 // Buffer#write(string);
346 if (util.isUndefined(offset)) {
348 length = this.length;
351 // Buffer#write(string, encoding)
352 } else if (util.isUndefined(length) && util.isString(offset)) {
354 length = this.length;
357 // Buffer#write(string, offset[, length][, encoding])
358 } else if (isFinite(offset)) {
359 offset = offset >>> 0;
360 if (isFinite(length)) {
361 length = length >>> 0;
362 if (util.isUndefined(encoding))
369 // XXX legacy write(string, encoding, offset, length) - remove in v0.13
372 if (process.throwDeprecation)
373 throw new Error(writeMsg);
374 else if (process.traceDeprecation)
375 console.trace(writeMsg);
377 console.error(writeMsg);
383 offset = length >>> 0;
387 var remaining = this.length - offset;
388 if (util.isUndefined(length) || length > remaining)
391 encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
393 if (string.length > 0 && (length < 0 || offset < 0))
394 throw new RangeError('attempt to write outside buffer bounds');
399 ret = this.hexWrite(string, offset, length);
404 ret = this.utf8Write(string, offset, length);
408 ret = this.asciiWrite(string, offset, length);
412 ret = this.binaryWrite(string, offset, length);
416 // Warning: maxLength not taken into account in base64Write
417 ret = this.base64Write(string, offset, length);
424 ret = this.ucs2Write(string, offset, length);
428 throw new TypeError('Unknown encoding: ' + encoding);
435 Buffer.prototype.toJSON = function() {
438 data: Array.prototype.slice.call(this, 0)
443 // TODO(trevnorris): currently works like Array.prototype.slice(), which
444 // doesn't follow the new standard for throwing on out of range indexes.
445 Buffer.prototype.slice = function(start, end) {
446 var len = this.length;
448 end = util.isUndefined(end) ? len : ~~end;
454 } else if (start > len) {
462 } else if (end > len) {
469 var buf = new NativeBuffer();
470 sliceOnto(this, buf, start, end);
471 buf.length = end - start;
473 buf.parent = util.isUndefined(this.parent) ? this : this.parent;
479 function checkOffset(offset, ext, length) {
480 if (offset + ext > length)
481 throw new RangeError('index out of range');
485 Buffer.prototype.readUIntLE = function(offset, byteLength, noAssert) {
486 offset = offset >>> 0;
487 byteLength = byteLength >>> 0;
489 checkOffset(offset, byteLength, this.length);
491 var val = this[offset];
494 while (++i < byteLength && (mul *= 0x100))
495 val += this[offset + i] * mul;
501 Buffer.prototype.readUIntBE = function(offset, byteLength, noAssert) {
502 offset = offset >>> 0;
503 byteLength = byteLength >>> 0;
505 checkOffset(offset, byteLength, this.length);
507 var val = this[offset + --byteLength];
509 while (byteLength > 0 && (mul *= 0x100))
510 val += this[offset + --byteLength] * mul;
516 Buffer.prototype.readUInt8 = function(offset, noAssert) {
517 offset = offset >>> 0;
519 checkOffset(offset, 1, this.length);
524 Buffer.prototype.readUInt16LE = function(offset, noAssert) {
525 offset = offset >>> 0;
527 checkOffset(offset, 2, this.length);
528 return this[offset] | (this[offset + 1] << 8);
532 Buffer.prototype.readUInt16BE = function(offset, noAssert) {
533 offset = offset >>> 0;
535 checkOffset(offset, 2, this.length);
536 return (this[offset] << 8) | this[offset + 1];
540 Buffer.prototype.readUInt32LE = function(offset, noAssert) {
541 offset = offset >>> 0;
543 checkOffset(offset, 4, this.length);
545 return ((this[offset]) |
546 (this[offset + 1] << 8) |
547 (this[offset + 2] << 16)) +
548 (this[offset + 3] * 0x1000000);
552 Buffer.prototype.readUInt32BE = function(offset, noAssert) {
553 offset = offset >>> 0;
555 checkOffset(offset, 4, this.length);
557 return (this[offset] * 0x1000000) +
558 ((this[offset + 1] << 16) |
559 (this[offset + 2] << 8) |
564 Buffer.prototype.readIntLE = function(offset, byteLength, noAssert) {
565 offset = offset >>> 0;
566 byteLength = byteLength >>> 0;
568 checkOffset(offset, byteLength, this.length);
570 var val = this[offset];
573 while (++i < byteLength && (mul *= 0x100))
574 val += this[offset + i] * mul;
578 val -= Math.pow(2, 8 * byteLength);
584 Buffer.prototype.readIntBE = function(offset, byteLength, noAssert) {
585 offset = offset >>> 0;
586 byteLength = byteLength >>> 0;
588 checkOffset(offset, byteLength, this.length);
592 var val = this[offset + --i];
593 while (i > 0 && (mul *= 0x100))
594 val += this[offset + --i] * mul;
598 val -= Math.pow(2, 8 * byteLength);
604 Buffer.prototype.readInt8 = function(offset, noAssert) {
605 offset = offset >>> 0;
607 checkOffset(offset, 1, this.length);
608 var val = this[offset];
609 return !(val & 0x80) ? val : (0xff - val + 1) * -1;
613 Buffer.prototype.readInt16LE = function(offset, noAssert) {
614 offset = offset >>> 0;
616 checkOffset(offset, 2, this.length);
617 var val = this[offset] | (this[offset + 1] << 8);
618 return (val & 0x8000) ? val | 0xFFFF0000 : val;
622 Buffer.prototype.readInt16BE = function(offset, noAssert) {
623 offset = offset >>> 0;
625 checkOffset(offset, 2, this.length);
626 var val = this[offset + 1] | (this[offset] << 8);
627 return (val & 0x8000) ? val | 0xFFFF0000 : val;
631 Buffer.prototype.readInt32LE = function(offset, noAssert) {
632 offset = offset >>> 0;
634 checkOffset(offset, 4, this.length);
636 return (this[offset]) |
637 (this[offset + 1] << 8) |
638 (this[offset + 2] << 16) |
639 (this[offset + 3] << 24);
643 Buffer.prototype.readInt32BE = function(offset, noAssert) {
644 offset = offset >>> 0;
646 checkOffset(offset, 4, this.length);
648 return (this[offset] << 24) |
649 (this[offset + 1] << 16) |
650 (this[offset + 2] << 8) |
655 Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
656 offset = offset >>> 0;
658 checkOffset(offset, 4, this.length);
659 return internal.readFloatLE(this, offset);
663 Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
664 offset = offset >>> 0;
666 checkOffset(offset, 4, this.length);
667 return internal.readFloatBE(this, offset);
671 Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
672 offset = offset >>> 0;
674 checkOffset(offset, 8, this.length);
675 return internal.readDoubleLE(this, offset);
679 Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
680 offset = offset >>> 0;
682 checkOffset(offset, 8, this.length);
683 return internal.readDoubleBE(this, offset);
687 function checkInt(buffer, value, offset, ext, max, min) {
688 if (!(buffer instanceof Buffer))
689 throw new TypeError('buffer must be a Buffer instance');
690 if (value > max || value < min)
691 throw new TypeError('value is out of bounds');
692 if (offset + ext > buffer.length)
693 throw new RangeError('index out of range');
697 Buffer.prototype.writeUIntLE = function(value, offset, byteLength, noAssert) {
699 offset = offset >>> 0;
700 byteLength = byteLength >>> 0;
702 checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0);
706 this[offset] = value;
707 while (++i < byteLength && (mul *= 0x100))
708 this[offset + i] = (value / mul) >>> 0;
710 return offset + byteLength;
714 Buffer.prototype.writeUIntBE = function(value, offset, byteLength, noAssert) {
716 offset = offset >>> 0;
717 byteLength = byteLength >>> 0;
719 checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0);
721 var i = byteLength - 1;
723 this[offset + i] = value;
724 while (--i >= 0 && (mul *= 0x100))
725 this[offset + i] = (value / mul) >>> 0;
727 return offset + byteLength;
731 Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
733 offset = offset >>> 0;
735 checkInt(this, value, offset, 1, 0xff, 0);
736 this[offset] = value;
741 Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
743 offset = offset >>> 0;
745 checkInt(this, value, offset, 2, 0xffff, 0);
746 this[offset] = value;
747 this[offset + 1] = (value >>> 8);
752 Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
754 offset = offset >>> 0;
756 checkInt(this, value, offset, 2, 0xffff, 0);
757 this[offset] = (value >>> 8);
758 this[offset + 1] = value;
763 Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
765 offset = offset >>> 0;
767 checkInt(this, value, offset, 4, 0xffffffff, 0);
768 this[offset + 3] = (value >>> 24);
769 this[offset + 2] = (value >>> 16);
770 this[offset + 1] = (value >>> 8);
771 this[offset] = value;
776 Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
778 offset = offset >>> 0;
780 checkInt(this, value, offset, 4, 0xffffffff, 0);
781 this[offset] = (value >>> 24);
782 this[offset + 1] = (value >>> 16);
783 this[offset + 2] = (value >>> 8);
784 this[offset + 3] = value;
789 Buffer.prototype.writeIntLE = function(value, offset, byteLength, noAssert) {
791 offset = offset >>> 0;
797 Math.pow(2, 8 * byteLength - 1) - 1,
798 -Math.pow(2, 8 * byteLength - 1));
803 var sub = value < 0 ? 1 : 0;
804 this[offset] = value;
805 while (++i < byteLength && (mul *= 0x100))
806 this[offset + i] = ((value / mul) >> 0) - sub;
808 return offset + byteLength;
812 Buffer.prototype.writeIntBE = function(value, offset, byteLength, noAssert) {
814 offset = offset >>> 0;
820 Math.pow(2, 8 * byteLength - 1) - 1,
821 -Math.pow(2, 8 * byteLength - 1));
824 var i = byteLength - 1;
826 var sub = value < 0 ? 1 : 0;
827 this[offset + i] = value;
828 while (--i >= 0 && (mul *= 0x100))
829 this[offset + i] = ((value / mul) >> 0) - sub;
831 return offset + byteLength;
835 Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
837 offset = offset >>> 0;
839 checkInt(this, value, offset, 1, 0x7f, -0x80);
840 this[offset] = value;
845 Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
847 offset = offset >>> 0;
849 checkInt(this, value, offset, 2, 0x7fff, -0x8000);
850 this[offset] = value;
851 this[offset + 1] = (value >>> 8);
856 Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
858 offset = offset >>> 0;
860 checkInt(this, value, offset, 2, 0x7fff, -0x8000);
861 this[offset] = (value >>> 8);
862 this[offset + 1] = value;
867 Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
869 offset = offset >>> 0;
871 checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
872 this[offset] = value;
873 this[offset + 1] = (value >>> 8);
874 this[offset + 2] = (value >>> 16);
875 this[offset + 3] = (value >>> 24);
880 Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
882 offset = offset >>> 0;
884 checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
885 this[offset] = (value >>> 24);
886 this[offset + 1] = (value >>> 16);
887 this[offset + 2] = (value >>> 8);
888 this[offset + 3] = value;
893 function checkFloat(buffer, value, offset, ext) {
894 if (!(buffer instanceof Buffer))
895 throw new TypeError('buffer must be a Buffer instance');
896 if (offset + ext > buffer.length)
897 throw new RangeError('index out of range');
901 Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) {
903 offset = offset >>> 0;
905 checkFloat(this, val, offset, 4);
906 internal.writeFloatLE(this, val, offset);
911 Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) {
913 offset = offset >>> 0;
915 checkFloat(this, val, offset, 4);
916 internal.writeFloatBE(this, val, offset);
921 Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) {
923 offset = offset >>> 0;
925 checkFloat(this, val, offset, 8);
926 internal.writeDoubleLE(this, val, offset);
931 Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
933 offset = offset >>> 0;
935 checkFloat(this, val, offset, 8);
936 internal.writeDoubleBE(this, val, offset);