2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
var buffer = process.binding('buffer');
|
2013-07-13 00:18:21 +02:00
|
|
|
var smalloc = process.binding('smalloc');
|
2011-05-01 20:38:10 +02:00
|
|
|
var assert = require('assert');
|
2013-04-27 01:58:18 +02:00
|
|
|
var util = require('util');
|
2013-04-18 01:26:15 +02:00
|
|
|
var alloc = smalloc.alloc;
|
|
|
|
var sliceOnto = smalloc.sliceOnto;
|
|
|
|
var kMaxLength = smalloc.kMaxLength;
|
2010-03-20 04:50:29 +01:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
exports.Buffer = Buffer;
|
2013-04-18 01:29:14 +02:00
|
|
|
exports.SlowBuffer = SlowBuffer;
|
2011-08-09 04:04:24 +02:00
|
|
|
exports.INSPECT_MAX_BYTES = 50;
|
2011-08-09 02:50:23 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
// add methods to Buffer prototype
|
|
|
|
buffer.setupBufferJS(Buffer);
|
2012-05-06 21:47:25 +02:00
|
|
|
|
2013-04-18 01:29:14 +02:00
|
|
|
Buffer.poolSize = 8 * 1024;
|
|
|
|
var poolSize = Buffer.poolSize;
|
|
|
|
var poolOffset = 0;
|
|
|
|
var allocPool = alloc({}, poolSize);
|
|
|
|
|
|
|
|
|
|
|
|
function createPool() {
|
|
|
|
poolSize = Buffer.poolSize;
|
|
|
|
allocPool = alloc({}, poolSize);
|
|
|
|
poolOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
function Buffer(subject, encoding) {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isBuffer(this))
|
2013-04-18 01:26:15 +02:00
|
|
|
return new Buffer(subject, encoding);
|
2010-09-04 22:48:04 +02:00
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isNumber(subject))
|
2013-07-24 18:03:53 +02:00
|
|
|
this.length = subject > 0 ? Math.floor(subject) : 0;
|
2013-07-26 23:38:08 +02:00
|
|
|
else if (util.isString(subject))
|
2013-07-24 18:03:53 +02:00
|
|
|
this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
|
2013-07-26 23:38:08 +02:00
|
|
|
else if (util.isObject(subject))
|
2013-07-24 18:03:53 +02:00
|
|
|
this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0;
|
2013-07-26 23:38:08 +02:00
|
|
|
else if (util.isUndefined(subject)) {
|
2013-04-18 01:26:15 +02:00
|
|
|
// undef first arg returns unallocated buffer, also assumes length passed.
|
|
|
|
// this is a stop-gap for now while look for better architecture.
|
|
|
|
// for internal use only.
|
2013-07-24 18:03:53 +02:00
|
|
|
this.length = encoding;
|
|
|
|
return;
|
2010-07-21 00:23:30 +02:00
|
|
|
}
|
2013-07-24 18:03:53 +02:00
|
|
|
else
|
|
|
|
throw new TypeError('must start with number, buffer, array or string');
|
2010-03-26 16:35:46 +01:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
if (this.length > kMaxLength)
|
|
|
|
throw new RangeError('length > kMaxLength');
|
2010-04-09 01:31:02 +02:00
|
|
|
|
2013-04-18 01:29:14 +02:00
|
|
|
if (this.length < Buffer.poolSize / 2 && this.length > 0) {
|
|
|
|
if (this.length > poolSize - poolOffset)
|
|
|
|
createPool();
|
2013-07-23 19:43:48 +02:00
|
|
|
this.parent = sliceOnto(allocPool,
|
|
|
|
this,
|
|
|
|
poolOffset,
|
|
|
|
poolOffset + this.length);
|
2013-04-18 01:29:14 +02:00
|
|
|
poolOffset += this.length;
|
|
|
|
} else {
|
|
|
|
alloc(this, this.length);
|
|
|
|
}
|
2010-04-09 01:31:02 +02:00
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isNumber(subject)) {
|
|
|
|
if (util.isString(subject)) {
|
2013-04-18 01:26:15 +02:00
|
|
|
// FIXME: the number of bytes hasn't changed, so why change the length?
|
|
|
|
this.length = this.write(subject, 0, encoding);
|
|
|
|
} else {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isBuffer(subject))
|
2013-04-18 01:26:15 +02:00
|
|
|
this.copy(subject, 0, 0, this.length);
|
2013-07-26 23:38:08 +02:00
|
|
|
else if (util.isNumber(subject.length) || util.isArray(subject))
|
2013-04-18 01:26:15 +02:00
|
|
|
for (var i = 0; i < this.length; i++)
|
|
|
|
this[i] = subject[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-09 01:31:02 +02:00
|
|
|
|
2010-07-23 22:52:44 +02:00
|
|
|
|
2013-04-18 01:29:14 +02:00
|
|
|
function SlowBuffer(length) {
|
|
|
|
length = ~~length;
|
|
|
|
var b = new Buffer(undefined, length);
|
|
|
|
alloc(b, length);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
// Static methods
|
2011-02-06 21:49:52 +01:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
Buffer.isBuffer = function isBuffer(b) {
|
2013-07-26 23:38:08 +02:00
|
|
|
return util.isBuffer(b);
|
2010-03-20 04:50:29 +01:00
|
|
|
};
|
|
|
|
|
2010-09-04 22:48:04 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
Buffer.isEncoding = function(encoding) {
|
|
|
|
switch ((encoding + '').toLowerCase()) {
|
2011-02-20 02:29:01 +01:00
|
|
|
case 'hex':
|
2010-04-09 01:31:02 +02:00
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
|
|
|
case 'ascii':
|
|
|
|
case 'binary':
|
2010-07-24 01:36:47 +02:00
|
|
|
case 'base64':
|
2011-02-06 21:49:52 +01:00
|
|
|
case 'ucs2':
|
|
|
|
case 'ucs-2':
|
2012-05-03 16:55:25 +02:00
|
|
|
case 'utf16le':
|
|
|
|
case 'utf-16le':
|
2013-04-18 01:26:15 +02:00
|
|
|
case 'raw':
|
|
|
|
return true;
|
2011-02-06 21:49:52 +01:00
|
|
|
|
2010-04-09 01:31:02 +02:00
|
|
|
default:
|
2013-04-18 01:26:15 +02:00
|
|
|
return false;
|
2010-04-09 01:31:02 +02:00
|
|
|
}
|
2010-08-20 08:29:06 +02:00
|
|
|
};
|
|
|
|
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
Buffer.concat = function(list, length) {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isArray(list))
|
2013-04-18 01:26:15 +02:00
|
|
|
throw new TypeError('Usage: Buffer.concat(list[, length])');
|
2012-06-18 11:03:32 +02:00
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isUndefined(length)) {
|
2013-04-18 01:26:15 +02:00
|
|
|
length = 0;
|
|
|
|
for (var i = 0; i < list.length; i++)
|
|
|
|
length += list[i].length;
|
2010-08-21 08:28:00 +02:00
|
|
|
} else {
|
2013-04-18 01:26:15 +02:00
|
|
|
length = ~~length;
|
|
|
|
}
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
if (length < 0) length = 0;
|
2010-09-08 01:29:53 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
if (list.length === 0)
|
|
|
|
return new Buffer(0);
|
|
|
|
else if (list.length === 1)
|
|
|
|
return list[0];
|
2013-01-05 22:14:50 +01:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
var buffer = new Buffer(length);
|
|
|
|
var pos = 0;
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
var buf = list[i];
|
|
|
|
buf.copy(buffer, pos);
|
|
|
|
pos += buf.length;
|
2010-08-21 08:28:00 +02:00
|
|
|
}
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
return buffer;
|
|
|
|
};
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2011-02-01 09:06:44 +01:00
|
|
|
|
2013-07-09 05:58:39 +02:00
|
|
|
// pre-set for values that may exist in the future
|
|
|
|
Buffer.prototype.length = undefined;
|
|
|
|
Buffer.prototype.parent = undefined;
|
|
|
|
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
|
|
Buffer.prototype.toString = function(encoding, start, end) {
|
|
|
|
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
start = ~~start;
|
2013-07-26 23:38:08 +02:00
|
|
|
end = util.isUndefined(end) ? this.length : ~~end;
|
2012-08-27 21:31:14 +02:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
if (start < 0) start = 0;
|
|
|
|
if (end > this.length) end = this.length;
|
|
|
|
if (end <= start) return '';
|
|
|
|
|
|
|
|
switch (encoding) {
|
2012-08-27 21:31:14 +02:00
|
|
|
case 'hex':
|
2013-04-18 01:26:15 +02:00
|
|
|
return this.hexSlice(start, end);
|
|
|
|
|
2012-08-27 21:31:14 +02:00
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
2013-04-18 01:26:15 +02:00
|
|
|
return this.utf8Slice(start, end);
|
|
|
|
|
2012-08-27 21:31:14 +02:00
|
|
|
case 'ascii':
|
2013-04-18 01:26:15 +02:00
|
|
|
return this.asciiSlice(start, end);
|
|
|
|
|
2012-08-27 21:31:14 +02:00
|
|
|
case 'binary':
|
2013-04-18 01:26:15 +02:00
|
|
|
return this.binarySlice(start, end);
|
|
|
|
|
2012-08-27 21:31:14 +02:00
|
|
|
case 'base64':
|
2013-04-18 01:26:15 +02:00
|
|
|
return this.base64Slice(start, end);
|
|
|
|
|
2012-08-27 21:31:14 +02:00
|
|
|
case 'ucs2':
|
|
|
|
case 'ucs-2':
|
|
|
|
case 'utf16le':
|
|
|
|
case 'utf-16le':
|
2013-04-18 01:26:15 +02:00
|
|
|
return this.ucs2Slice(start, end);
|
2012-08-27 21:31:14 +02:00
|
|
|
|
|
|
|
default:
|
2013-04-18 01:26:15 +02:00
|
|
|
throw new TypeError('Unknown encoding: ' + encoding);
|
2012-08-27 21:31:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-08-21 08:28:00 +02:00
|
|
|
// Inspect
|
2010-08-21 13:18:15 +02:00
|
|
|
Buffer.prototype.inspect = function inspect() {
|
2013-04-18 01:26:15 +02:00
|
|
|
var str = '';
|
|
|
|
if (this.length > 0)
|
|
|
|
str = this.hexSlice(0, this.length).match(/.{2}/g).join(' ');
|
|
|
|
return '<' + this.constructor.name + ' ' + str + '>';
|
2010-08-21 08:28:00 +02:00
|
|
|
};
|
|
|
|
|
2010-09-04 22:48:04 +02:00
|
|
|
|
2013-04-27 01:58:18 +02:00
|
|
|
// XXX remove in v0.13
|
|
|
|
Buffer.prototype.get = util.deprecate(function get(offset) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-22 20:10:11 +01:00
|
|
|
if (offset < 0 || offset >= this.length)
|
2013-04-18 01:26:15 +02:00
|
|
|
throw new RangeError('index out of range');
|
|
|
|
return this[offset];
|
2013-04-27 01:58:18 +02:00
|
|
|
}, '.get() is deprecated. Access using array indexes instead.');
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2010-09-04 22:48:04 +02:00
|
|
|
|
2013-04-27 01:58:18 +02:00
|
|
|
// XXX remove in v0.13
|
|
|
|
Buffer.prototype.set = util.deprecate(function set(offset, v) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-22 20:10:11 +01:00
|
|
|
if (offset < 0 || offset >= this.length)
|
2013-04-18 01:26:15 +02:00
|
|
|
throw new RangeError('index out of range');
|
|
|
|
return this[offset] = v;
|
2013-04-27 01:58:18 +02:00
|
|
|
}, '.set() is deprecated. Set using array indexes instead.');
|
2010-08-21 08:28:00 +02:00
|
|
|
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
// TODO(trevnorris): fix these checks to follow new standard
|
|
|
|
// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
|
2013-04-27 01:58:18 +02:00
|
|
|
var writeWarned = false;
|
|
|
|
var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
|
|
|
|
' Use write(string, offset, length, encoding) instead.';
|
2011-07-21 19:53:11 +02:00
|
|
|
Buffer.prototype.write = function(string, offset, length, encoding) {
|
2013-04-27 01:58:18 +02:00
|
|
|
// allow write(string, encoding)
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isString(offset) && util.isUndefined(length)) {
|
2013-04-27 01:58:18 +02:00
|
|
|
encoding = offset;
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
// allow write(string, offset[, length], encoding)
|
|
|
|
} else if (isFinite(offset)) {
|
|
|
|
offset = ~~offset;
|
|
|
|
if (isFinite(length)) {
|
|
|
|
length = ~~length;
|
|
|
|
} else {
|
2011-07-21 19:53:11 +02:00
|
|
|
encoding = length;
|
|
|
|
length = undefined;
|
|
|
|
}
|
2013-04-27 01:58:18 +02:00
|
|
|
|
|
|
|
// XXX legacy write(string, encoding, offset, length) - remove in v0.13
|
|
|
|
} else {
|
|
|
|
if (!writeWarned) {
|
|
|
|
if (process.throwDeprecation)
|
|
|
|
throw new Error(writeMsg);
|
|
|
|
else if (process.traceDeprecation)
|
|
|
|
console.trace(writeMsg);
|
|
|
|
else
|
|
|
|
console.error(writeMsg);
|
|
|
|
writeWarned = true;
|
|
|
|
}
|
|
|
|
|
2010-08-21 08:28:00 +02:00
|
|
|
var swap = encoding;
|
|
|
|
encoding = offset;
|
2013-04-27 01:58:18 +02:00
|
|
|
offset = ~~length;
|
2011-07-21 19:53:11 +02:00
|
|
|
length = swap;
|
2010-08-21 08:28:00 +02:00
|
|
|
}
|
|
|
|
|
2011-07-21 19:53:11 +02:00
|
|
|
var remaining = this.length - offset;
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isUndefined(length) || length > remaining)
|
2011-07-21 19:53:11 +02:00
|
|
|
length = remaining;
|
2013-04-18 01:26:15 +02:00
|
|
|
|
2013-04-27 01:58:18 +02:00
|
|
|
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2013-05-20 23:37:55 +02:00
|
|
|
if (string.length > 0 && (length < 0 || offset < 0))
|
|
|
|
throw new RangeError('attempt to write beyond buffer bounds');
|
|
|
|
|
2010-09-06 12:07:32 +02:00
|
|
|
var ret;
|
2010-09-05 22:30:30 +02:00
|
|
|
switch (encoding) {
|
2011-02-20 02:29:01 +01:00
|
|
|
case 'hex':
|
2013-04-18 01:26:15 +02:00
|
|
|
ret = this.hexWrite(string, offset, length);
|
2011-02-20 02:29:01 +01:00
|
|
|
break;
|
|
|
|
|
2010-09-05 22:30:30 +02:00
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
2013-04-18 01:26:15 +02:00
|
|
|
ret = this.utf8Write(string, offset, length);
|
2010-09-06 12:07:32 +02:00
|
|
|
break;
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2010-09-05 22:30:30 +02:00
|
|
|
case 'ascii':
|
2013-04-18 01:26:15 +02:00
|
|
|
ret = this.asciiWrite(string, offset, length);
|
2010-09-06 12:07:32 +02:00
|
|
|
break;
|
2010-09-05 22:30:30 +02:00
|
|
|
|
|
|
|
case 'binary':
|
2013-04-18 01:26:15 +02:00
|
|
|
ret = this.binaryWrite(string, offset, length);
|
2010-09-06 12:07:32 +02:00
|
|
|
break;
|
2010-09-05 22:30:30 +02:00
|
|
|
|
|
|
|
case 'base64':
|
2010-09-06 12:07:32 +02:00
|
|
|
// Warning: maxLength not taken into account in base64Write
|
2013-04-18 01:26:15 +02:00
|
|
|
ret = this.base64Write(string, offset, length);
|
2010-09-06 12:07:32 +02:00
|
|
|
break;
|
2010-09-05 22:30:30 +02:00
|
|
|
|
2011-02-06 21:49:52 +01:00
|
|
|
case 'ucs2':
|
|
|
|
case 'ucs-2':
|
2012-05-03 16:55:25 +02:00
|
|
|
case 'utf16le':
|
|
|
|
case 'utf-16le':
|
2013-04-18 01:26:15 +02:00
|
|
|
ret = this.ucs2Write(string, offset, length);
|
2011-02-06 21:49:52 +01:00
|
|
|
break;
|
|
|
|
|
2010-09-05 22:30:30 +02:00
|
|
|
default:
|
2013-01-22 20:10:11 +01:00
|
|
|
throw new TypeError('Unknown encoding: ' + encoding);
|
2010-09-05 22:30:30 +02:00
|
|
|
}
|
2010-09-06 12:07:32 +02:00
|
|
|
|
|
|
|
return ret;
|
2010-09-04 22:48:04 +02:00
|
|
|
};
|
|
|
|
|
2010-08-21 08:28:00 +02:00
|
|
|
|
2012-09-09 01:10:00 +02:00
|
|
|
Buffer.prototype.toJSON = function() {
|
2013-03-26 16:14:52 +01:00
|
|
|
return {
|
|
|
|
type: 'Buffer',
|
|
|
|
data: Array.prototype.slice.call(this, 0)
|
|
|
|
};
|
2012-09-09 01:10:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
// TODO(trevnorris): currently works like Array.prototype.slice(), which
|
|
|
|
// doesn't follow the new standard for throwing on out of range indexes.
|
|
|
|
Buffer.prototype.slice = function(start, end) {
|
|
|
|
var len = this.length;
|
|
|
|
start = ~~start;
|
2013-07-26 23:38:08 +02:00
|
|
|
end = util.isUndefined(end) ? len : ~~end;
|
2013-04-18 01:26:15 +02:00
|
|
|
|
|
|
|
if (start < 0) {
|
|
|
|
start += len;
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
} else if (start > len) {
|
|
|
|
start = len;
|
2012-06-12 00:24:38 +02:00
|
|
|
}
|
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
if (end < 0) {
|
|
|
|
end += len;
|
|
|
|
if (end < 0)
|
|
|
|
end = 0;
|
|
|
|
} else if (end > len) {
|
|
|
|
end = len;
|
2012-06-12 00:24:38 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 19:48:54 +01:00
|
|
|
if (end < start)
|
2013-04-18 01:26:15 +02:00
|
|
|
end = start;
|
2010-11-02 18:09:59 +01:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
var buf = new Buffer();
|
2013-07-09 05:58:39 +02:00
|
|
|
sliceOnto(this, buf, start, end);
|
2013-04-18 01:26:15 +02:00
|
|
|
buf.length = end - start;
|
2013-07-23 02:04:17 +02:00
|
|
|
if (buf.length > 0)
|
2013-07-26 23:38:08 +02:00
|
|
|
buf.parent = util.isUndefined(this.parent) ? this : this.parent;
|
2010-11-02 18:09:59 +01:00
|
|
|
|
2013-04-18 01:26:15 +02:00
|
|
|
return buf;
|
2010-11-02 18:09:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function checkOffset(offset, ext, length) {
|
2013-04-18 01:26:15 +02:00
|
|
|
if (offset < 0 || offset + ext > length)
|
|
|
|
throw new RangeError('index out of range');
|
2013-01-11 01:59:39 +01:00
|
|
|
}
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
Buffer.prototype.readUInt8 = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 1, this.length);
|
|
|
|
return this[offset];
|
2011-08-08 22:26:00 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function readUInt16(buffer, offset, isBigEndian) {
|
|
|
|
var val = 0;
|
2011-08-09 06:40:10 +02:00
|
|
|
if (isBigEndian) {
|
2011-05-01 20:38:10 +02:00
|
|
|
val = buffer[offset] << 8;
|
|
|
|
val |= buffer[offset + 1];
|
|
|
|
} else {
|
|
|
|
val = buffer[offset];
|
|
|
|
val |= buffer[offset + 1] << 8;
|
|
|
|
}
|
|
|
|
return val;
|
2011-08-09 06:40:10 +02:00
|
|
|
}
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 2, this.length);
|
2011-08-09 06:40:10 +02:00
|
|
|
return readUInt16(this, offset, false, noAssert);
|
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 2, this.length);
|
2011-08-09 06:40:10 +02:00
|
|
|
return readUInt16(this, offset, true, noAssert);
|
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
function readUInt32(buffer, offset, isBigEndian, noAssert) {
|
|
|
|
var val = 0;
|
|
|
|
if (isBigEndian) {
|
2011-05-01 20:38:10 +02:00
|
|
|
val = buffer[offset + 1] << 16;
|
|
|
|
val |= buffer[offset + 2] << 8;
|
|
|
|
val |= buffer[offset + 3];
|
|
|
|
val = val + (buffer[offset] << 24 >>> 0);
|
|
|
|
} else {
|
|
|
|
val = buffer[offset + 2] << 16;
|
|
|
|
val |= buffer[offset + 1] << 8;
|
|
|
|
val |= buffer[offset];
|
|
|
|
val = val + (buffer[offset + 3] << 24 >>> 0);
|
|
|
|
}
|
|
|
|
return val;
|
2011-08-09 06:40:10 +02:00
|
|
|
}
|
2011-08-08 22:26:00 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 4, this.length);
|
2011-08-09 06:40:10 +02:00
|
|
|
return readUInt32(this, offset, false, noAssert);
|
|
|
|
};
|
2011-08-08 22:26:00 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 4, this.length);
|
2011-08-09 06:40:10 +02:00
|
|
|
return readUInt32(this, offset, true, noAssert);
|
2011-08-08 22:26:00 +02:00
|
|
|
};
|
|
|
|
|
2011-05-01 20:38:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Signed integer types, yay team! A reminder on how two's complement actually
|
|
|
|
* works. The first bit is the signed bit, i.e. tells us whether or not the
|
|
|
|
* number should be positive or negative. If the two's complement value is
|
|
|
|
* positive, then we're done, as it's equivalent to the unsigned representation.
|
|
|
|
*
|
|
|
|
* Now if the number is positive, you're pretty much done, you can just leverage
|
|
|
|
* the unsigned translations and return those. Unfortunately, negative numbers
|
|
|
|
* aren't quite that straightforward.
|
|
|
|
*
|
|
|
|
* At first glance, one might be inclined to use the traditional formula to
|
|
|
|
* translate binary numbers between the positive and negative values in two's
|
|
|
|
* complement. (Though it doesn't quite work for the most negative value)
|
|
|
|
* Mainly:
|
|
|
|
* - invert all the bits
|
|
|
|
* - add one to the result
|
|
|
|
*
|
|
|
|
* Of course, this doesn't quite work in Javascript. Take for example the value
|
|
|
|
* of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
|
|
|
|
* course, Javascript will do the following:
|
|
|
|
*
|
|
|
|
* > ~0xff80
|
|
|
|
* -65409
|
|
|
|
*
|
|
|
|
* Whoh there, Javascript, that's not quite right. But wait, according to
|
|
|
|
* Javascript that's perfectly correct. When Javascript ends up seeing the
|
|
|
|
* constant 0xff80, it has no notion that it is actually a signed number. It
|
|
|
|
* assumes that we've input the unsigned value 0xff80. Thus, when it does the
|
|
|
|
* binary negation, it casts it into a signed value, (positive 0xff80). Then
|
|
|
|
* when you perform binary negation on that, it turns it into a negative number.
|
|
|
|
*
|
|
|
|
* Instead, we're going to have to use the following general formula, that works
|
|
|
|
* in a rather Javascript friendly way. I'm glad we don't support this kind of
|
|
|
|
* weird numbering scheme in the kernel.
|
|
|
|
*
|
|
|
|
* (BIT-MAX - (unsigned)val + 1) * -1
|
|
|
|
*
|
|
|
|
* The astute observer, may think that this doesn't make sense for 8-bit numbers
|
|
|
|
* (really it isn't necessary for them). However, when you get 16-bit numbers,
|
|
|
|
* you do. Let's go back to our prior example and see how this will look:
|
|
|
|
*
|
|
|
|
* (0xffff - 0xff80 + 1) * -1
|
|
|
|
* (0x007f + 1) * -1
|
|
|
|
* (0x0080) * -1
|
|
|
|
*/
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
Buffer.prototype.readInt8 = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 1, this.length);
|
|
|
|
if (!(this[offset] & 0x80))
|
|
|
|
return (this[offset]);
|
|
|
|
return ((0xff - this[offset] + 1) * -1);
|
2011-05-01 20:38:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function readInt16(buffer, offset, isBigEndian) {
|
|
|
|
var val = readUInt16(buffer, offset, isBigEndian);
|
|
|
|
if (!(val & 0x8000))
|
2011-05-01 20:38:10 +02:00
|
|
|
return val;
|
|
|
|
return (0xffff - val + 1) * -1;
|
2011-08-09 06:40:10 +02:00
|
|
|
}
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readInt16LE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 2, this.length);
|
|
|
|
return readInt16(this, offset, false);
|
2011-05-01 20:38:10 +02:00
|
|
|
};
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readInt16BE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkOffset(offset, 2, this.length);
|
|
|
|
return readInt16(this, offset, true);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function readInt32(buffer, offset, isBigEndian) {
|
|
|
|
var val = readUInt32(buffer, offset, isBigEndian);
|
|
|
|
if (!(val & 0x80000000))
|
2011-05-01 20:38:10 +02:00
|
|
|
return (val);
|
|
|
|
return (0xffffffff - val + 1) * -1;
|
2011-08-09 06:40:10 +02:00
|
|
|
}
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readInt32LE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
2013-04-05 16:48:18 +02:00
|
|
|
checkOffset(offset, 4, this.length);
|
2013-01-11 01:59:39 +01:00
|
|
|
return readInt32(this, offset, false);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.readInt32BE = function(offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
2013-04-05 16:48:18 +02:00
|
|
|
checkOffset(offset, 4, this.length);
|
2013-01-11 01:59:39 +01:00
|
|
|
return readInt32(this, offset, true);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-05-16 03:25:11 +02:00
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function checkInt(buffer, value, offset, ext, max, min) {
|
2013-04-18 01:26:15 +02:00
|
|
|
if (value > max || value < min)
|
2013-07-23 20:02:47 +02:00
|
|
|
throw new TypeError('value is out of bounds');
|
2013-04-18 01:26:15 +02:00
|
|
|
if (offset < 0 || offset + ext > buffer.length || buffer.length + offset < 0)
|
2013-07-23 20:02:47 +02:00
|
|
|
throw new RangeError('index out of range');
|
2011-05-01 20:38:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 1, 0xff, 0);
|
|
|
|
this[offset] = value;
|
2013-07-13 01:29:54 +02:00
|
|
|
return offset + 1;
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function writeUInt16(buffer, value, offset, isBigEndian) {
|
2011-08-09 06:40:10 +02:00
|
|
|
if (isBigEndian) {
|
2011-05-01 20:38:10 +02:00
|
|
|
buffer[offset] = (value & 0xff00) >>> 8;
|
|
|
|
buffer[offset + 1] = value & 0x00ff;
|
|
|
|
} else {
|
|
|
|
buffer[offset + 1] = (value & 0xff00) >>> 8;
|
|
|
|
buffer[offset] = value & 0x00ff;
|
|
|
|
}
|
2013-07-23 20:02:47 +02:00
|
|
|
return offset + 2;
|
2011-08-09 06:40:10 +02:00
|
|
|
}
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 2, 0xffff, 0);
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt16(this, value, offset, false);
|
2011-08-08 22:26:00 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 2, 0xffff, 0);
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt16(this, value, offset, true);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2011-08-08 22:26:00 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
function writeUInt32(buffer, value, offset, isBigEndian) {
|
2011-08-09 06:40:10 +02:00
|
|
|
if (isBigEndian) {
|
2011-05-01 20:38:10 +02:00
|
|
|
buffer[offset] = (value >>> 24) & 0xff;
|
|
|
|
buffer[offset + 1] = (value >>> 16) & 0xff;
|
|
|
|
buffer[offset + 2] = (value >>> 8) & 0xff;
|
|
|
|
buffer[offset + 3] = value & 0xff;
|
|
|
|
} else {
|
|
|
|
buffer[offset + 3] = (value >>> 24) & 0xff;
|
|
|
|
buffer[offset + 2] = (value >>> 16) & 0xff;
|
|
|
|
buffer[offset + 1] = (value >>> 8) & 0xff;
|
|
|
|
buffer[offset] = value & 0xff;
|
|
|
|
}
|
2013-07-23 20:02:47 +02:00
|
|
|
return offset + 4;
|
2011-08-09 06:40:10 +02:00
|
|
|
}
|
2011-08-08 22:26:00 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 4, 0xffffffff, 0);
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt32(this, value, offset, false);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-08-08 22:26:00 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 4, 0xffffffff, 0);
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt32(this, value, offset, true);
|
2011-08-08 22:26:00 +02:00
|
|
|
};
|
|
|
|
|
2011-05-01 20:38:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We now move onto our friends in the signed number category. Unlike unsigned
|
|
|
|
* numbers, we're going to have to worry a bit more about how we put values into
|
|
|
|
* arrays. Since we are only worrying about signed 32-bit values, we're in
|
|
|
|
* slightly better shape. Unfortunately, we really can't do our favorite binary
|
|
|
|
* & in this system. It really seems to do the wrong thing. For example:
|
|
|
|
*
|
|
|
|
* > -32 & 0xff
|
|
|
|
* 224
|
|
|
|
*
|
|
|
|
* What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of
|
|
|
|
* this aren't treated as a signed number. Ultimately a bad thing.
|
|
|
|
*
|
|
|
|
* What we're going to want to do is basically create the unsigned equivalent of
|
|
|
|
* our representation and pass that off to the wuint* functions. To do that
|
|
|
|
* we're going to do the following:
|
|
|
|
*
|
|
|
|
* - if the value is positive
|
|
|
|
* we can pass it directly off to the equivalent wuint
|
|
|
|
* - if the value is negative
|
|
|
|
* we do the following computation:
|
|
|
|
* mb + val + 1, where
|
|
|
|
* mb is the maximum unsigned value in that byte size
|
|
|
|
* val is the Javascript negative integer
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* As a concrete value, take -128. In signed 16 bits this would be 0xff80. If
|
|
|
|
* you do out the computations:
|
|
|
|
*
|
|
|
|
* 0xffff - 128 + 1
|
|
|
|
* 0xffff - 127
|
|
|
|
* 0xff80
|
|
|
|
*
|
|
|
|
* You can then encode this value as the signed version. This is really rather
|
|
|
|
* hacky, but it should work and get the job done which is our goal here.
|
|
|
|
*/
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 1, 0x7f, -0x80);
|
|
|
|
if (value < 0) value = 0xff + value + 1;
|
|
|
|
this[offset] = value;
|
2013-07-13 01:29:54 +02:00
|
|
|
return offset + 1;
|
2011-05-01 20:38:10 +02:00
|
|
|
};
|
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
|
|
|
|
if (value < 0) value = 0xffff + value + 1;
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt16(this, value, offset, false);
|
2011-05-01 20:38:10 +02:00
|
|
|
};
|
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
|
|
|
|
if (value < 0) value = 0xffff + value + 1;
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt16(this, value, offset, true);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|
2011-05-01 20:38:10 +02:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
|
|
|
|
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
|
|
|
|
if (value < 0) value = 0xffffffff + value + 1;
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt32(this, value, offset, false);
|
2011-05-01 20:38:10 +02:00
|
|
|
};
|
2011-05-16 03:25:11 +02:00
|
|
|
|
2013-01-11 01:59:39 +01:00
|
|
|
|
2011-08-09 06:40:10 +02:00
|
|
|
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
|
2013-04-18 01:26:15 +02:00
|
|
|
value = +value;
|
|
|
|
offset = ~~offset;
|
2013-01-11 01:59:39 +01:00
|
|
|
if (!noAssert)
|
|
|
|
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
|
|
|
|
if (value < 0) value = 0xffffffff + value + 1;
|
2013-07-23 20:02:47 +02:00
|
|
|
return writeUInt32(this, value, offset, true);
|
2011-08-09 06:40:10 +02:00
|
|
|
};
|