Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const len = Buffer.byteLength(obj)
var buf
if (len < 32) {
buf = Buffer.allocUnsafe(1 + len)
buf[0] = 0xa0 | len
if (len > 0) {
buf.write(obj, 1)
}
} else if (len <= 0xff && !options.compatibilityMode) {
// str8, but only when not in compatibility mode
buf = Buffer.allocUnsafe(2 + len)
buf[0] = 0xd9
buf[1] = len
buf.write(obj, 2)
} else if (len <= 0xffff) {
buf = Buffer.allocUnsafe(3 + len)
buf[0] = 0xda
buf.writeUInt16BE(len, 1)
buf.write(obj, 3)
} else {
buf = Buffer.allocUnsafe(5 + len)
buf[0] = 0xdb
buf.writeUInt32BE(len, 1)
buf.write(obj, 5)
}
return buf
}
test('encode/decode 2^8-1 bytes buffers', function (t) {
var encoder = msgpack()
var all = []
all.push(build(Math.pow(2, 8) - 1))
all.push(build(Math.pow(2, 6) + 1))
all.push(build(1))
all.push(Buffer.allocUnsafe(0))
all.forEach(function (orig) {
t.test('encoding a buffer of length ' + orig.length, function (t) {
var buf = encoder.encode(orig)
t.equal(buf.length, 2 + orig.length, 'must have the right length')
t.equal(buf.readUInt8(0), 0xc4, 'must have the proper header')
t.equal(buf.readUInt8(1), orig.length, 'must include the buf length')
t.equal(buf.toString('utf8', 2), orig.toString('utf8'), 'must decode correctly')
t.end()
})
t.test('decoding a buffer of length ' + orig.length, function (t) {
var buf = Buffer.allocUnsafe(2 + orig.length)
buf[0] = 0xc4
buf[1] = orig.length
orig.copy(buf, 2)
function build (size) {
var buf
buf = Buffer.allocUnsafe(size)
buf.fill('a')
return buf
}
HandshakeInitializationPacket.prototype.scrambleBuff = function() {
var buffer = null;
if (typeof this.scrambleBuff2 === 'undefined') {
buffer = Buffer.from(this.scrambleBuff1);
} else {
buffer = Buffer.allocUnsafe(this.scrambleBuff1.length + this.scrambleBuff2.length);
this.scrambleBuff1.copy(buffer, 0);
this.scrambleBuff2.copy(buffer, this.scrambleBuff1.length);
}
return buffer;
};
exports.encrypt = function (self, data, decrypt) {
var out = Buffer.allocUnsafe(0)
var len
while (data.length) {
if (self._cache.length === 0) {
self._cache = self._cipher.encryptBlock(self._prev)
self._prev = Buffer.allocUnsafe(0)
}
if (self._cache.length <= data.length) {
len = self._cache.length
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)])
data = data.slice(len)
} else {
out = Buffer.concat([out, encryptStart(self, data, decrypt)])
break
}
function copyFromBuffer(n, list) {
var ret = Buffer.allocUnsafe(n);
var p = list.head;
var c = 1;
p.data.copy(ret);
n -= p.data.length;
while (p = p.next) {
var buf = p.data;
var nb = n > buf.length ? buf.length : n;
buf.copy(ret, ret.length - n, 0, nb);
n -= nb;
if (n === 0) {
if (nb === buf.length) {
++c;
if (p.next) list.head = p.next;else list.head = list.tail = null;
} else {
list.head = p;
p.data = buf.slice(nb);
function copyFromBuffer(n, list) {
var ret = Buffer.allocUnsafe(n);
var p = list.head;
var c = 1;
p.data.copy(ret);
n -= p.data.length;
while (p = p.next) {
var buf = p.data;
var nb = n > buf.length ? buf.length : n;
buf.copy(ret, ret.length - n, 0, nb);
n -= nb;
if (n === 0) {
if (nb === buf.length) {
++c;
if (p.next) list.head = p.next;else list.head = list.tail = null;
} else {
list.head = p;
p.data = buf.slice(nb);
function nonZero (len) {
var out = Buffer.allocUnsafe(len)
var i = 0
var cache = randomBytes(len * 2)
var cur = 0
var num
while (i < len) {
if (cur === cache.length) {
cache = randomBytes(len * 2)
cur = 0
}
num = cache[cur++]
if (num) {
out[i++] = num
}
}
return out
}
writeVarSlice(out.script)
})
hashOutputs = bcrypto.hash256(tbuffer)
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
const output = this.outs[inIndex]
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
toffset = 0
writeUInt64(output.value)
writeVarSlice(output.script)
hashOutputs = bcrypto.hash256(tbuffer)
}
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
toffset = 0
const input = this.ins[inIndex]
writeUInt32(this.version)
writeSlice(hashPrevouts)
writeSlice(hashSequence)
writeSlice(input.hash)
writeUInt32(input.index)
writeVarSlice(prevOutScript)
writeUInt64(value)
writeUInt32(input.sequence)
writeSlice(hashOutputs)
writeUInt32(this.locktime)
writeUInt32(hashType)
return bcrypto.hash256(tbuffer)
}
function encodeFloat (obj, forceFloat64) {
var buf
if (forceFloat64 || !fround || fround(obj) !== obj) {
buf = Buffer.allocUnsafe(9)
buf[0] = 0xcb
buf.writeDoubleBE(obj, 1)
} else {
buf = Buffer.allocUnsafe(5)
buf[0] = 0xca
buf.writeFloatBE(obj, 1)
}
return buf
}