How to use the safe-buffer.Buffer.allocUnsafe function in safe-buffer

To help you get started, we’ve selected a few safe-buffer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mcollina / msgpack5 / lib / encoder.js View on Github external
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
}
github mcollina / msgpack5 / test / 1-byte-length-buffers.js View on Github external
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)
github mcollina / msgpack5 / test / 2-bytes-length-buffers.js View on Github external
function build (size) {
  var buf

  buf = Buffer.allocUnsafe(size)
  buf.fill('a')

  return buf
}
github mysqljs / mysql / lib / protocol / packets / HandshakeInitializationPacket.js View on Github external
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;
};
github makuga01 / dnsFookup / FE / node_modules / browserify-aes / modes / cfb.js View on Github external
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
    }
github angus-c / just / node_modules / readable-stream / lib / _stream_readable.js View on Github external
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);
github tonyxu-io / React-Linkedin-Login / client / node_modules / readable-stream / lib / _stream_readable.js View on Github external
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);
github johandb / svg-drawing-tool / node_modules / public-encrypt / publicEncrypt.js View on Github external
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
}
github bitcoinjs / bitcoinjs-lib / src / transaction.js View on Github external
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)
  }
github mcollina / msgpack5 / lib / encoder.js View on Github external
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
}