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';
|
|
|
|
|
2019-11-03 12:45:32 +01:00
|
|
|
const {
|
2019-11-23 10:09:05 +01:00
|
|
|
Array,
|
|
|
|
ArrayIsArray,
|
2020-01-02 19:19:02 +01:00
|
|
|
Error,
|
2019-11-22 18:04:46 +01:00
|
|
|
MathFloor,
|
|
|
|
MathMin,
|
|
|
|
MathTrunc,
|
2019-11-27 19:59:29 +01:00
|
|
|
NumberIsNaN,
|
|
|
|
NumberMAX_SAFE_INTEGER,
|
|
|
|
NumberMIN_SAFE_INTEGER,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectDefineProperty,
|
2020-01-24 08:44:15 +01:00
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetPrototypeOf,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf,
|
2019-12-13 22:47:39 +01:00
|
|
|
SymbolSpecies,
|
2019-12-11 20:45:08 +01:00
|
|
|
SymbolToPrimitive,
|
2020-01-24 08:44:15 +01:00
|
|
|
Uint8ArrayPrototype,
|
2019-11-03 12:45:32 +01:00
|
|
|
} = primordials;
|
2019-03-31 13:30:12 +02:00
|
|
|
|
2017-06-20 01:06:02 +02:00
|
|
|
const {
|
|
|
|
byteLengthUtf8,
|
|
|
|
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,
|
2018-12-31 09:11:01 +01:00
|
|
|
kStringMaxLength,
|
2018-12-31 09:43:26 +01:00
|
|
|
zeroFill: bindingZeroFill
|
2018-08-17 10:33:45 +02:00
|
|
|
} = internalBinding('buffer');
|
2018-12-20 03:43:02 +01:00
|
|
|
const {
|
2020-04-10 12:43:15 +02:00
|
|
|
arraybuffer_untransferable_private_symbol,
|
2018-12-20 03:43:02 +01:00
|
|
|
getOwnNonIndexProperties,
|
|
|
|
propertyFilter: {
|
|
|
|
ALL_PROPERTIES,
|
|
|
|
ONLY_ENUMERABLE
|
2020-04-10 12:43:15 +02:00
|
|
|
},
|
|
|
|
setHiddenValue,
|
2018-12-20 03:43:02 +01:00
|
|
|
} = internalBinding('util');
|
2017-06-20 01:06:02 +02:00
|
|
|
const {
|
|
|
|
customInspectSymbol,
|
2018-03-22 00:30:21 +01:00
|
|
|
isInsideNodeModules,
|
2017-06-20 01:06:02 +02:00
|
|
|
normalizeEncoding,
|
|
|
|
kIsEncodingSymbol
|
|
|
|
} = require('internal/util');
|
2017-09-28 09:16:41 +02:00
|
|
|
const {
|
2018-12-20 17:35:40 +01:00
|
|
|
isAnyArrayBuffer,
|
2017-09-28 09:16:41 +02:00
|
|
|
isArrayBufferView,
|
|
|
|
isUint8Array
|
|
|
|
} = require('internal/util/types');
|
2018-12-20 03:43:02 +01:00
|
|
|
const {
|
2019-04-03 19:54:37 +02:00
|
|
|
inspect: utilInspect
|
2018-12-20 03:43:02 +01:00
|
|
|
} = require('internal/util/inspect');
|
2019-08-20 05:25:28 +02:00
|
|
|
const { encodings } = internalBinding('string_decoder');
|
|
|
|
|
2018-12-11 18:24:59 +01:00
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
2019-03-18 02:29:39 +01:00
|
|
|
codes: {
|
|
|
|
ERR_BUFFER_OUT_OF_BOUNDS,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
ERR_INVALID_BUFFER_SIZE,
|
|
|
|
ERR_INVALID_OPT_VALUE,
|
2019-08-09 13:29:48 +02:00
|
|
|
ERR_OUT_OF_RANGE,
|
2019-03-18 02:29:39 +01:00
|
|
|
ERR_UNKNOWN_ENCODING
|
|
|
|
},
|
|
|
|
hideStackFrames
|
|
|
|
} = require('internal/errors');
|
2019-04-02 00:01:29 +02:00
|
|
|
const {
|
2020-04-10 20:25:48 +02:00
|
|
|
validateBuffer,
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32,
|
|
|
|
validateString
|
|
|
|
} = require('internal/validators');
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2018-12-31 09:43:26 +01:00
|
|
|
const {
|
|
|
|
FastBuffer,
|
|
|
|
addBufferPrototypeMethods
|
|
|
|
} = require('internal/buffer');
|
2017-06-20 01:06:02 +02:00
|
|
|
|
2020-01-24 08:44:15 +01:00
|
|
|
const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8ArrayPrototype);
|
|
|
|
|
|
|
|
const TypedArrayProto_byteLength =
|
|
|
|
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
|
|
|
|
'byteLength').get;
|
|
|
|
const TypedArrayFill = TypedArrayPrototype.fill;
|
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
FastBuffer.prototype.constructor = Buffer;
|
|
|
|
Buffer.prototype = FastBuffer.prototype;
|
2018-12-31 09:43:26 +01:00
|
|
|
addBufferPrototypeMethods(Buffer.prototype);
|
2018-12-31 09:11:01 +01:00
|
|
|
|
2019-11-03 12:45:32 +01:00
|
|
|
const constants = ObjectDefineProperties({}, {
|
2017-06-05 11:58:20 +02:00
|
|
|
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;
|
2018-12-29 14:17:40 +01:00
|
|
|
let poolSize, poolOffset, allocPool;
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2018-12-31 09:11:01 +01:00
|
|
|
// A toggle used to access the zero fill setting of the array buffer allocator
|
|
|
|
// in C++.
|
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.
|
2018-12-31 09:11:01 +01:00
|
|
|
const zeroFill = bindingZeroFill || [0];
|
2015-09-15 00:31:10 +02:00
|
|
|
|
2019-11-03 12:45:32 +01:00
|
|
|
const encodingsMap = ObjectCreate(null);
|
2019-08-20 05:25:28 +02:00
|
|
|
for (let i = 0; i < encodings.length; ++i)
|
|
|
|
encodingsMap[encodings[i]] = i;
|
|
|
|
|
2016-05-20 11:50:53 +02:00
|
|
|
function createUnsafeBuffer(size) {
|
|
|
|
zeroFill[0] = 0;
|
2016-06-01 15:53:01 +02:00
|
|
|
try {
|
2019-02-25 04:41:53 +01:00
|
|
|
return new FastBuffer(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;
|
2019-02-25 04:41:53 +01:00
|
|
|
allocPool = createUnsafeBuffer(poolSize).buffer;
|
2020-04-10 12:43:15 +02:00
|
|
|
setHiddenValue(allocPool, arraybuffer_untransferable_private_symbol, true);
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 00:30:21 +01:00
|
|
|
let bufferWarningAlreadyEmitted = false;
|
|
|
|
let nodeModulesCheckCounter = 0;
|
2018-04-02 07:03:59 +02:00
|
|
|
const bufferWarning = 'Buffer() is deprecated due to security and usability ' +
|
|
|
|
'issues. Please use the Buffer.alloc(), ' +
|
|
|
|
'Buffer.allocUnsafe(), or Buffer.from() methods instead.';
|
2017-03-11 21:32:25 +01:00
|
|
|
|
|
|
|
function showFlaggedDeprecation() {
|
2018-03-22 00:30:21 +01:00
|
|
|
if (bufferWarningAlreadyEmitted ||
|
|
|
|
++nodeModulesCheckCounter > 10000 ||
|
2018-12-11 18:24:59 +01:00
|
|
|
(!require('internal/options').getOptionValue('--pending-deprecation') &&
|
2018-03-22 00:30:21 +01:00
|
|
|
isInsideNodeModules())) {
|
|
|
|
// We don't emit a warning, because we either:
|
|
|
|
// - Already did so, or
|
|
|
|
// - Already checked too many times whether a call is coming
|
|
|
|
// from node_modules and want to stop slowing down things, or
|
|
|
|
// - We aren't running with `--pending-deprecation` enabled,
|
|
|
|
// and the code is inside `node_modules`.
|
|
|
|
return;
|
2017-03-11 21:32:25 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 00:30:21 +01:00
|
|
|
process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005');
|
|
|
|
bufferWarningAlreadyEmitted = true;
|
|
|
|
}
|
2017-03-11 21:32:25 +01:00
|
|
|
|
2019-08-09 13:29:48 +02:00
|
|
|
function toInteger(n, defaultVal) {
|
|
|
|
n = +n;
|
2019-11-27 19:59:29 +01:00
|
|
|
if (!NumberIsNaN(n) &&
|
|
|
|
n >= NumberMIN_SAFE_INTEGER &&
|
|
|
|
n <= NumberMAX_SAFE_INTEGER) {
|
2019-11-03 12:45:32 +01:00
|
|
|
return ((n % 1) === 0 ? n : MathFloor(n));
|
2019-08-09 13:29:48 +02:00
|
|
|
}
|
|
|
|
return defaultVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _copy(source, target, targetStart, sourceStart, sourceEnd) {
|
|
|
|
if (!isUint8Array(source))
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
|
|
|
|
if (!isUint8Array(target))
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
|
|
|
|
|
|
|
|
if (targetStart === undefined) {
|
|
|
|
targetStart = 0;
|
|
|
|
} else {
|
|
|
|
targetStart = toInteger(targetStart, 0);
|
|
|
|
if (targetStart < 0)
|
|
|
|
throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceStart === undefined) {
|
|
|
|
sourceStart = 0;
|
|
|
|
} else {
|
|
|
|
sourceStart = toInteger(sourceStart, 0);
|
|
|
|
if (sourceStart < 0)
|
|
|
|
throw new ERR_OUT_OF_RANGE('sourceStart', '>= 0', sourceStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceEnd === undefined) {
|
|
|
|
sourceEnd = source.length;
|
|
|
|
} else {
|
|
|
|
sourceEnd = toInteger(sourceEnd, 0);
|
|
|
|
if (sourceEnd < 0)
|
|
|
|
throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetStart >= target.length || sourceStart >= sourceEnd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sourceStart > source.length) {
|
|
|
|
throw new ERR_OUT_OF_RANGE('sourceStart',
|
|
|
|
`<= ${source.length}`,
|
|
|
|
sourceStart);
|
|
|
|
}
|
|
|
|
|
2020-01-26 16:01:21 +01:00
|
|
|
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
|
2019-08-09 13:29:48 +02:00
|
|
|
if (sourceEnd - sourceStart > target.length - targetStart)
|
|
|
|
sourceEnd = sourceStart + target.length - targetStart;
|
|
|
|
|
|
|
|
let nb = sourceEnd - sourceStart;
|
|
|
|
const targetLen = target.length - targetStart;
|
|
|
|
const sourceLen = source.length - sourceStart;
|
|
|
|
if (nb > targetLen)
|
|
|
|
nb = targetLen;
|
|
|
|
if (nb > sourceLen)
|
|
|
|
nb = sourceLen;
|
|
|
|
|
2020-05-02 19:30:44 +02:00
|
|
|
if (sourceStart !== 0 || sourceEnd < source.length)
|
2019-08-09 13:29:48 +02:00
|
|
|
source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb);
|
|
|
|
|
|
|
|
target.set(source, targetStart);
|
|
|
|
|
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
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
|
2018-03-25 16:27:38 +02:00
|
|
|
*/
|
2016-01-26 00:00:06 +01:00
|
|
|
function Buffer(arg, encodingOrOffset, length) {
|
2018-03-22 00:30:21 +01:00
|
|
|
showFlaggedDeprecation();
|
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
|
|
|
|
2019-12-13 22:47:39 +01:00
|
|
|
ObjectDefineProperty(Buffer, SymbolSpecies, {
|
2017-03-11 21:32:25 +01:00
|
|
|
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]])
|
2018-03-25 16:27:38 +02:00
|
|
|
*/
|
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
|
|
|
|
2019-03-20 17:10:06 +01:00
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
|
if (isAnyArrayBuffer(value))
|
|
|
|
return fromArrayBuffer(value, encodingOrOffset, length);
|
|
|
|
|
|
|
|
const valueOf = value.valueOf && value.valueOf();
|
2019-09-23 08:14:35 +02:00
|
|
|
if (valueOf != null &&
|
|
|
|
valueOf !== value &&
|
|
|
|
(typeof valueOf === 'string' || typeof valueOf === 'object')) {
|
|
|
|
return from(valueOf, encodingOrOffset, length);
|
|
|
|
}
|
2019-03-20 17:10:06 +01:00
|
|
|
|
|
|
|
const b = fromObject(value);
|
|
|
|
if (b)
|
|
|
|
return b;
|
|
|
|
|
2019-12-11 20:45:08 +01:00
|
|
|
if (typeof value[SymbolToPrimitive] === 'function') {
|
2019-09-23 08:14:35 +02:00
|
|
|
const primitive = value[SymbolToPrimitive]('string');
|
|
|
|
if (typeof primitive === 'string') {
|
|
|
|
return fromString(primitive, encodingOrOffset);
|
|
|
|
}
|
2019-03-20 17:10:06 +01:00
|
|
|
}
|
2017-06-16 21:35:15 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-03-29 22:02:52 +02:00
|
|
|
// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
|
|
|
|
// Buffer() constructor. Must use arrow function syntax to avoid automatically
|
|
|
|
// adding a `prototype` property and making the function a constructor.
|
|
|
|
//
|
|
|
|
// Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of
|
|
|
|
// Refs: https://esdiscuss.org/topic/isconstructor#content-11
|
|
|
|
const of = (...items) => {
|
|
|
|
const newObj = createUnsafeBuffer(items.length);
|
2019-11-06 12:26:54 +01:00
|
|
|
for (let k = 0; k < items.length; k++)
|
2018-03-29 22:02:52 +02:00
|
|
|
newObj[k] = items[k];
|
|
|
|
return newObj;
|
|
|
|
};
|
|
|
|
Buffer.of = of;
|
|
|
|
|
2019-11-03 12:45:32 +01:00
|
|
|
ObjectSetPrototypeOf(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.
|
2019-03-18 02:29:39 +01:00
|
|
|
const assertSize = hideStackFrames((size) => {
|
2017-06-20 23:37:00 +02:00
|
|
|
if (typeof size !== 'number') {
|
2019-03-18 02:29:39 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2019-03-18 02:29:39 +01:00
|
|
|
if (!(size >= 0 && size <= kMaxLength)) {
|
|
|
|
throw new ERR_INVALID_OPT_VALUE.RangeError('size', size);
|
2016-03-21 20:38:08 +01:00
|
|
|
}
|
2019-03-18 02:29:39 +01:00
|
|
|
});
|
2016-03-21 20:38:08 +01:00
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
/**
|
|
|
|
* Creates a new filled Buffer instance.
|
|
|
|
* alloc(size[, fill[, encoding]])
|
2018-03-25 16:27:38 +02:00
|
|
|
*/
|
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-07-27 00:14:25 +02:00
|
|
|
if (fill !== undefined && fill !== 0 && size > 0) {
|
2018-08-10 03:45:40 +02:00
|
|
|
const buf = createUnsafeBuffer(size);
|
|
|
|
return _fill(buf, fill, 0, buf.length, 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.
|
2018-03-25 16:27:38 +02:00
|
|
|
*/
|
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.
|
2018-03-25 16:27:38 +02:00
|
|
|
*/
|
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) {
|
2018-12-29 14:17:40 +01:00
|
|
|
assertSize(length);
|
|
|
|
return createUnsafeBuffer(length);
|
2015-09-15 00:31:10 +02:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2019-11-03 12:45:32 +01:00
|
|
|
ObjectSetPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
|
|
|
|
ObjectSetPrototypeOf(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();
|
2018-12-29 14:17:40 +01:00
|
|
|
const 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;
|
|
|
|
}
|
2018-12-20 03:40:40 +01:00
|
|
|
return createUnsafeBuffer(size);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 05:25:28 +02:00
|
|
|
function fromStringFast(string, ops) {
|
|
|
|
const length = ops.byteLength(string);
|
2016-05-15 18:04:03 +02:00
|
|
|
|
2015-06-19 21:00:19 +02:00
|
|
|
if (length >= (Buffer.poolSize >>> 1))
|
2019-08-20 05:25:28 +02:00
|
|
|
return createFromString(string, ops.encodingVal);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
|
|
|
if (length > (poolSize - poolOffset))
|
|
|
|
createPool();
|
2018-12-29 14:17:40 +01:00
|
|
|
let b = new FastBuffer(allocPool, poolOffset, length);
|
2019-08-20 05:25:28 +02:00
|
|
|
const actual = ops.write(b, string, 0, length);
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-20 05:25:28 +02:00
|
|
|
function fromString(string, encoding) {
|
|
|
|
let ops;
|
|
|
|
if (typeof encoding !== 'string' || encoding.length === 0) {
|
|
|
|
if (string.length === 0)
|
|
|
|
return new FastBuffer();
|
|
|
|
ops = encodingOps.utf8;
|
|
|
|
encoding = undefined;
|
|
|
|
} else {
|
|
|
|
ops = getEncodingOps(encoding);
|
|
|
|
if (ops === undefined)
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
if (string.length === 0)
|
|
|
|
return new FastBuffer();
|
|
|
|
}
|
|
|
|
return fromStringFast(string, ops);
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
function fromArrayBuffer(obj, byteOffset, length) {
|
2019-03-22 03:44:26 +01:00
|
|
|
// Convert byteOffset to integer
|
2017-04-10 11:03:57 +02:00
|
|
|
if (byteOffset === undefined) {
|
|
|
|
byteOffset = 0;
|
|
|
|
} else {
|
|
|
|
byteOffset = +byteOffset;
|
2019-11-27 19:59:29 +01:00
|
|
|
if (NumberIsNaN(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
|
|
|
}
|
|
|
|
|
2020-02-02 16:49:30 +01:00
|
|
|
function fromArrayLike(obj) {
|
|
|
|
if (obj.length <= 0)
|
|
|
|
return new FastBuffer();
|
|
|
|
if (obj.length < (Buffer.poolSize >>> 1)) {
|
|
|
|
if (obj.length > (poolSize - poolOffset))
|
|
|
|
createPool();
|
|
|
|
const b = new FastBuffer(allocPool, poolOffset, obj.length);
|
|
|
|
b.set(obj, 0);
|
|
|
|
poolOffset += obj.length;
|
|
|
|
alignPool();
|
2015-06-19 21:00:19 +02:00
|
|
|
return b;
|
|
|
|
}
|
2020-02-02 16:49:30 +01:00
|
|
|
return new FastBuffer(obj);
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2020-02-02 16:49:30 +01:00
|
|
|
function fromObject(obj) {
|
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
|
|
|
|
2019-11-23 10:09:05 +01:00
|
|
|
if (obj.type === 'Buffer' && ArrayIsArray(obj.data)) {
|
2017-08-30 19:18:40 +02:00
|
|
|
return fromArrayLike(obj.data);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static methods
|
|
|
|
|
|
|
|
Buffer.isBuffer = function isBuffer(b) {
|
|
|
|
return b instanceof Buffer;
|
|
|
|
};
|
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
Buffer.compare = function compare(buf1, buf2) {
|
|
|
|
if (!isUint8Array(buf1)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('buf1', ['Buffer', 'Uint8Array'], buf1);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
if (!isUint8Array(buf2)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('buf2', ['Buffer', 'Uint8Array'], buf2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf1 === buf2) {
|
2015-06-19 21:00:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
return _compare(buf1, buf2);
|
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) {
|
2019-11-23 10:09:05 +01:00
|
|
|
if (!ArrayIsArray(list)) {
|
2019-04-02 05:00:10 +02:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('list', 'Array', list);
|
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;
|
2020-05-19 16:51:55 +02:00
|
|
|
for (let i = 0; i < list.length; i++) {
|
2019-02-17 14:59:10 +01:00
|
|
|
if (list[i].length) {
|
|
|
|
length += list[i].length;
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
} else {
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(length, 'length', 0);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 14:17:40 +01:00
|
|
|
const buffer = Buffer.allocUnsafe(length);
|
|
|
|
let pos = 0;
|
2020-05-19 16:51:55 +02:00
|
|
|
for (let i = 0; i < list.length; i++) {
|
2018-12-29 14:17:40 +01:00
|
|
|
const buf = list[i];
|
2017-02-03 16:05:28 +01:00
|
|
|
if (!isUint8Array(buf)) {
|
2018-03-19 14:18:50 +01:00
|
|
|
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
|
|
|
|
// Instead, find the proper error code for this.
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2019-08-19 05:24:49 +02:00
|
|
|
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
|
2017-02-03 16:05:28 +01:00
|
|
|
}
|
2020-01-26 16:01:21 +01:00
|
|
|
pos += _copyActual(buf, buffer, pos, 0, buf.length);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2020-01-26 16:01:21 +01:00
|
|
|
TypedArrayFill.call(buffer, 0, pos, length);
|
2016-09-16 07:07:23 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-20 05:25:28 +02:00
|
|
|
const encodingOps = {
|
|
|
|
utf8: {
|
|
|
|
encoding: 'utf8',
|
|
|
|
encodingVal: encodingsMap.utf8,
|
|
|
|
byteLength: byteLengthUtf8,
|
|
|
|
write: (buf, string, offset, len) => buf.utf8Write(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.utf8Slice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir)
|
|
|
|
},
|
|
|
|
ucs2: {
|
|
|
|
encoding: 'ucs2',
|
|
|
|
encodingVal: encodingsMap.utf16le,
|
|
|
|
byteLength: (string) => string.length * 2,
|
|
|
|
write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.ucs2Slice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir)
|
|
|
|
},
|
|
|
|
utf16le: {
|
|
|
|
encoding: 'utf16le',
|
|
|
|
encodingVal: encodingsMap.utf16le,
|
|
|
|
byteLength: (string) => string.length * 2,
|
|
|
|
write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.ucs2Slice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir)
|
|
|
|
},
|
|
|
|
latin1: {
|
|
|
|
encoding: 'latin1',
|
|
|
|
encodingVal: encodingsMap.latin1,
|
|
|
|
byteLength: (string) => string.length,
|
|
|
|
write: (buf, string, offset, len) => buf.latin1Write(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.latin1Slice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir)
|
|
|
|
},
|
|
|
|
ascii: {
|
|
|
|
encoding: 'ascii',
|
|
|
|
encodingVal: encodingsMap.ascii,
|
|
|
|
byteLength: (string) => string.length,
|
|
|
|
write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.asciiSlice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfBuffer(buf,
|
|
|
|
fromStringFast(val, encodingOps.ascii),
|
|
|
|
byteOffset,
|
|
|
|
encodingsMap.ascii,
|
|
|
|
dir)
|
|
|
|
},
|
|
|
|
base64: {
|
|
|
|
encoding: 'base64',
|
|
|
|
encodingVal: encodingsMap.base64,
|
|
|
|
byteLength: (string) => base64ByteLength(string, string.length),
|
|
|
|
write: (buf, string, offset, len) => buf.base64Write(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.base64Slice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfBuffer(buf,
|
|
|
|
fromStringFast(val, encodingOps.base64),
|
|
|
|
byteOffset,
|
|
|
|
encodingsMap.base64,
|
|
|
|
dir)
|
|
|
|
},
|
|
|
|
hex: {
|
|
|
|
encoding: 'hex',
|
|
|
|
encodingVal: encodingsMap.hex,
|
|
|
|
byteLength: (string) => string.length >>> 1,
|
|
|
|
write: (buf, string, offset, len) => buf.hexWrite(string, offset, len),
|
|
|
|
slice: (buf, start, end) => buf.hexSlice(start, end),
|
|
|
|
indexOf: (buf, val, byteOffset, dir) =>
|
|
|
|
indexOfBuffer(buf,
|
|
|
|
fromStringFast(val, encodingOps.hex),
|
|
|
|
byteOffset,
|
|
|
|
encodingsMap.hex,
|
|
|
|
dir)
|
2016-02-16 04:08:49 +01:00
|
|
|
}
|
2019-08-20 05:25:28 +02:00
|
|
|
};
|
|
|
|
function getEncodingOps(encoding) {
|
2017-04-10 11:03:57 +02:00
|
|
|
encoding += '';
|
|
|
|
switch (encoding.length) {
|
|
|
|
case 4:
|
2019-08-20 05:25:28 +02:00
|
|
|
if (encoding === 'utf8') return encodingOps.utf8;
|
|
|
|
if (encoding === 'ucs2') return encodingOps.ucs2;
|
2017-04-10 11:03:57 +02:00
|
|
|
encoding = encoding.toLowerCase();
|
2019-08-20 05:25:28 +02:00
|
|
|
if (encoding === 'utf8') return encodingOps.utf8;
|
|
|
|
if (encoding === 'ucs2') return encodingOps.ucs2;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
2019-08-20 05:25:28 +02:00
|
|
|
if (encoding === 'utf-8') return encodingOps.utf8;
|
|
|
|
if (encoding === 'ascii') return encodingOps.ascii;
|
|
|
|
if (encoding === 'ucs-2') return encodingOps.ucs2;
|
2017-04-10 11:03:57 +02:00
|
|
|
encoding = encoding.toLowerCase();
|
2019-08-20 05:25:28 +02:00
|
|
|
if (encoding === 'utf-8') return encodingOps.utf8;
|
|
|
|
if (encoding === 'ascii') return encodingOps.ascii;
|
|
|
|
if (encoding === 'ucs-2') return encodingOps.ucs2;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
|
2019-08-20 05:25:28 +02:00
|
|
|
return encodingOps.utf16le;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
|
2019-08-20 05:25:28 +02:00
|
|
|
return encodingOps.utf16le;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
|
|
|
case 6:
|
2019-08-20 05:25:28 +02:00
|
|
|
if (encoding === 'latin1' || encoding === 'binary')
|
|
|
|
return encodingOps.latin1;
|
|
|
|
if (encoding === 'base64') return encodingOps.base64;
|
2017-04-10 11:03:57 +02:00
|
|
|
encoding = encoding.toLowerCase();
|
2019-08-20 05:25:28 +02:00
|
|
|
if (encoding === 'latin1' || encoding === 'binary')
|
|
|
|
return encodingOps.latin1;
|
|
|
|
if (encoding === 'base64') return encodingOps.base64;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
|
2019-08-20 05:25:28 +02:00
|
|
|
return encodingOps.hex;
|
2017-04-10 11:03:57 +02:00
|
|
|
break;
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
2019-08-20 05:25:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function byteLength(string, encoding) {
|
|
|
|
if (typeof string !== 'string') {
|
|
|
|
if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
|
|
|
|
return string.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'string', ['string', 'Buffer', 'ArrayBuffer'], string
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const len = string.length;
|
|
|
|
const mustMatch = (arguments.length > 2 && arguments[2] === true);
|
|
|
|
if (!mustMatch && len === 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!encoding)
|
|
|
|
return (mustMatch ? -1 : byteLengthUtf8(string));
|
|
|
|
|
|
|
|
const ops = getEncodingOps(encoding);
|
|
|
|
if (ops === undefined)
|
|
|
|
return (mustMatch ? -1 : byteLengthUtf8(string));
|
|
|
|
return ops.byteLength(string);
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer.byteLength = byteLength;
|
|
|
|
|
|
|
|
// For backwards compatibility.
|
2019-11-03 12:45:32 +01:00
|
|
|
ObjectDefineProperty(Buffer.prototype, 'parent', {
|
2015-06-19 21:00:19 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2019-11-03 12:45:32 +01:00
|
|
|
ObjectDefineProperty(Buffer.prototype, 'offset', {
|
2015-06-19 21:00:19 +02:00
|
|
|
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-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;
|
|
|
|
|
2018-12-20 03:40:40 +01:00
|
|
|
if (start <= 0)
|
2017-04-30 18:53:04 +02:00
|
|
|
start = 0;
|
|
|
|
else if (start >= len)
|
|
|
|
return '';
|
2018-12-20 03:40:40 +01:00
|
|
|
else
|
|
|
|
start |= 0;
|
2017-04-30 18:53:04 +02:00
|
|
|
|
|
|
|
if (end === undefined || end > len)
|
|
|
|
end = len;
|
2018-12-20 03:40:40 +01:00
|
|
|
else
|
|
|
|
end |= 0;
|
2017-04-30 18:53:04 +02:00
|
|
|
|
|
|
|
if (end <= start)
|
|
|
|
return '';
|
2019-08-20 05:25:28 +02:00
|
|
|
|
|
|
|
if (encoding === undefined)
|
|
|
|
return this.utf8Slice(start, end);
|
|
|
|
|
|
|
|
const ops = getEncodingOps(encoding);
|
|
|
|
if (ops === undefined)
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
|
|
|
|
return ops.slice(this, start, end);
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
Buffer.prototype.equals = function equals(otherBuffer) {
|
|
|
|
if (!isUint8Array(otherBuffer)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
|
2017-07-13 21:17:45 +02:00
|
|
|
}
|
2019-08-19 07:43:29 +02:00
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
if (this === otherBuffer)
|
2015-06-19 21:00:19 +02:00
|
|
|
return true;
|
|
|
|
|
2019-08-19 07:43:29 +02:00
|
|
|
if (this.byteLength !== otherBuffer.byteLength)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return this.byteLength === 0 || _compare(this, otherBuffer) === 0;
|
2015-06-19 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2019-04-04 05:36:41 +02:00
|
|
|
let INSPECT_MAX_BYTES = 50;
|
2016-08-19 00:26:36 +02:00
|
|
|
// Override how buffers are presented by util.inspect().
|
2018-12-20 03:40:40 +01:00
|
|
|
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
|
2019-04-04 05:36:41 +02:00
|
|
|
const max = INSPECT_MAX_BYTES;
|
2019-11-03 12:45:32 +01:00
|
|
|
const actualMax = MathMin(max, this.length);
|
2018-08-13 00:47:11 +02:00
|
|
|
const remaining = this.length - max;
|
2018-12-20 03:43:02 +01:00
|
|
|
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
|
2018-08-13 00:47:11 +02:00
|
|
|
if (remaining > 0)
|
|
|
|
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
|
2018-12-20 03:43:02 +01:00
|
|
|
// Inspect special properties as well, if possible.
|
|
|
|
if (ctx) {
|
2019-04-03 19:54:37 +02:00
|
|
|
let extras = false;
|
2018-12-20 03:43:02 +01:00
|
|
|
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
|
2019-04-03 19:54:37 +02:00
|
|
|
const obj = getOwnNonIndexProperties(this, filter).reduce((obj, key) => {
|
|
|
|
extras = true;
|
|
|
|
obj[key] = this[key];
|
|
|
|
return obj;
|
2019-11-03 12:45:32 +01:00
|
|
|
}, ObjectCreate(null));
|
2019-04-03 19:54:37 +02:00
|
|
|
if (extras) {
|
|
|
|
if (this.length !== 0)
|
|
|
|
str += ', ';
|
|
|
|
// '[Object: null prototype] {'.length === 26
|
|
|
|
// This is guarded with a test.
|
|
|
|
str += utilInspect(obj, {
|
|
|
|
...ctx,
|
|
|
|
breakLength: Infinity,
|
|
|
|
compact: true
|
|
|
|
}).slice(27, -2);
|
|
|
|
}
|
2018-12-20 03:43:02 +01:00
|
|
|
}
|
2017-02-28 09:15:07 +01:00
|
|
|
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,
|
2018-09-20 05:04:47 +02:00
|
|
|
targetStart,
|
|
|
|
targetEnd,
|
|
|
|
sourceStart,
|
|
|
|
sourceEnd) {
|
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
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
if (targetStart === undefined)
|
|
|
|
targetStart = 0;
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(targetStart, 'targetStart', 0);
|
2017-01-20 04:26:18 +01:00
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
if (targetEnd === undefined)
|
|
|
|
targetEnd = target.length;
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(targetEnd, 'targetEnd', 0, target.length);
|
2017-01-20 04:26:18 +01:00
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
if (sourceStart === undefined)
|
|
|
|
sourceStart = 0;
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(sourceStart, 'sourceStart', 0);
|
2017-01-20 04:26:18 +01:00
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
if (sourceEnd === undefined)
|
|
|
|
sourceEnd = this.length;
|
2017-01-20 04:26:18 +01:00
|
|
|
else
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(sourceEnd, 'sourceEnd', 0, this.length);
|
2016-03-23 22:27:39 +01:00
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
if (sourceStart >= sourceEnd)
|
|
|
|
return (targetStart >= targetEnd ? 0 : -1);
|
2019-04-02 00:01:29 +02:00
|
|
|
if (targetStart >= targetEnd)
|
2016-03-23 22:27:39 +01:00
|
|
|
return 1;
|
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
return compareOffset(this, target, targetStart, sourceStart, targetEnd,
|
|
|
|
sourceEnd);
|
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) {
|
2020-04-10 20:25:48 +02:00
|
|
|
validateBuffer(buffer);
|
|
|
|
|
2016-01-28 22:12:09 +01:00
|
|
|
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.
|
2019-11-27 19:59:29 +01:00
|
|
|
if (NumberIsNaN(byteOffset)) {
|
2020-04-10 20:25:48 +02:00
|
|
|
byteOffset = dir ? 0 : (buffer.length || buffer.byteLength);
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
dir = !!dir; // Cast to bool.
|
|
|
|
|
2019-08-20 05:25:28 +02:00
|
|
|
if (typeof val === 'number')
|
2018-08-29 16:46:36 +02:00
|
|
|
return indexOfNumber(buffer, val >>> 0, byteOffset, dir);
|
2019-08-20 05:25:28 +02:00
|
|
|
|
|
|
|
let ops;
|
|
|
|
if (encoding === undefined)
|
|
|
|
ops = encodingOps.utf8;
|
|
|
|
else
|
|
|
|
ops = getEncodingOps(encoding);
|
|
|
|
|
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (ops === undefined)
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
return ops.indexOf(buffer, val, byteOffset, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUint8Array(val)) {
|
|
|
|
const encodingVal =
|
|
|
|
(ops === undefined ? encodingsMap.utf8 : ops.encodingVal);
|
|
|
|
return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir);
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
2019-08-20 05:26:11 +02:00
|
|
|
'value', ['number', 'string', 'Buffer', 'Uint8Array'], val
|
2017-07-13 21:17:45 +02:00
|
|
|
);
|
2016-01-28 22:12:09 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 10:10:30 +02:00
|
|
|
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])
|
2018-09-20 05:04:47 +02:00
|
|
|
Buffer.prototype.fill = function fill(value, offset, end, encoding) {
|
|
|
|
return _fill(this, value, offset, end, encoding);
|
2017-12-02 16:28:35 +01:00
|
|
|
};
|
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
function _fill(buf, value, offset, end, encoding) {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
if (offset === undefined || typeof offset === 'string') {
|
|
|
|
encoding = offset;
|
|
|
|
offset = 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-08-03 00:51:02 +02:00
|
|
|
validateString(encoding, 'encoding');
|
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
|
|
|
|
2018-09-20 05:04:47 +02:00
|
|
|
if (value.length === 0) {
|
|
|
|
// If value === '' default to zero.
|
|
|
|
value = 0;
|
|
|
|
} else if (value.length === 1) {
|
|
|
|
// Fast path: If `value` fits into a single byte, use that numeric value.
|
2018-02-14 23:42:57 +01:00
|
|
|
if (normalizedEncoding === 'utf8') {
|
2018-09-20 05:04:47 +02:00
|
|
|
const code = value.charCodeAt(0);
|
2018-02-14 23:42:57 +01:00
|
|
|
if (code < 128) {
|
2018-09-20 05:04:47 +02:00
|
|
|
value = code;
|
2018-02-14 23:42:57 +01:00
|
|
|
}
|
|
|
|
} else if (normalizedEncoding === 'latin1') {
|
2018-09-20 05:04:47 +02:00
|
|
|
value = value.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-09-20 05:04:47 +02:00
|
|
|
if (offset === undefined) {
|
|
|
|
offset = 0;
|
2018-02-14 23:42:57 +01:00
|
|
|
end = buf.length;
|
|
|
|
} else {
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(offset, 'offset', 0);
|
2018-02-14 23:42:57 +01:00
|
|
|
// Invalid ranges are not set to a default, so can range check early.
|
|
|
|
if (end === undefined) {
|
|
|
|
end = buf.length;
|
|
|
|
} else {
|
2019-04-02 00:01:29 +02:00
|
|
|
validateInt32(end, 'end', 0, buf.length);
|
2018-02-14 23:42:57 +01:00
|
|
|
}
|
2018-09-20 05:04:47 +02:00
|
|
|
if (offset >= end)
|
2018-02-14 23:42:57 +01:00
|
|
|
return buf;
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2020-01-24 08:44:15 +01:00
|
|
|
|
|
|
|
if (typeof value === 'number') {
|
|
|
|
// OOB check
|
|
|
|
const byteLen = TypedArrayProto_byteLength.call(buf);
|
|
|
|
const fillLength = end - offset;
|
|
|
|
if (offset > end || fillLength + offset > byteLen)
|
|
|
|
throw new ERR_BUFFER_OUT_OF_BOUNDS();
|
|
|
|
|
|
|
|
TypedArrayFill.call(buf, value, offset, end);
|
|
|
|
} else {
|
|
|
|
const res = bindingFill(buf, value, offset, end, encoding);
|
|
|
|
if (res < 0) {
|
|
|
|
if (res === -1)
|
|
|
|
throw new ERR_INVALID_ARG_VALUE('value', value);
|
|
|
|
throw new ERR_BUFFER_OUT_OF_BOUNDS();
|
|
|
|
}
|
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);
|
2019-04-02 00:01:29 +02:00
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
// Buffer#write(string, encoding)
|
2019-04-02 00:01:29 +02:00
|
|
|
if (length === undefined && typeof offset === 'string') {
|
2015-06-19 21:00:19 +02:00
|
|
|
encoding = offset;
|
|
|
|
length = this.length;
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
// Buffer#write(string, offset[, length][, encoding])
|
2019-04-02 00:01:29 +02:00
|
|
|
} else {
|
2019-04-28 09:06:23 +02:00
|
|
|
validateInt32(offset, 'offset', 0, this.length);
|
2017-04-10 11:07:58 +02:00
|
|
|
|
2018-12-29 14:17:40 +01:00
|
|
|
const remaining = this.length - offset;
|
2017-04-10 11:07:58 +02:00
|
|
|
|
2019-04-02 00:01:29 +02:00
|
|
|
if (length === undefined) {
|
|
|
|
length = remaining;
|
|
|
|
} else if (typeof length === 'string') {
|
|
|
|
encoding = length;
|
|
|
|
length = remaining;
|
|
|
|
} else {
|
|
|
|
validateInt32(length, 'length', 0, this.length);
|
|
|
|
if (length > remaining)
|
|
|
|
length = remaining;
|
|
|
|
}
|
2015-06-19 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 00:01:29 +02:00
|
|
|
if (!encoding)
|
|
|
|
return this.utf8Write(string, offset, length);
|
2015-06-19 21:00:19 +02:00
|
|
|
|
2019-08-20 05:25:28 +02:00
|
|
|
const ops = getEncodingOps(encoding);
|
|
|
|
if (ops === undefined)
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
return ops.write(this, string, offset, length);
|
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);
|
2019-11-06 12:26:54 +01:00
|
|
|
for (let i = 0; i < this.length; ++i)
|
2017-01-19 08:55:21 +01:00
|
|
|
data[i] = this[i];
|
|
|
|
return { type: 'Buffer', data };
|
|
|
|
}
|
2018-12-20 03:40:40 +01:00
|
|
|
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.
|
2019-11-03 12:45:32 +01:00
|
|
|
offset = MathTrunc(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;
|
|
|
|
}
|
2019-11-27 19:59:29 +01:00
|
|
|
return NumberIsNaN(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) {
|
2019-11-06 12:26:54 +01:00
|
|
|
for (let i = 0; i < len; i += 2)
|
2016-06-04 22:38:08 +02:00
|
|
|
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) {
|
2019-11-06 12:26:54 +01:00
|
|
|
for (let i = 0; i < len; i += 4) {
|
2016-06-04 22:38:08 +02:00
|
|
|
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) {
|
2019-11-06 12:26:54 +01:00
|
|
|
for (let i = 0; i < len; i += 8) {
|
2016-06-04 22:38:08 +02:00
|
|
|
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;
|
2018-10-09 01:10:07 +02:00
|
|
|
if (internalBinding('config').hasIntl) {
|
2017-06-20 01:06:02 +02:00
|
|
|
const {
|
|
|
|
icuErrName,
|
|
|
|
transcode: _transcode
|
2018-10-03 04:30:07 +02:00
|
|
|
} = internalBinding('icu');
|
2017-06-20 01:06:02 +02:00
|
|
|
|
|
|
|
// 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);
|
2018-03-15 14:22:43 +01:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2017-06-20 01:06:02 +02:00
|
|
|
const err = new Error(`Unable to transcode Buffer [${code}]`);
|
|
|
|
err.code = code;
|
|
|
|
err.errno = result;
|
|
|
|
throw err;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-04 05:36:41 +02:00
|
|
|
module.exports = {
|
2017-06-20 01:06:02 +02:00
|
|
|
Buffer,
|
|
|
|
SlowBuffer,
|
|
|
|
transcode,
|
|
|
|
// Legacy
|
|
|
|
kMaxLength,
|
|
|
|
kStringMaxLength
|
|
|
|
};
|
|
|
|
|
2019-11-03 12:45:32 +01:00
|
|
|
ObjectDefineProperties(module.exports, {
|
2019-04-04 05:36:41 +02:00
|
|
|
constants: {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
value: constants
|
|
|
|
},
|
|
|
|
INSPECT_MAX_BYTES: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() { return INSPECT_MAX_BYTES; },
|
|
|
|
set(val) { INSPECT_MAX_BYTES = val; }
|
|
|
|
}
|
2017-06-20 01:06:02 +02:00
|
|
|
});
|