2017-01-03 22:16:48 +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.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
const {
|
|
|
|
byteLengthUtf8,
|
|
|
|
copy: _copy,
|
|
|
|
compare: _compare,
|
|
|
|
compareOffset,
|
|
|
|
createFromString,
|
2017-12-02 16:28:35 +01:00
|
|
|
fill: bindingFill,
|
2017-06-20 01:06:02 +02:00
|
|
|
indexOfBuffer,
|
|
|
|
indexOfNumber,
|
|
|
|
indexOfString,
|
|
|
|
swap16: _swap16,
|
|
|
|
swap32: _swap32,
|
|
|
|
swap64: _swap64,
|
|
|
|
kMaxLength,
|
|
|
|
kStringMaxLength
|
|
|
|
} = process.binding('buffer');
|
2018-01-27 22:01:32 +01:00
|
|
|
// We cannot use internalBinding unconditionally here because of the way
|
|
|
|
// that test/parallel/test-buffer-bindingobj-no-zerofill.js is written.
|
|
|
|
let isAnyArrayBuffer;
|
|
|
|
try {
|
2018-03-06 15:42:32 +01:00
|
|
|
const { internalBinding } = require('internal/bootstrap/loaders');
|
2018-01-27 22:01:32 +01:00
|
|
|
isAnyArrayBuffer = internalBinding('types').isAnyArrayBuffer;
|
|
|
|
} catch (e) {
|
|
|
|
isAnyArrayBuffer = require('util').types.isAnyArrayBuffer;
|
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
const {
|
|
|
|
customInspectSymbol,
|
|
|
|
normalizeEncoding,
|
|
|
|
kIsEncodingSymbol
|
|
|
|
} = require('internal/util');
|
2017-09-28 09:16:41 +02:00
|
|
|
const {
|
|
|
|
isArrayBufferView,
|
|
|
|
isUint8Array
|
|
|
|
} = require('internal/util/types');
|
2017-06-20 01:06:02 +02:00
|
|
|
const {
|
|
|
|
pendingDeprecation
|
|
|
|
} = process.binding('config');
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
|
|
|
ERR_BUFFER_OUT_OF_BOUNDS,
|
|
|
|
ERR_INDEX_OUT_OF_RANGE,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
ERR_INVALID_BUFFER_SIZE,
|
|
|
|
ERR_INVALID_OPT_VALUE,
|
|
|
|
ERR_NO_LONGER_SUPPORTED,
|
|
|
|
ERR_UNKNOWN_ENCODING
|
|
|
|
} = require('internal/errors').codes;
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
const internalBuffer = require('internal/buffer');
|
|
|
|
|
2017-10-22 08:25:59 +02:00
|
|
|
const { setupBufferJS } = internalBuffer;
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
const bindingObj = {};
|
|
|
|
|
2016-09-26 20:14:32 +02:00
|
|
|
class FastBuffer extends Uint8Array {
|
|
|
|
constructor(arg1, arg2, arg3) {
|
|
|
|
super(arg1, arg2, arg3);
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 11:50:53 +02:00
|
|
|
FastBuffer.prototype.constructor = Buffer;
|
2017-06-20 01:06:02 +02:00
|
|
|
internalBuffer.FastBuffer = FastBuffer;
|
2017-03-11 21:32:25 +01:00
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
Buffer.prototype = FastBuffer.prototype;
|
|
|
|
|
2018-01-30 17:34:25 +01:00
|
|
|
for (const [name, method] of Object.entries(internalBuffer.readWrites)) {
|
|
|
|
Buffer.prototype[name] = method;
|
|
|
|
}
|
|
|
|
|
2017-06-05 11:58:20 +02:00
|
|
|
const constants = Object.defineProperties({}, {
|
|
|
|
MAX_LENGTH: {
|
2017-08-14 12:54:05 +02:00
|
|
|
value: kMaxLength,
|
2017-06-05 11:58:20 +02:00
|
|
|
writable: false,
|
|
|
|
enumerable: true
|
|
|
|
},
|
|
|
|
MAX_STRING_LENGTH: {
|
2017-06-20 01:06:02 +02:00
|
|
|
value: kStringMaxLength,
|
2017-06-05 11:58:20 +02:00
|
|
|
writable: false,
|
|
|
|
enumerable: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
Buffer.poolSize = 8 * 1024;
|
|
|
|
var poolSize, poolOffset, allocPool;
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
setupBufferJS(Buffer.prototype, bindingObj);
|
2016-03-15 19:08:32 +01:00
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
// |zeroFill| can be undefined when running inside an isolate where we
|
2016-05-31 20:58:31 +02:00
|
|
|
// do not own the ArrayBuffer allocator. Zero fill is always on in that case.
|
|
|
|
const zeroFill = bindingObj.zeroFill || [0];
|
2015-09-15 00:31:10 +02:00
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
function createUnsafeBuffer(size) {
|
2016-08-27 19:26:01 +02:00
|
|
|
return new FastBuffer(createUnsafeArrayBuffer(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUnsafeArrayBuffer(size) {
|
2016-05-20 11:50:53 +02:00
|
|
|
zeroFill[0] = 0;
|
2016-06-01 15:53:01 +02:00
|
|
|
try {
|
2016-08-27 19:26:01 +02:00
|
|
|
return new ArrayBuffer(size);
|
2016-06-01 15:53:01 +02:00
|
|
|
} finally {
|
2016-05-20 11:50:53 +02:00
|
|
|
zeroFill[0] = 1;
|
2016-06-01 15:53:01 +02:00
|
|
|
}
|
2015-12-18 03:09:15 +01:00
|
|
|
}
|
2015-09-15 00:31:10 +02:00
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
function createPool() {
|
|
|
|
poolSize = Buffer.poolSize;
|
2016-08-27 19:26:01 +02:00
|
|
|
allocPool = createUnsafeArrayBuffer(poolSize);
|
2015-06-19 21:00:19 +02:00
|
|
|
poolOffset = 0;
|
|
|
|
}
|
2015-09-15 00:31:10 +02:00
|
|
|
createPool();
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2015-08-21 21:48:10 +02:00
|
|
|
function alignPool() {
|
|
|
|
// Ensure aligned slices
|
|
|
|
if (poolOffset & 0x7) {
|
|
|
|
poolOffset |= 0x7;
|
|
|
|
poolOffset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 21:32:25 +01:00
|
|
|
var bufferWarn = true;
|
|
|
|
const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
|
|
|
|
'recommended for use due to security and usability ' +
|
|
|
|
'concerns. Please use the new Buffer.alloc(), ' +
|
|
|
|
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
|
|
|
|
'methods instead.';
|
|
|
|
|
|
|
|
function showFlaggedDeprecation() {
|
|
|
|
if (bufferWarn) {
|
|
|
|
// This is a *pending* deprecation warning. It is not emitted by
|
|
|
|
// default unless the --pending-deprecation command-line flag is
|
2017-06-17 15:11:45 +02:00
|
|
|
// used or the NODE_PENDING_DEPRECATION=1 env var is set.
|
2017-03-11 21:32:25 +01:00
|
|
|
process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005');
|
|
|
|
bufferWarn = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const doFlaggedDeprecation =
|
|
|
|
pendingDeprecation ?
|
|
|
|
showFlaggedDeprecation :
|
|
|
|
function() {};
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
/**
|
2017-06-17 15:11:45 +02:00
|
|
|
* The Buffer() constructor is deprecated in documentation and should not be
|
2016-12-04 21:47:01 +01:00
|
|
|
* used moving forward. Rather, developers should use one of the three new
|
|
|
|
* factory APIs: Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() based on
|
|
|
|
* their specific needs. There is no runtime deprecation because of the extent
|
|
|
|
* to which the Buffer constructor is used in the ecosystem currently -- a
|
|
|
|
* runtime deprecation would introduce too much breakage at this time. It's not
|
|
|
|
* likely that the Buffer constructors would ever actually be removed.
|
|
|
|
* Deprecation Code: DEP0005
|
2016-01-26 00:00:06 +01:00
|
|
|
**/
|
|
|
|
function Buffer(arg, encodingOrOffset, length) {
|
2017-03-11 21:32:25 +01:00
|
|
|
doFlaggedDeprecation();
|
2015-06-19 21:00:19 +02:00
|
|
|
// Common case.
|
|
|
|
if (typeof arg === 'number') {
|
2016-01-26 00:00:06 +01:00
|
|
|
if (typeof encodingOrOffset === 'string') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('string', 'string', arg);
|
2016-01-02 17:16:19 +01:00
|
|
|
}
|
2017-03-31 05:08:17 +02:00
|
|
|
return Buffer.alloc(arg);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
2016-01-26 00:00:06 +01:00
|
|
|
return Buffer.from(arg, encodingOrOffset, length);
|
2015-09-15 00:31:10 +02:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-03-11 21:32:25 +01:00
|
|
|
Object.defineProperty(Buffer, Symbol.species, {
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
get() { return FastBuffer; }
|
|
|
|
});
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
/**
|
|
|
|
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
|
|
|
|
* if value is a number.
|
|
|
|
* Buffer.from(str[, encoding])
|
|
|
|
* Buffer.from(array)
|
|
|
|
* Buffer.from(buffer)
|
|
|
|
* Buffer.from(arrayBuffer[, byteOffset[, length]])
|
|
|
|
**/
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.from = function from(value, encodingOrOffset, length) {
|
2017-04-10 11:03:57 +02:00
|
|
|
if (typeof value === 'string')
|
|
|
|
return fromString(value, encodingOrOffset);
|
2016-01-26 00:00:06 +01:00
|
|
|
|
2017-04-10 11:03:57 +02:00
|
|
|
if (isAnyArrayBuffer(value))
|
2016-01-26 00:00:06 +01:00
|
|
|
return fromArrayBuffer(value, encodingOrOffset, length);
|
|
|
|
|
2017-09-04 14:53:43 +02:00
|
|
|
if (value === null || value === undefined) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2017-07-13 21:17:45 +02:00
|
|
|
'first argument',
|
2017-10-28 11:39:55 +02:00
|
|
|
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
|
2017-07-13 21:17:45 +02:00
|
|
|
value
|
|
|
|
);
|
|
|
|
}
|
2017-06-16 21:35:15 +02:00
|
|
|
|
2017-02-03 16:05:28 +01:00
|
|
|
if (typeof value === 'number') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('value', 'not number', value);
|
2017-02-03 16:05:28 +01:00
|
|
|
}
|
2017-06-16 21:35:15 +02:00
|
|
|
|
|
|
|
const valueOf = value.valueOf && value.valueOf();
|
2017-09-04 14:53:43 +02:00
|
|
|
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
|
2017-06-16 21:35:15 +02:00
|
|
|
return Buffer.from(valueOf, encodingOrOffset, length);
|
|
|
|
|
2017-04-10 11:03:57 +02:00
|
|
|
var b = fromObject(value);
|
|
|
|
if (b)
|
|
|
|
return b;
|
2016-01-26 00:00:06 +01:00
|
|
|
|
2017-06-16 21:35:15 +02:00
|
|
|
if (typeof value[Symbol.toPrimitive] === 'function') {
|
|
|
|
return Buffer.from(value[Symbol.toPrimitive]('string'),
|
|
|
|
encodingOrOffset,
|
|
|
|
length);
|
|
|
|
}
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2017-07-13 21:17:45 +02:00
|
|
|
'first argument',
|
2017-10-28 11:39:55 +02:00
|
|
|
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
|
2017-08-22 13:57:26 +02:00
|
|
|
value
|
2017-07-13 21:17:45 +02:00
|
|
|
);
|
2016-01-26 00:00:06 +01:00
|
|
|
};
|
|
|
|
|
2015-09-27 00:18:08 +02:00
|
|
|
Object.setPrototypeOf(Buffer, Uint8Array);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-12-25 01:50:05 +01:00
|
|
|
// The 'assertSize' method will remove itself from the callstack when an error
|
|
|
|
// occurs. This is done simply to keep the internal details of the
|
|
|
|
// implementation from bleeding out to users.
|
2016-03-21 20:38:08 +01:00
|
|
|
function assertSize(size) {
|
2016-05-31 19:41:30 +02:00
|
|
|
let err = null;
|
|
|
|
|
2017-06-20 23:37:00 +02:00
|
|
|
if (typeof size !== 'number') {
|
2018-02-27 14:55:32 +01:00
|
|
|
err = new ERR_INVALID_ARG_TYPE('size', 'number', size);
|
2018-02-09 00:42:23 +01:00
|
|
|
} else if (size < 0 || size > kMaxLength) {
|
2018-02-27 14:55:32 +01:00
|
|
|
err = new ERR_INVALID_OPT_VALUE.RangeError('size', size);
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2016-05-31 19:41:30 +02:00
|
|
|
|
2018-02-14 23:48:35 +01:00
|
|
|
if (err !== null) {
|
2016-03-21 20:38:08 +01:00
|
|
|
Error.captureStackTrace(err, assertSize);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
/**
|
|
|
|
* Creates a new filled Buffer instance.
|
|
|
|
* alloc(size[, fill[, encoding]])
|
|
|
|
**/
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.alloc = function alloc(size, fill, encoding) {
|
2016-03-21 20:38:08 +01:00
|
|
|
assertSize(size);
|
2018-02-14 23:42:57 +01:00
|
|
|
if (fill !== undefined && size > 0) {
|
|
|
|
return _fill(createUnsafeBuffer(size), fill, encoding);
|
2016-01-26 00:00:06 +01:00
|
|
|
}
|
2016-05-20 11:50:53 +02:00
|
|
|
return new FastBuffer(size);
|
2016-01-26 00:00:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to Buffer(num), by default creates a non-zero-filled Buffer
|
|
|
|
* instance. If `--zero-fill-buffers` is set, will zero-fill the buffer.
|
|
|
|
**/
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.allocUnsafe = function allocUnsafe(size) {
|
2016-03-21 20:38:08 +01:00
|
|
|
assertSize(size);
|
2016-01-26 00:00:06 +01:00
|
|
|
return allocate(size);
|
|
|
|
};
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-03-21 20:38:08 +01:00
|
|
|
/**
|
|
|
|
* Equivalent to SlowBuffer(num), by default creates a non-zero-filled
|
|
|
|
* Buffer instance that is not allocated off the pre-initialized pool.
|
|
|
|
* If `--zero-fill-buffers` is set, will zero-fill the buffer.
|
|
|
|
**/
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
|
2016-03-21 20:38:08 +01:00
|
|
|
assertSize(size);
|
2016-05-20 11:50:53 +02:00
|
|
|
return createUnsafeBuffer(size);
|
2016-03-21 20:38:08 +01:00
|
|
|
};
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
// If --zero-fill-buffers command line argument is set, a zero-filled
|
|
|
|
// buffer is returned.
|
2015-06-19 21:00:19 +02:00
|
|
|
function SlowBuffer(length) {
|
2017-04-16 20:29:35 +02:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2015-09-01 10:51:17 +02:00
|
|
|
if (+length != length)
|
2015-06-19 21:00:19 +02:00
|
|
|
length = 0;
|
2016-12-13 05:51:01 +01:00
|
|
|
assertSize(+length);
|
2016-05-20 11:50:53 +02:00
|
|
|
return createUnsafeBuffer(+length);
|
2015-09-15 00:31:10 +02:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2015-09-27 00:18:08 +02:00
|
|
|
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
|
|
|
|
Object.setPrototypeOf(SlowBuffer, Uint8Array);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
|
|
|
function allocate(size) {
|
2016-05-29 20:03:32 +02:00
|
|
|
if (size <= 0) {
|
2016-05-20 11:50:53 +02:00
|
|
|
return new FastBuffer();
|
2015-09-17 18:48:16 +02:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
if (size < (Buffer.poolSize >>> 1)) {
|
|
|
|
if (size > (poolSize - poolOffset))
|
|
|
|
createPool();
|
2016-08-27 19:26:01 +02:00
|
|
|
var b = new FastBuffer(allocPool, poolOffset, size);
|
2015-06-19 21:00:19 +02:00
|
|
|
poolOffset += size;
|
2015-08-21 21:48:10 +02:00
|
|
|
alignPool();
|
2015-06-19 21:00:19 +02:00
|
|
|
return b;
|
|
|
|
} else {
|
2016-05-20 11:50:53 +02:00
|
|
|
return createUnsafeBuffer(size);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromString(string, encoding) {
|
2017-04-10 11:03:57 +02:00
|
|
|
var length;
|
|
|
|
if (typeof encoding !== 'string' || encoding.length === 0) {
|
|
|
|
if (string.length === 0)
|
|
|
|
return new FastBuffer();
|
2017-09-04 14:53:43 +02:00
|
|
|
encoding = 'utf8';
|
2017-06-20 01:06:02 +02:00
|
|
|
length = byteLengthUtf8(string);
|
2017-04-10 11:03:57 +02:00
|
|
|
} else {
|
|
|
|
length = byteLength(string, encoding, true);
|
|
|
|
if (length === -1)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
2017-04-10 11:03:57 +02:00
|
|
|
if (string.length === 0)
|
|
|
|
return new FastBuffer();
|
|
|
|
}
|
2016-05-15 18:04:03 +02:00
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
if (length >= (Buffer.poolSize >>> 1))
|
2017-06-20 01:06:02 +02:00
|
|
|
return createFromString(string, encoding);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
|
|
|
if (length > (poolSize - poolOffset))
|
|
|
|
createPool();
|
2016-08-27 19:26:01 +02:00
|
|
|
var b = new FastBuffer(allocPool, poolOffset, length);
|
2017-04-10 11:03:57 +02:00
|
|
|
const actual = b.write(string, encoding);
|
2016-08-27 19:26:01 +02:00
|
|
|
if (actual !== length) {
|
2017-02-06 20:44:17 +01:00
|
|
|
// byteLength() may overestimate. That's a rare case, though.
|
2016-08-27 19:26:01 +02:00
|
|
|
b = new FastBuffer(allocPool, poolOffset, actual);
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
poolOffset += actual;
|
2015-08-21 21:48:10 +02:00
|
|
|
alignPool();
|
2015-06-19 21:00:19 +02:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2016-01-29 11:15:32 +01:00
|
|
|
function fromArrayLike(obj) {
|
|
|
|
const length = obj.length;
|
|
|
|
const b = allocate(length);
|
2016-03-21 10:46:42 +01:00
|
|
|
for (var i = 0; i < length; i++)
|
2016-05-20 11:50:53 +02:00
|
|
|
b[i] = obj[i];
|
2016-01-29 11:15:32 +01:00
|
|
|
return b;
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
function fromArrayBuffer(obj, byteOffset, length) {
|
2017-04-01 07:48:35 +02:00
|
|
|
// convert byteOffset to integer
|
2017-04-10 11:03:57 +02:00
|
|
|
if (byteOffset === undefined) {
|
|
|
|
byteOffset = 0;
|
|
|
|
} else {
|
|
|
|
byteOffset = +byteOffset;
|
2018-02-13 00:56:35 +01:00
|
|
|
if (Number.isNaN(byteOffset))
|
2017-04-10 11:03:57 +02:00
|
|
|
byteOffset = 0;
|
|
|
|
}
|
2016-01-26 00:00:06 +01:00
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
const maxLength = obj.byteLength - byteOffset;
|
|
|
|
|
2016-06-06 14:06:56 +02:00
|
|
|
if (maxLength < 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
|
2016-05-20 11:50:53 +02:00
|
|
|
|
|
|
|
if (length === undefined) {
|
|
|
|
length = maxLength;
|
|
|
|
} else {
|
2018-02-13 00:56:35 +01:00
|
|
|
// Convert length to non-negative integer.
|
2017-04-01 07:48:35 +02:00
|
|
|
length = +length;
|
2018-02-13 00:58:22 +01:00
|
|
|
if (length > 0) {
|
2017-04-10 11:03:57 +02:00
|
|
|
if (length > maxLength)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
|
2017-04-10 11:03:57 +02:00
|
|
|
} else {
|
|
|
|
length = 0;
|
|
|
|
}
|
2016-05-20 11:50:53 +02:00
|
|
|
}
|
2016-01-26 00:00:06 +01:00
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
return new FastBuffer(obj, byteOffset, length);
|
2016-01-26 00:00:06 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
function fromObject(obj) {
|
2016-12-10 22:28:37 +01:00
|
|
|
if (isUint8Array(obj)) {
|
2016-01-26 22:17:31 +01:00
|
|
|
const b = allocate(obj.length);
|
2015-12-17 10:44:34 +01:00
|
|
|
|
|
|
|
if (b.length === 0)
|
|
|
|
return b;
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
_copy(obj, b, 0, 0, obj.length);
|
2015-06-19 21:00:19 +02:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:18:40 +02:00
|
|
|
if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
|
2018-02-13 00:58:22 +01:00
|
|
|
if (typeof obj.length !== 'number') {
|
2017-08-30 19:18:40 +02:00
|
|
|
return new FastBuffer();
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
2017-08-30 19:18:40 +02:00
|
|
|
return fromArrayLike(obj);
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-08-30 19:18:40 +02:00
|
|
|
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
|
|
|
|
return fromArrayLike(obj.data);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static methods
|
|
|
|
|
|
|
|
Buffer.isBuffer = function isBuffer(b) {
|
|
|
|
return b instanceof Buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
Buffer.compare = function compare(a, b) {
|
2016-12-10 22:28:37 +01:00
|
|
|
if (!isUint8Array(a) || !isUint8Array(b)) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(['buf1', 'buf2'], ['Buffer', 'Uint8Array']);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a === b) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
return _compare(a, b);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.isEncoding = function isEncoding(encoding) {
|
2018-02-15 03:26:17 +01:00
|
|
|
return typeof encoding === 'string' && encoding.length !== 0 &&
|
2018-02-14 23:48:35 +01:00
|
|
|
normalizeEncoding(encoding) !== undefined;
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.concat = function concat(list, length) {
|
2016-03-21 10:46:42 +01:00
|
|
|
var i;
|
2017-02-03 16:05:28 +01:00
|
|
|
if (!Array.isArray(list)) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
|
2017-02-03 16:05:28 +01:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
|
|
|
if (list.length === 0)
|
2016-05-20 11:50:53 +02:00
|
|
|
return new FastBuffer();
|
2015-06-19 21:00:19 +02:00
|
|
|
|
|
|
|
if (length === undefined) {
|
|
|
|
length = 0;
|
2016-03-21 10:46:42 +01:00
|
|
|
for (i = 0; i < list.length; i++)
|
2015-06-19 21:00:19 +02:00
|
|
|
length += list[i].length;
|
|
|
|
} else {
|
|
|
|
length = length >>> 0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
var buffer = Buffer.allocUnsafe(length);
|
2015-06-19 21:00:19 +02:00
|
|
|
var pos = 0;
|
2016-03-21 10:46:42 +01:00
|
|
|
for (i = 0; i < list.length; i++) {
|
2015-06-19 21:00:19 +02:00
|
|
|
var buf = list[i];
|
2017-02-03 16:05:28 +01:00
|
|
|
if (!isUint8Array(buf)) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
|
2017-02-03 16:05:28 +01:00
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
_copy(buf, buffer, pos);
|
2015-06-19 21:00:19 +02:00
|
|
|
pos += buf.length;
|
|
|
|
}
|
|
|
|
|
2016-09-16 07:07:23 +02:00
|
|
|
// Note: `length` is always equal to `buffer.length` at this point
|
|
|
|
if (pos < length) {
|
|
|
|
// Zero-fill the remaining bytes if the specified `length` was more than
|
|
|
|
// the actual total length, i.e. if we have some remaining allocated bytes
|
|
|
|
// there were not initialized.
|
|
|
|
buffer.fill(0, pos, length);
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
return buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
function base64ByteLength(str, bytes) {
|
|
|
|
// Handle padding
|
|
|
|
if (str.charCodeAt(bytes - 1) === 0x3D)
|
|
|
|
bytes--;
|
|
|
|
if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D)
|
|
|
|
bytes--;
|
|
|
|
|
|
|
|
// Base64 ratio: 3/4
|
|
|
|
return (bytes * 3) >>> 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
function byteLength(string, encoding) {
|
2016-02-16 04:08:49 +01:00
|
|
|
if (typeof string !== 'string') {
|
2017-09-28 09:16:41 +02:00
|
|
|
if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
|
2016-02-16 04:08:49 +01:00
|
|
|
return string.byteLength;
|
2016-09-12 08:33:34 +02:00
|
|
|
}
|
2016-01-18 08:08:12 +01:00
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'string', ['string', 'Buffer', 'ArrayBuffer'], string
|
2017-07-13 21:17:45 +02:00
|
|
|
);
|
2016-02-16 04:08:49 +01:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-04-10 11:03:57 +02:00
|
|
|
const len = string.length;
|
|
|
|
const mustMatch = (arguments.length > 2 && arguments[2] === true);
|
|
|
|
if (!mustMatch && len === 0)
|
2015-06-19 21:00:19 +02:00
|
|
|
return 0;
|
|
|
|
|
2017-04-10 11:03:57 +02:00
|
|
|
if (!encoding)
|
2017-06-20 01:06:02 +02:00
|
|
|
return (mustMatch ? -1 : byteLengthUtf8(string));
|
2017-04-10 11:03:57 +02:00
|
|
|
|
|
|
|
encoding += '';
|
|
|
|
switch (encoding.length) {
|
|
|
|
case 4:
|
2017-06-20 01:06:02 +02:00
|
|
|
if (encoding === 'utf8') return byteLengthUtf8(string);
|
2017-04-10 11:03:57 +02:00
|
|
|
if (encoding === 'ucs2') return len * 2;
|
|
|
|
encoding = encoding.toLowerCase();
|
2017-06-20 01:06:02 +02:00
|
|
|
if (encoding === 'utf8') return byteLengthUtf8(string);
|
2017-04-10 11:03:57 +02:00
|
|
|
if (encoding === 'ucs2') return len * 2;
|
|
|
|
break;
|
|
|
|
case 5:
|
2017-06-20 01:06:02 +02:00
|
|
|
if (encoding === 'utf-8') return byteLengthUtf8(string);
|
2017-04-10 11:03:57 +02:00
|
|
|
if (encoding === 'ascii') return len;
|
|
|
|
if (encoding === 'ucs-2') return len * 2;
|
|
|
|
encoding = encoding.toLowerCase();
|
2017-06-20 01:06:02 +02:00
|
|
|
if (encoding === 'utf-8') return byteLengthUtf8(string);
|
2017-04-10 11:03:57 +02:00
|
|
|
if (encoding === 'ascii') return len;
|
|
|
|
if (encoding === 'ucs-2') return len * 2;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
|
2015-06-19 21:00:19 +02:00
|
|
|
return len * 2;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
|
|
|
|
return len * 2;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (encoding === 'latin1' || encoding === 'binary') return len;
|
|
|
|
if (encoding === 'base64') return base64ByteLength(string, len);
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'latin1' || encoding === 'binary') return len;
|
|
|
|
if (encoding === 'base64') return base64ByteLength(string, len);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
|
2015-06-19 21:00:19 +02:00
|
|
|
return len >>> 1;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
return (mustMatch ? -1 : byteLengthUtf8(string));
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer.byteLength = byteLength;
|
|
|
|
|
|
|
|
// For backwards compatibility.
|
|
|
|
Object.defineProperty(Buffer.prototype, 'parent', {
|
|
|
|
enumerable: true,
|
2017-06-20 01:06:02 +02:00
|
|
|
get() {
|
2015-10-09 22:18:54 +02:00
|
|
|
if (!(this instanceof Buffer))
|
|
|
|
return undefined;
|
2015-06-19 21:00:19 +02:00
|
|
|
return this.buffer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Object.defineProperty(Buffer.prototype, 'offset', {
|
|
|
|
enumerable: true,
|
2017-06-20 01:06:02 +02:00
|
|
|
get() {
|
2015-10-09 22:18:54 +02:00
|
|
|
if (!(this instanceof Buffer))
|
|
|
|
return undefined;
|
2015-06-19 21:00:19 +02:00
|
|
|
return this.byteOffset;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-10 11:05:58 +02:00
|
|
|
function stringSlice(buf, encoding, start, end) {
|
|
|
|
if (encoding === undefined) return buf.utf8Slice(start, end);
|
|
|
|
encoding += '';
|
|
|
|
switch (encoding.length) {
|
|
|
|
case 4:
|
|
|
|
if (encoding === 'utf8') return buf.utf8Slice(start, end);
|
|
|
|
if (encoding === 'ucs2') return buf.ucs2Slice(start, end);
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'utf8') return buf.utf8Slice(start, end);
|
|
|
|
if (encoding === 'ucs2') return buf.ucs2Slice(start, end);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (encoding === 'utf-8') return buf.utf8Slice(start, end);
|
|
|
|
if (encoding === 'ascii') return buf.asciiSlice(start, end);
|
|
|
|
if (encoding === 'ucs-2') return buf.ucs2Slice(start, end);
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'utf-8') return buf.utf8Slice(start, end);
|
|
|
|
if (encoding === 'ascii') return buf.asciiSlice(start, end);
|
|
|
|
if (encoding === 'ucs-2') return buf.ucs2Slice(start, end);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (encoding === 'latin1' || encoding === 'binary')
|
2017-02-15 18:35:57 +01:00
|
|
|
return buf.latin1Slice(start, end);
|
2017-04-10 11:05:58 +02:00
|
|
|
if (encoding === 'base64') return buf.base64Slice(start, end);
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'latin1' || encoding === 'binary')
|
|
|
|
return buf.latin1Slice(start, end);
|
|
|
|
if (encoding === 'base64') return buf.base64Slice(start, end);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
|
|
|
|
return buf.hexSlice(start, end);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
|
2017-02-15 18:35:57 +01:00
|
|
|
return buf.ucs2Slice(start, end);
|
2017-04-10 11:05:58 +02:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
|
|
|
|
return buf.ucs2Slice(start, end);
|
|
|
|
break;
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.prototype.copy =
|
|
|
|
function copy(target, targetStart, sourceStart, sourceEnd) {
|
|
|
|
return _copy(this, target, targetStart, sourceStart, sourceEnd);
|
|
|
|
};
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-04-10 11:05:58 +02:00
|
|
|
// No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
|
|
|
|
// property of a typed array.
|
|
|
|
// This behaves neither like String nor Uint8Array in that we set start/end
|
|
|
|
// to their upper/lower bounds if the value passed is out of range.
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.prototype.toString = function toString(encoding, start, end) {
|
2015-08-16 16:09:02 +02:00
|
|
|
if (arguments.length === 0) {
|
2017-04-30 18:53:04 +02:00
|
|
|
return this.utf8Slice(0, this.length);
|
2015-08-16 16:09:02 +02:00
|
|
|
}
|
2017-04-30 18:53:04 +02:00
|
|
|
|
|
|
|
const len = this.length;
|
|
|
|
if (len === 0)
|
|
|
|
return '';
|
|
|
|
|
|
|
|
if (!start || start < 0)
|
|
|
|
start = 0;
|
|
|
|
else if (start >= len)
|
|
|
|
return '';
|
|
|
|
|
|
|
|
if (end === undefined || end > len)
|
|
|
|
end = len;
|
|
|
|
else if (end <= 0)
|
|
|
|
return '';
|
|
|
|
|
|
|
|
start |= 0;
|
|
|
|
end |= 0;
|
|
|
|
|
|
|
|
if (end <= start)
|
|
|
|
return '';
|
|
|
|
return stringSlice(this, encoding, start, end);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Buffer.prototype.equals = function equals(b) {
|
2017-07-13 21:17:45 +02:00
|
|
|
if (!isUint8Array(b)) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('otherBuffer', ['Buffer', 'Uint8Array'], b);
|
2017-07-13 21:17:45 +02:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
if (this === b)
|
|
|
|
return true;
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
return _compare(this, b) === 0;
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-08-19 00:26:36 +02:00
|
|
|
// Override how buffers are presented by util.inspect().
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.prototype[customInspectSymbol] = function inspect() {
|
2015-06-19 21:00:19 +02:00
|
|
|
var str = '';
|
|
|
|
var max = exports.INSPECT_MAX_BYTES;
|
2017-02-28 09:15:07 +01:00
|
|
|
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
|
|
|
|
if (this.length > max)
|
|
|
|
str += ' ... ';
|
|
|
|
return `<${this.constructor.name} ${str}>`;
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-03-23 22:27:39 +01:00
|
|
|
Buffer.prototype.compare = function compare(target,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
thisStart,
|
|
|
|
thisEnd) {
|
2017-07-13 21:17:45 +02:00
|
|
|
if (!isUint8Array(target)) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
|
2017-07-13 21:17:45 +02:00
|
|
|
}
|
2017-01-20 04:26:18 +01:00
|
|
|
if (arguments.length === 1)
|
2017-06-20 01:06:02 +02:00
|
|
|
return _compare(this, target);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-03-23 22:27:39 +01:00
|
|
|
if (start === undefined)
|
|
|
|
start = 0;
|
2017-01-20 04:26:18 +01:00
|
|
|
else if (start < 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
|
|
|
start >>>= 0;
|
|
|
|
|
2016-03-23 22:27:39 +01:00
|
|
|
if (end === undefined)
|
2016-09-15 20:28:02 +02:00
|
|
|
end = target.length;
|
2017-01-20 04:26:18 +01:00
|
|
|
else if (end > target.length)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
|
|
|
end >>>= 0;
|
|
|
|
|
2016-03-23 22:27:39 +01:00
|
|
|
if (thisStart === undefined)
|
|
|
|
thisStart = 0;
|
2017-01-20 04:26:18 +01:00
|
|
|
else if (thisStart < 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
|
|
|
thisStart >>>= 0;
|
|
|
|
|
2016-03-23 22:27:39 +01:00
|
|
|
if (thisEnd === undefined)
|
|
|
|
thisEnd = this.length;
|
2017-01-20 04:26:18 +01:00
|
|
|
else if (thisEnd > this.length)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
|
|
|
thisEnd >>>= 0;
|
2016-03-23 22:27:39 +01:00
|
|
|
|
|
|
|
if (thisStart >= thisEnd)
|
2017-01-20 04:26:18 +01:00
|
|
|
return (start >= end ? 0 : -1);
|
|
|
|
else if (start >= end)
|
2016-03-23 22:27:39 +01:00
|
|
|
return 1;
|
|
|
|
|
2017-01-20 04:26:18 +01:00
|
|
|
return compareOffset(this, target, start, thisStart, end, thisEnd);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-01-28 22:12:09 +01:00
|
|
|
// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
|
|
|
|
// OR the last index of `val` in `buffer` at offset <= `byteOffset`.
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
// - buffer - a Buffer to search
|
|
|
|
// - val - a string, Buffer, or number
|
|
|
|
// - byteOffset - an index into `buffer`; will be clamped to an int32
|
2018-03-04 09:24:17 +01:00
|
|
|
// - encoding - an optional encoding, relevant if val is a string
|
2016-01-28 22:12:09 +01:00
|
|
|
// - dir - true for indexOf, false for lastIndexOf
|
|
|
|
function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
|
|
|
|
if (typeof byteOffset === 'string') {
|
|
|
|
encoding = byteOffset;
|
|
|
|
byteOffset = undefined;
|
|
|
|
} else if (byteOffset > 0x7fffffff) {
|
|
|
|
byteOffset = 0x7fffffff;
|
|
|
|
} else if (byteOffset < -0x80000000) {
|
|
|
|
byteOffset = -0x80000000;
|
|
|
|
}
|
2016-12-07 09:47:38 +01:00
|
|
|
// Coerce to Number. Values like null and [] become 0.
|
|
|
|
byteOffset = +byteOffset;
|
|
|
|
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
|
2018-02-13 00:56:35 +01:00
|
|
|
if (Number.isNaN(byteOffset)) {
|
2017-05-14 19:57:52 +02:00
|
|
|
byteOffset = dir ? 0 : buffer.length;
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
dir = !!dir; // Cast to bool.
|
|
|
|
|
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (encoding === undefined) {
|
2017-06-20 01:06:02 +02:00
|
|
|
return indexOfString(buffer, val, byteOffset, encoding, dir);
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
return slowIndexOf(buffer, val, byteOffset, encoding, dir);
|
2016-12-10 22:28:37 +01:00
|
|
|
} else if (isUint8Array(val)) {
|
2017-06-20 01:06:02 +02:00
|
|
|
return indexOfBuffer(buffer, val, byteOffset, encoding, dir);
|
2016-01-28 22:12:09 +01:00
|
|
|
} else if (typeof val === 'number') {
|
2017-06-20 01:06:02 +02:00
|
|
|
return indexOfNumber(buffer, val, byteOffset, dir);
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'value', ['string', 'Buffer', 'Uint8Array'], val
|
2017-07-13 21:17:45 +02:00
|
|
|
);
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
|
2015-09-03 10:10:30 +02:00
|
|
|
var loweredCase = false;
|
|
|
|
for (;;) {
|
|
|
|
switch (encoding) {
|
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
|
|
|
case 'ucs2':
|
|
|
|
case 'ucs-2':
|
|
|
|
case 'utf16le':
|
|
|
|
case 'utf-16le':
|
2016-06-02 18:55:36 +02:00
|
|
|
case 'latin1':
|
2015-09-03 10:10:30 +02:00
|
|
|
case 'binary':
|
2017-06-20 01:06:02 +02:00
|
|
|
return indexOfString(buffer, val, byteOffset, encoding, dir);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2015-09-03 10:10:30 +02:00
|
|
|
case 'base64':
|
|
|
|
case 'ascii':
|
|
|
|
case 'hex':
|
2017-06-20 01:06:02 +02:00
|
|
|
return indexOfBuffer(
|
2017-07-05 18:21:40 +02:00
|
|
|
buffer, Buffer.from(val, encoding), byteOffset, encoding, dir);
|
2015-09-03 10:10:30 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (loweredCase) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
2015-09-03 10:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
encoding = ('' + encoding).toLowerCase();
|
|
|
|
loweredCase = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
|
2016-01-28 22:12:09 +01:00
|
|
|
return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
|
|
|
|
};
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-01-28 22:12:09 +01:00
|
|
|
Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
|
|
|
|
return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2015-10-28 17:33:57 +01:00
|
|
|
Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
|
|
|
|
return this.indexOf(val, byteOffset, encoding) !== -1;
|
|
|
|
};
|
|
|
|
|
2016-01-27 00:37:31 +01:00
|
|
|
// Usage:
|
|
|
|
// buffer.fill(number[, offset[, end]])
|
|
|
|
// buffer.fill(buffer[, offset[, end]])
|
|
|
|
// buffer.fill(string[, offset[, end]][, encoding])
|
|
|
|
Buffer.prototype.fill = function fill(val, start, end, encoding) {
|
2018-02-14 23:42:57 +01:00
|
|
|
return _fill(this, val, start, end, encoding);
|
2017-12-02 16:28:35 +01:00
|
|
|
};
|
|
|
|
|
2018-02-14 23:42:57 +01:00
|
|
|
function _fill(buf, val, start, end, encoding) {
|
2016-01-27 00:37:31 +01:00
|
|
|
if (typeof val === 'string') {
|
2018-02-14 23:42:57 +01:00
|
|
|
if (start === undefined || typeof start === 'string') {
|
2016-01-27 00:37:31 +01:00
|
|
|
encoding = start;
|
|
|
|
start = 0;
|
2017-12-02 16:28:35 +01:00
|
|
|
end = buf.length;
|
2016-01-27 00:37:31 +01:00
|
|
|
} else if (typeof end === 'string') {
|
|
|
|
encoding = end;
|
2017-12-02 16:28:35 +01:00
|
|
|
end = buf.length;
|
2016-01-27 00:37:31 +01:00
|
|
|
}
|
2016-11-29 12:03:24 +01:00
|
|
|
|
2018-02-14 23:42:57 +01:00
|
|
|
const normalizedEncoding = normalizeEncoding(encoding);
|
2016-11-29 12:03:24 +01:00
|
|
|
if (normalizedEncoding === undefined) {
|
2018-02-14 23:42:57 +01:00
|
|
|
if (typeof encoding !== 'string') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('encoding', 'string', encoding);
|
2018-02-14 23:42:57 +01:00
|
|
|
}
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
2016-01-27 00:37:31 +01:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2016-11-29 12:03:24 +01:00
|
|
|
if (val.length === 0) {
|
2018-02-14 23:42:57 +01:00
|
|
|
// If val === '' default to zero.
|
2016-11-29 12:03:24 +01:00
|
|
|
val = 0;
|
|
|
|
} else if (val.length === 1) {
|
2018-02-14 23:42:57 +01:00
|
|
|
// Fast path: If `val` fits into a single byte, use that numeric value.
|
|
|
|
if (normalizedEncoding === 'utf8') {
|
|
|
|
const code = val.charCodeAt(0);
|
|
|
|
if (code < 128) {
|
|
|
|
val = code;
|
|
|
|
}
|
|
|
|
} else if (normalizedEncoding === 'latin1') {
|
|
|
|
val = val.charCodeAt(0);
|
2016-11-29 12:03:24 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 23:42:57 +01:00
|
|
|
} else {
|
|
|
|
encoding = undefined;
|
2016-01-27 00:37:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:42:57 +01:00
|
|
|
if (start === undefined) {
|
|
|
|
start = 0;
|
|
|
|
end = buf.length;
|
|
|
|
} else {
|
|
|
|
// Invalid ranges are not set to a default, so can range check early.
|
|
|
|
if (end === undefined) {
|
|
|
|
if (start < 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2018-02-14 23:42:57 +01:00
|
|
|
end = buf.length;
|
|
|
|
} else {
|
|
|
|
if (start < 0 || end > buf.length || end < 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2018-02-14 23:42:57 +01:00
|
|
|
end = end >>> 0;
|
|
|
|
}
|
|
|
|
start = start >>> 0;
|
|
|
|
if (start >= end)
|
|
|
|
return buf;
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2018-02-14 23:42:57 +01:00
|
|
|
const res = bindingFill(buf, val, start, end, encoding);
|
|
|
|
if (res < 0) {
|
|
|
|
if (res === -1)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_VALUE('value', val);
|
|
|
|
throw new ERR_INDEX_OUT_OF_RANGE();
|
2018-02-14 23:42:57 +01:00
|
|
|
}
|
2017-12-02 16:04:50 +01:00
|
|
|
|
2018-02-14 23:42:57 +01:00
|
|
|
return buf;
|
2017-12-02 16:28:35 +01:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.prototype.write = function write(string, offset, length, encoding) {
|
2015-06-19 21:00:19 +02:00
|
|
|
// Buffer#write(string);
|
|
|
|
if (offset === undefined) {
|
2017-04-10 11:07:58 +02:00
|
|
|
return this.utf8Write(string, 0, this.length);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
|
|
|
// Buffer#write(string, encoding)
|
|
|
|
} else if (length === undefined && typeof offset === 'string') {
|
|
|
|
encoding = offset;
|
|
|
|
length = this.length;
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
// Buffer#write(string, offset[, length][, encoding])
|
|
|
|
} else if (isFinite(offset)) {
|
|
|
|
offset = offset >>> 0;
|
|
|
|
if (isFinite(length)) {
|
|
|
|
length = length >>> 0;
|
|
|
|
} else {
|
|
|
|
encoding = length;
|
|
|
|
length = undefined;
|
|
|
|
}
|
2017-04-10 11:07:58 +02:00
|
|
|
|
|
|
|
var remaining = this.length - offset;
|
|
|
|
if (length === undefined || length > remaining)
|
|
|
|
length = remaining;
|
|
|
|
|
|
|
|
if (string.length > 0 && (length < 0 || offset < 0))
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_BUFFER_OUT_OF_BOUNDS('length', true);
|
2015-06-19 21:00:19 +02:00
|
|
|
} else {
|
2016-02-03 01:38:36 +01:00
|
|
|
// if someone is still calling the obsolete form of write(), tell them.
|
|
|
|
// we don't want eg buf.write("foo", "utf8", 10) to silently turn into
|
|
|
|
// buf.write("foo", "utf8"), so we can't ignore extra args
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_NO_LONGER_SUPPORTED(
|
2017-07-13 21:17:45 +02:00
|
|
|
'Buffer.write(string, encoding, offset[, length])'
|
|
|
|
);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:07:58 +02:00
|
|
|
if (!encoding) return this.utf8Write(string, offset, length);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2017-04-10 11:07:58 +02:00
|
|
|
encoding += '';
|
|
|
|
switch (encoding.length) {
|
|
|
|
case 4:
|
|
|
|
if (encoding === 'utf8') return this.utf8Write(string, offset, length);
|
|
|
|
if (encoding === 'ucs2') return this.ucs2Write(string, offset, length);
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'utf8') return this.utf8Write(string, offset, length);
|
|
|
|
if (encoding === 'ucs2') return this.ucs2Write(string, offset, length);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (encoding === 'utf-8') return this.utf8Write(string, offset, length);
|
|
|
|
if (encoding === 'ascii') return this.asciiWrite(string, offset, length);
|
|
|
|
if (encoding === 'ucs-2') return this.ucs2Write(string, offset, length);
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'utf-8') return this.utf8Write(string, offset, length);
|
|
|
|
if (encoding === 'ascii') return this.asciiWrite(string, offset, length);
|
|
|
|
if (encoding === 'ucs-2') return this.ucs2Write(string, offset, length);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
|
|
|
|
return this.ucs2Write(string, offset, length);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
|
|
|
|
return this.ucs2Write(string, offset, length);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (encoding === 'latin1' || encoding === 'binary')
|
2016-06-02 18:55:36 +02:00
|
|
|
return this.latin1Write(string, offset, length);
|
2017-04-10 11:07:58 +02:00
|
|
|
if (encoding === 'base64')
|
2015-06-19 21:00:19 +02:00
|
|
|
return this.base64Write(string, offset, length);
|
2017-04-10 11:07:58 +02:00
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
if (encoding === 'latin1' || encoding === 'binary')
|
|
|
|
return this.latin1Write(string, offset, length);
|
|
|
|
if (encoding === 'base64')
|
|
|
|
return this.base64Write(string, offset, length);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
|
|
|
|
return this.hexWrite(string, offset, length);
|
|
|
|
break;
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer.prototype.toJSON = function toJSON() {
|
2017-03-07 17:25:58 +01:00
|
|
|
if (this.length > 0) {
|
|
|
|
const data = new Array(this.length);
|
2017-01-19 08:55:21 +01:00
|
|
|
for (var i = 0; i < this.length; ++i)
|
|
|
|
data[i] = this[i];
|
|
|
|
return { type: 'Buffer', data };
|
|
|
|
} else {
|
|
|
|
return { type: 'Buffer', data: [] };
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
function adjustOffset(offset, length) {
|
2016-10-28 16:35:58 +02:00
|
|
|
// Use Math.trunc() to convert offset to an integer value that can be larger
|
|
|
|
// than an Int32. Hence, don't use offset | 0 or similar techniques.
|
|
|
|
offset = Math.trunc(offset);
|
2018-02-13 00:56:35 +01:00
|
|
|
if (offset === 0) {
|
2016-05-20 11:50:53 +02:00
|
|
|
return 0;
|
2018-02-13 00:56:35 +01:00
|
|
|
}
|
|
|
|
if (offset < 0) {
|
2016-05-20 11:50:53 +02:00
|
|
|
offset += length;
|
|
|
|
return offset > 0 ? offset : 0;
|
|
|
|
}
|
2018-02-13 00:56:35 +01:00
|
|
|
if (offset < length) {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
return Number.isNaN(offset) ? 0 : length;
|
2016-05-20 11:50:53 +02:00
|
|
|
}
|
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
Buffer.prototype.slice = function slice(start, end) {
|
2016-05-20 11:50:53 +02:00
|
|
|
const srcLength = this.length;
|
|
|
|
start = adjustOffset(start, srcLength);
|
|
|
|
end = end !== undefined ? adjustOffset(end, srcLength) : srcLength;
|
|
|
|
const newLength = end > start ? end - start : 0;
|
|
|
|
return new FastBuffer(this.buffer, this.byteOffset + start, newLength);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-06-04 22:38:08 +02:00
|
|
|
function swap(b, n, m) {
|
|
|
|
const i = b[n];
|
|
|
|
b[n] = b[m];
|
|
|
|
b[m] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer.prototype.swap16 = function swap16() {
|
|
|
|
// For Buffer.length < 128, it's generally faster to
|
|
|
|
// do the swap in javascript. For larger buffers,
|
|
|
|
// dropping down to the native code is faster.
|
|
|
|
const len = this.length;
|
|
|
|
if (len % 2 !== 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_BUFFER_SIZE('16-bits');
|
2016-06-04 22:38:08 +02:00
|
|
|
if (len < 128) {
|
|
|
|
for (var i = 0; i < len; i += 2)
|
|
|
|
swap(this, i, i + 1);
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
return _swap16(this);
|
2016-06-04 22:38:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Buffer.prototype.swap32 = function swap32() {
|
|
|
|
// For Buffer.length < 192, it's generally faster to
|
|
|
|
// do the swap in javascript. For larger buffers,
|
|
|
|
// dropping down to the native code is faster.
|
|
|
|
const len = this.length;
|
|
|
|
if (len % 4 !== 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_BUFFER_SIZE('32-bits');
|
2016-06-04 22:38:08 +02:00
|
|
|
if (len < 192) {
|
|
|
|
for (var i = 0; i < len; i += 4) {
|
|
|
|
swap(this, i, i + 3);
|
|
|
|
swap(this, i + 1, i + 2);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
return _swap32(this);
|
2016-06-04 22:38:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Buffer.prototype.swap64 = function swap64() {
|
|
|
|
// For Buffer.length < 192, it's generally faster to
|
|
|
|
// do the swap in javascript. For larger buffers,
|
|
|
|
// dropping down to the native code is faster.
|
|
|
|
const len = this.length;
|
|
|
|
if (len % 8 !== 0)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_BUFFER_SIZE('64-bits');
|
2016-06-04 22:38:08 +02:00
|
|
|
if (len < 192) {
|
|
|
|
for (var i = 0; i < len; i += 8) {
|
|
|
|
swap(this, i, i + 7);
|
|
|
|
swap(this, i + 1, i + 6);
|
|
|
|
swap(this, i + 2, i + 5);
|
|
|
|
swap(this, i + 3, i + 4);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
return _swap64(this);
|
2016-06-04 22:38:08 +02:00
|
|
|
};
|
2016-08-17 19:56:24 +02:00
|
|
|
|
|
|
|
Buffer.prototype.toLocaleString = Buffer.prototype.toString;
|
2016-10-11 23:12:31 +02:00
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
let transcode;
|
|
|
|
if (process.binding('config').hasIntl) {
|
|
|
|
const {
|
|
|
|
icuErrName,
|
|
|
|
transcode: _transcode
|
|
|
|
} = process.binding('icu');
|
|
|
|
|
|
|
|
// Transcodes the Buffer from one encoding to another, returning a new
|
|
|
|
// Buffer instance.
|
|
|
|
transcode = function transcode(source, fromEncoding, toEncoding) {
|
2017-02-03 16:05:28 +01:00
|
|
|
if (!isUint8Array(source)) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('source',
|
|
|
|
['Buffer', 'Uint8Array'], source);
|
2017-02-03 16:05:28 +01:00
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
if (source.length === 0) return Buffer.alloc(0);
|
|
|
|
|
|
|
|
fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
|
|
|
|
toEncoding = normalizeEncoding(toEncoding) || toEncoding;
|
|
|
|
const result = _transcode(source, fromEncoding, toEncoding);
|
|
|
|
if (typeof result !== 'number')
|
|
|
|
return result;
|
|
|
|
|
|
|
|
const code = icuErrName(result);
|
|
|
|
const err = new Error(`Unable to transcode Buffer [${code}]`);
|
|
|
|
err.code = code;
|
|
|
|
err.errno = result;
|
|
|
|
throw err;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = {
|
|
|
|
Buffer,
|
|
|
|
SlowBuffer,
|
|
|
|
transcode,
|
|
|
|
INSPECT_MAX_BYTES: 50,
|
|
|
|
|
|
|
|
// Legacy
|
|
|
|
kMaxLength,
|
|
|
|
kStringMaxLength
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperty(exports, 'constants', {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
value: constants
|
|
|
|
});
|