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 SlowBuffer = process.binding('buffer').SlowBuffer;
23 var assert = require('assert');
25 exports.INSPECT_MAX_BYTES = 50;
27 // Make SlowBuffer inherit from Buffer.
28 // This is an exception to the rule that __proto__ is not allowed in core.
29 SlowBuffer.prototype.__proto__ = Buffer.prototype;
32 function clamp(index, len, defaultValue) {
33 if (typeof index !== 'number') return defaultValue;
34 index = ~~index; // Coerce to integer.
35 if (index >= len) return len;
36 if (index >= 0) return index;
38 if (index >= 0) return index;
44 if (n < 16) return '0' + n.toString(16);
45 return n.toString(16);
49 SlowBuffer.prototype.toString = function(encoding, start, end) {
50 encoding = String(encoding || 'utf8').toLowerCase();
52 if (typeof end !== 'number') end = this.length;
54 // Fastpath empty strings
61 return this.hexSlice(start, end);
65 return this.utf8Slice(start, end);
68 return this.asciiSlice(start, end);
71 return this.binarySlice(start, end);
74 return this.base64Slice(start, end);
80 return this.ucs2Slice(start, end);
83 throw new TypeError('Unknown encoding: ' + encoding);
88 SlowBuffer.prototype.write = function(string, offset, length, encoding) {
89 // Support both (string, offset, length, encoding)
90 // and the legacy (string, encoding, offset, length)
91 if (isFinite(offset)) {
92 if (!isFinite(length)) {
103 offset = +offset || 0;
104 var remaining = this.length - offset;
109 if (length > remaining) {
113 encoding = String(encoding || 'utf8').toLowerCase();
117 return this.hexWrite(string, offset, length);
121 return this.utf8Write(string, offset, length);
124 return this.asciiWrite(string, offset, length);
127 return this.binaryWrite(string, offset, length);
130 return this.base64Write(string, offset, length);
136 return this.ucs2Write(string, offset, length);
139 throw new TypeError('Unknown encoding: ' + encoding);
145 SlowBuffer.prototype.slice = function(start, end) {
146 var len = this.length;
147 start = clamp(start, len, 0);
148 end = clamp(end, len, len);
149 return new Buffer(this, end - start, start);
153 var zeroBuffer = new SlowBuffer(0);
156 function Buffer(subject, encoding, offset) {
157 if (!(this instanceof Buffer)) {
158 return new Buffer(subject, encoding, offset);
164 if (typeof offset === 'number') {
165 if (!Buffer.isBuffer(subject)) {
166 throw new TypeError('First argument must be a Buffer when slicing');
169 this.length = +encoding > 0 ? Math.ceil(encoding) : 0;
170 this.parent = subject.parent ? subject.parent : subject;
171 this.offset = offset;
174 switch (type = typeof subject) {
176 this.length = +subject > 0 ? Math.ceil(subject) : 0;
180 this.length = Buffer.byteLength(subject, encoding);
183 case 'object': // Assume object is array-ish
184 this.length = +subject.length > 0 ? Math.ceil(subject.length) : 0;
188 throw new TypeError('First argument needs to be a number, ' +
192 if (this.length > Buffer.poolSize) {
193 // Big buffer, just alloc one.
194 this.parent = new SlowBuffer(this.length);
197 } else if (this.length > 0) {
199 if (!pool || pool.length - pool.used < this.length) allocPool();
201 this.offset = pool.used;
202 // Align on 8 byte boundary to avoid alignment issues on ARM.
203 pool.used = (pool.used + this.length + 7) & ~7;
206 // Zero-length buffer
207 this.parent = zeroBuffer;
211 // optimize by branching logic for new allocations
212 if (typeof subject !== 'number') {
213 if (type === 'string') {
215 this.length = this.write(subject, 0, encoding);
216 // if subject is buffer then use built-in copy method
217 } else if (Buffer.isBuffer(subject)) {
219 subject.parent.copy(this.parent,
222 this.length + subject.offset);
224 subject.copy(this.parent, this.offset, 0, this.length);
225 } else if (isArrayIsh(subject)) {
226 for (var i = 0; i < this.length; i++)
227 this.parent[i + this.offset] = subject[i];
232 SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
235 function isArrayIsh(subject) {
236 return Array.isArray(subject) ||
237 subject && typeof subject === 'object' &&
238 typeof subject.length === 'number';
241 exports.SlowBuffer = SlowBuffer;
242 exports.Buffer = Buffer;
245 Buffer.isEncoding = function(encoding) {
246 switch (encoding && encoding.toLowerCase()) {
267 Buffer.poolSize = 8 * 1024;
270 function allocPool() {
271 pool = new SlowBuffer(Buffer.poolSize);
277 Buffer.isBuffer = function isBuffer(b) {
278 return b instanceof Buffer;
283 Buffer.prototype.inspect = function inspect() {
286 name = this.constructor.name;
288 for (var i = 0; i < len; i++) {
289 out[i] = toHex(this[i]);
290 if (i == exports.INSPECT_MAX_BYTES) {
296 return '<' + name + ' ' + out.join(' ') + '>';
300 Buffer.prototype.get = function get(offset) {
301 if (offset < 0 || offset >= this.length)
302 throw new RangeError('offset is out of bounds');
303 return this.parent[this.offset + offset];
307 Buffer.prototype.set = function set(offset, v) {
308 if (offset < 0 || offset >= this.length)
309 throw new RangeError('offset is out of bounds');
310 return this.parent[this.offset + offset] = v;
314 // write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8')
315 Buffer.prototype.write = function(string, offset, length, encoding) {
316 // Support both (string, offset, length, encoding)
317 // and the legacy (string, encoding, offset, length)
318 if (isFinite(offset)) {
319 if (!isFinite(length)) {
330 offset = +offset || 0;
331 var remaining = this.length - offset;
336 if (length > remaining) {
340 encoding = String(encoding || 'utf8').toLowerCase();
342 if (string.length > 0 && (length < 0 || offset < 0))
343 throw new RangeError('attempt to write beyond buffer bounds');
348 ret = this.parent.hexWrite(string, this.offset + offset, length);
353 ret = this.parent.utf8Write(string, this.offset + offset, length);
357 ret = this.parent.asciiWrite(string, this.offset + offset, length);
361 ret = this.parent.binaryWrite(string, this.offset + offset, length);
365 // Warning: maxLength not taken into account in base64Write
366 ret = this.parent.base64Write(string, this.offset + offset, length);
373 ret = this.parent.ucs2Write(string, this.offset + offset, length);
377 throw new TypeError('Unknown encoding: ' + encoding);
380 Buffer._charsWritten = SlowBuffer._charsWritten;
386 Buffer.prototype.toJSON = function() {
387 return Array.prototype.slice.call(this, 0);
391 // toString(encoding, start=0, end=buffer.length)
392 Buffer.prototype.toString = function(encoding, start, end) {
393 encoding = String(encoding || 'utf8').toLowerCase();
395 if (typeof start !== 'number' || start < 0) {
397 } else if (start > this.length) {
401 if (typeof end !== 'number' || end > this.length) {
403 } else if (end < 0) {
407 start = start + this.offset;
408 end = end + this.offset;
412 return this.parent.hexSlice(start, end);
416 return this.parent.utf8Slice(start, end);
419 return this.parent.asciiSlice(start, end);
422 return this.parent.binarySlice(start, end);
425 return this.parent.base64Slice(start, end);
431 return this.parent.ucs2Slice(start, end);
434 throw new TypeError('Unknown encoding: ' + encoding);
440 Buffer.byteLength = SlowBuffer.byteLength;
443 // fill(value, start=0, end=buffer.length)
444 Buffer.prototype.fill = function fill(value, start, end) {
445 value || (value = 0);
446 start || (start = 0);
447 end || (end = this.length);
449 if (typeof value === 'string') {
450 value = value.charCodeAt(0);
452 if (typeof value !== 'number' || isNaN(value)) {
453 throw new TypeError('value is not a number');
456 if (end < start) throw new RangeError('end < start');
458 // Fill 0 bytes; we're done
459 if (end === start) return 0;
460 if (this.length == 0) return 0;
462 if (start < 0 || start >= this.length) {
463 throw new RangeError('start out of bounds');
466 if (end < 0 || end > this.length) {
467 throw new RangeError('end out of bounds');
470 return this.parent.fill(value,
476 Buffer.concat = function(list, length) {
477 if (!Array.isArray(list)) {
478 throw new TypeError('Usage: Buffer.concat(list, [length])');
481 if (list.length === 0) {
482 return new Buffer(0);
483 } else if (list.length === 1) {
487 if (typeof length !== 'number') {
489 for (var i = 0; i < list.length; i++) {
491 length += buf.length;
495 var buffer = new Buffer(length);
497 for (var i = 0; i < list.length; i++) {
499 buf.copy(buffer, pos);
508 // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
509 Buffer.prototype.copy = function(target, target_start, start, end) {
510 // set undefined/NaN or out of bounds values equal to their default
511 if (!(target_start >= 0)) target_start = 0;
512 if (!(start >= 0)) start = 0;
513 if (!(end < this.length)) end = this.length;
515 // Copy 0 bytes; we're done
517 target.length === 0 ||
523 throw new RangeError('sourceEnd < sourceStart');
525 if (target_start >= target.length)
526 throw new RangeError('targetStart out of bounds');
528 if (target.length - target_start < end - start)
529 end = target.length - target_start + start;
531 return this.parent.copy(target.parent || target,
532 target_start + (target.offset || 0),
539 Buffer.prototype.slice = function(start, end) {
540 var len = this.length;
541 start = clamp(start, len, 0);
542 end = clamp(end, len, len);
543 return new Buffer(this.parent, end - start, start + this.offset);
547 // Legacy methods for backwards compatibility.
549 Buffer.prototype.utf8Slice = function(start, end) {
550 return this.toString('utf8', start, end);
553 Buffer.prototype.binarySlice = function(start, end) {
554 return this.toString('binary', start, end);
557 Buffer.prototype.asciiSlice = function(start, end) {
558 return this.toString('ascii', start, end);
561 Buffer.prototype.utf8Write = function(string, offset) {
562 return this.write(string, offset, 'utf8');
565 Buffer.prototype.binaryWrite = function(string, offset) {
566 return this.write(string, offset, 'binary');
569 Buffer.prototype.asciiWrite = function(string, offset) {
570 return this.write(string, offset, 'ascii');
575 * Need to make sure that buffer isn't trying to write out of bounds.
576 * This check is far too slow internally for fast buffers.
578 function checkOffset(offset, ext, length) {
579 if ((offset % 1) !== 0 || offset < 0)
580 throw new RangeError('offset is not uint');
581 if (offset + ext > length)
582 throw new RangeError('Trying to access beyond buffer length');
586 Buffer.prototype.readUInt8 = function(offset, noAssert) {
588 checkOffset(offset, 1, this.length);
593 Buffer.prototype.readUInt16LE = function(offset, noAssert) {
595 checkOffset(offset, 2, this.length);
596 return this[offset] | (this[offset + 1] << 8);
600 Buffer.prototype.readUInt16BE = function(offset, noAssert) {
602 checkOffset(offset, 2, this.length);
603 return (this[offset] << 8) | this[offset + 1];
607 Buffer.prototype.readUInt32LE = function(offset, noAssert) {
609 checkOffset(offset, 4, this.length);
611 return ((this[offset]) |
612 (this[offset + 1] << 8) |
613 (this[offset + 2] << 16)) +
614 (this[offset + 3] * 0x1000000);
618 Buffer.prototype.readUInt32BE = function(offset, noAssert) {
620 checkOffset(offset, 4, this.length);
622 return (this[offset] * 0x1000000) +
623 ((this[offset + 1] << 16) |
624 (this[offset + 2] << 8) |
629 Buffer.prototype.readInt8 = function(offset, noAssert) {
631 checkOffset(offset, 1, this.length);
632 if (!(this[offset] & 0x80))
633 return (this[offset]);
634 return ((0xff - this[offset] + 1) * -1);
638 Buffer.prototype.readInt16LE = function(offset, noAssert) {
640 checkOffset(offset, 2, this.length);
641 var val = this[offset] | (this[offset + 1] << 8);
642 return (val & 0x8000) ? val | 0xFFFF0000 : val;
646 Buffer.prototype.readInt16BE = function(offset, noAssert) {
648 checkOffset(offset, 2, this.length);
649 var val = this[offset + 1] | (this[offset] << 8);
650 return (val & 0x8000) ? val | 0xFFFF0000 : val;
654 Buffer.prototype.readInt32LE = function(offset, noAssert) {
656 checkOffset(offset, 4, this.length);
658 return (this[offset]) |
659 (this[offset + 1] << 8) |
660 (this[offset + 2] << 16) |
661 (this[offset + 3] << 24);
665 Buffer.prototype.readInt32BE = function(offset, noAssert) {
667 checkOffset(offset, 4, this.length);
669 return (this[offset] << 24) |
670 (this[offset + 1] << 16) |
671 (this[offset + 2] << 8) |
675 Buffer.prototype.readFloatLE = function(offset, noAssert) {
677 checkOffset(offset, 4, this.length);
678 return this.parent.readFloatLE(this.offset + offset, !!noAssert);
682 Buffer.prototype.readFloatBE = function(offset, noAssert) {
684 checkOffset(offset, 4, this.length);
685 return this.parent.readFloatBE(this.offset + offset, !!noAssert);
689 Buffer.prototype.readDoubleLE = function(offset, noAssert) {
691 checkOffset(offset, 8, this.length);
692 return this.parent.readDoubleLE(this.offset + offset, !!noAssert);
696 Buffer.prototype.readDoubleBE = function(offset, noAssert) {
698 checkOffset(offset, 8, this.length);
699 return this.parent.readDoubleBE(this.offset + offset, !!noAssert);
703 function checkInt(buffer, value, offset, ext, max, min) {
704 if ((value % 1) !== 0 || value > max || value < min)
705 throw TypeError('value is out of bounds');
706 if ((offset % 1) !== 0 || offset < 0)
707 throw TypeError('offset is not uint');
708 if (offset + ext > buffer.length || buffer.length + offset < 0)
709 throw RangeError('Trying to write outside buffer length');
713 Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
715 checkInt(this, value, offset, 1, 0xff, 0);
716 this[offset] = value;
720 Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
722 checkInt(this, value, offset, 2, 0xffff, 0);
723 this[offset] = value;
724 this[offset + 1] = (value >>> 8);
728 Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
730 checkInt(this, value, offset, 2, 0xffff, 0);
731 this[offset] = (value >>> 8);
732 this[offset + 1] = value;
736 Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
738 checkInt(this, value, offset, 4, 0xffffffff, 0);
739 this[offset + 3] = (value >>> 24);
740 this[offset + 2] = (value >>> 16);
741 this[offset + 1] = (value >>> 8);
742 this[offset] = value;
746 Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
748 checkInt(this, value, offset, 4, 0xffffffff, 0);
749 this[offset] = (value >>> 24);
750 this[offset + 1] = (value >>> 16);
751 this[offset + 2] = (value >>> 8);
752 this[offset + 3] = value;
756 Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
758 checkInt(this, value, offset, 1, 0x7f, -0x80);
759 if (value < 0) value = 0xff + value + 1;
760 this[offset] = value;
764 Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
766 checkInt(this, value, offset, 2, 0x7fff, -0x8000);
767 this[offset] = value;
768 this[offset + 1] = (value >>> 8);
772 Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
774 checkInt(this, value, offset, 2, 0x7fff, -0x8000);
775 this[offset] = (value >>> 8);
776 this[offset + 1] = value;
780 Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
782 checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
783 this[offset] = value;
784 this[offset + 1] = (value >>> 8);
785 this[offset + 2] = (value >>> 16);
786 this[offset + 3] = (value >>> 24);
790 Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
792 checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
793 this[offset] = (value >>> 24);
794 this[offset + 1] = (value >>> 16);
795 this[offset + 2] = (value >>> 8);
796 this[offset + 3] = value;
800 Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
802 checkOffset(offset, 4, this.length);
803 this.parent.writeFloatLE(value, this.offset + offset, !!noAssert);
807 Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
809 checkOffset(offset, 4, this.length);
810 this.parent.writeFloatBE(value, this.offset + offset, !!noAssert);
814 Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
816 checkOffset(offset, 8, this.length);
817 this.parent.writeDoubleLE(value, this.offset + offset, !!noAssert);
821 Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
823 checkOffset(offset, 8, this.length);
824 this.parent.writeDoubleBE(value, this.offset + offset, !!noAssert);