How to use the safer-buffer.Buffer.alloc function in safer-buffer

To help you get started, we’ve selected a few safer-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 bnoordhuis / node-iconv / test / test-big-buffer.js View on Github external
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

'use strict';

var Iconv = require('../lib/iconv').Iconv;
var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;

var iconv = new Iconv('UTF-8', 'UTF-16LE');

var utf8 = Buffer.alloc(20000000);
for (var i = 0; i < utf8.length; i++) {
  utf8[i] = 97 + i % 26; // cycle from 'a' to 'z'.
}
var utf16 = iconv.convert(utf8);

assert.equal(utf16.length, utf8.length * 2);

for (i = 0; i < utf8.length; ++i) {
  assert.equal(utf16[i * 2], utf8[i]);
  assert.equal(utf16[i * 2 + 1], 0);
}
github sx1989827 / DOClever / node_modules / sshpk / lib / formats / x509.js View on Github external
function writeBitField(setBits, bitIndex) {
	var bitLen = bitIndex.length;
	var blen = Math.ceil(bitLen / 8);
	var unused = blen * 8 - bitLen;
	var bits = Buffer.alloc(1 + blen); // zero-filled
	bits[0] = unused;
	for (var i = 0; i < bitLen; ++i) {
		var byteN = 1 + Math.floor(i / 8);
		var bit = 7 - (i % 8);
		var mask = 1 << bit;
		var name = bitIndex[i];
		if (name === undefined)
			continue;
		var bitVal = (setBits.indexOf(name) !== -1);
		if (bitVal) {
			bits[byteN] |= mask;
		}
	}
	return (bits);
}
github farhadi / node-smpp / test / types.js View on Github external
describe('cstring', function() {
	var b = Buffer.alloc(9), expected = 'abcd1234';
	b[8] = 0;
	b.write(expected, 0);

	describe('#read()', function() {
		it('should read a C-Octet String (null-terminated string) from the buffer', function() {
			var result = types.cstring.read(b, 0);
			assert.equal(result, expected);
		});
	});

	describe('#size()', function() {
		it('should return the length of a C-Octet String (null-terminated string)', function() {
			assert.equal(9, types.cstring.size(expected));
		});
	});
github joyent / node-sshpk / lib / utils.js View on Github external
function zeroPadToLength(buf, len) {
	assert.buffer(buf);
	assert.number(len);
	while (buf.length > len) {
		assert.equal(buf[0], 0x00);
		buf = buf.slice(1);
	}
	while (buf.length < len) {
		var b = Buffer.alloc(buf.length + 1);
		b[0] = 0x00;
		buf.copy(b, 1);
		buf = b;
	}
	return (buf);
}
github sx1989827 / DOClever / node_modules / sshpk / lib / utils.js View on Github external
function mpNormalize(buf) {
	assert.buffer(buf);
	while (buf.length > 1 && buf[0] === 0x00 && (buf[1] & 0x80) === 0x00)
		buf = buf.slice(1);
	if ((buf[0] & 0x80) === 0x80) {
		var b = Buffer.alloc(buf.length + 1);
		b[0] = 0x00;
		buf.copy(b, 1);
		buf = b;
	}
	return (buf);
}
github bnoordhuis / node-iconv / lib / iconv.js View on Github external
input_size,
                                 output,
                                 output_start,
                                 output_size,
                                 out,
                                 input === FLUSH);
    var input_consumed = out[0];
    var output_consumed = out[1];
    input_start += input_consumed;
    input_size -= input_consumed;
    output_start += output_consumed;
    output_size -= output_consumed;
    if (errno) {
      if (errno === E2BIG) {
        output_size += output.length;
        var newbuf = Buffer.alloc(output.length * 2);
        output.copy(newbuf, 0, 0, output_start);
        output = newbuf;
        continue;
      }
      else if (errno === EILSEQ) {
        throw errnoException('EILSEQ', 'Illegal character sequence.');
      }
      else if (errno === EINVAL) {
        if (context === null || input === FLUSH) {
          throw errnoException('EINVAL', 'Incomplete character sequence.');
        }
        else {
          context.trailer = input.slice(input_start);
          return output.slice(0, output_start);
        }
      }
github ashtuchkin / iconv-lite / encodings / utf16.js View on Github external
Utf16BEDecoder.prototype.write = function(buf) {
    if (buf.length == 0)
        return '';

    var buf2 = Buffer.alloc(buf.length + 1),
        i = 0, j = 0;

    if (this.overflowByte !== -1) {
        buf2[0] = buf[0];
        buf2[1] = this.overflowByte;
        i = 1; j = 2;
    }

    for (; i < buf.length-1; i += 2, j+= 2) {
        buf2[j] = buf[i+1];
        buf2[j+1] = buf[i];
    }

    this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;

    return buf2.slice(0, j).toString('ucs2');
github ashiq-r31 / PWA-Sushi-Hunt / node_modules / sshpk / lib / utils.js View on Github external
function mpNormalize(buf) {
	assert.buffer(buf);
	while (buf.length > 1 && buf[0] === 0x00 && (buf[1] & 0x80) === 0x00)
		buf = buf.slice(1);
	if ((buf[0] & 0x80) === 0x80) {
		var b = Buffer.alloc(buf.length + 1);
		b[0] = 0x00;
		buf.copy(b, 1);
		buf = b;
	}
	return (buf);
}
github joyent / node-sshpk / lib / ssh-buffer.js View on Github external
SSHBuffer.prototype.expand = function () {
	this._size *= 2;
	var buf = Buffer.alloc(this._size);
	this._buffer.copy(buf, 0);
	this._buffer = buf;
};

safer-buffer

Modern Buffer API polyfill without footguns

MIT
Latest version published 7 years ago

Package Health Score

65 / 100
Full package analysis

Similar packages