Example #1
0
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  buf.fill(0xFF);
  written = buf.write('abcd', 0, 2, encoding);
  console.log(buf);
  assert.equal(written, 2);
  assert.equal(buf[0], 0x61);
  assert.equal(buf[1], 0x00);
  assert.equal(buf[2], 0xFF);
  assert.equal(buf[3], 0xFF);
});
Example #2
0
GitObject.prototype.getAsBuffer = function() {
    var type = this.getType();
    var content = this.getContent();

    var nullBuf = new Buffer(1);
    nullBuf.fill(0);

    return Buffer.concat([
        (new Buffer(type + ' ' + content.length, 'utf8')),
        nullBuf,
        content
    ]);
};
Example #3
0
function write_boot2() {
	if (typeof boot2_enc_algo === "undefined") {
		/* Add 16 zeros for MIC */
		var zero_buf = new Buffer(16);
		zero_buf.fill(0);
		boot2_buf.write(zero_buf.toString('hex'), es_size + boot2_len, 16, 'hex');
	}
	/* Add 16 to length for MIC */
	boot2_len += 16;
	boot2_buf = boot2_buf.slice(0, es_size + boot2_len);
	var filename = get_outfile_name();
	fs.writeFileSync(filename, boot2_buf, 'hex');

	console.log("\r\nGenerated " + filename);
}
 it('should error out on blending an invalid JPEG image', function(done) {
     var buffer = new Buffer(32);
     buffer.fill(0);
     buffer[0] = 0xFF;
     buffer[1] = 0xD8;
     blend([
         buffer,
         fs.readFileSync('test/fixture/2.png')
     ], function(err, data) {
         assert.ok(err);
         assert.ok(err.message === "Premature end of JPEG file" ||
                   err.message === "JPEG datastream contains no image");
         done();
     });
 });
Example #5
0
    packFileId: function(command, group, filename, charset) {
        // --------- 封装header
        var fnLength = Buffer.byteLength(filename, charset);
        var bodyLength = protocol.FDFS_GROUP_NAME_MAX_LEN + fnLength;
        var header = protocol.packHeader(command, bodyLength, 0);

        // --------- 封装body
        var body = new Buffer(bodyLength);
        // 默认都填充上0
        body.fill(0);
        var groupBL = Buffer.byteLength(group, charset);
        body.write(group, 0, groupBL, charset);
        body.write(filename, protocol.FDFS_GROUP_NAME_MAX_LEN, fnLength, charset);

        return Buffer.concat([header, body]);
    },
  writePackets (message, packetSize = 64) {
    const toWrite = iconv.encode(message, 'cp852')
    const writePromises = []
    const packetCount = Math.ceil(toWrite.length / packetSize)

    for (var i = 0; i < packetCount; i++) {
      const packet = new Buffer(packetSize)
      packet.fill(' ')
      toWrite.copy(packet, 0, i * packetSize, (i + 1) * packetSize)
      writePromises.push(BluetoothSerial.write(packet))
    }

    Promise.all(writePromises)
    .then((result) => {
    })
  }
Example #7
0
/*
 * Note: this is *not* necessarily the recommended way to
 * generate key material and initialization-vectors, but
 * it follows the OpenSSL method, as used by Node.js proper,
 * which is slightly weird and non-standard for longer key-lengths,
 * but does end up being compatible with produced cipher-texts from
 * Node.js.
 */
function kdf(data, keyLen, ivLen) {
  var totalLen = keyLen + ivLen;
  var curLen = 0;
  var prev = new Buffer('');
  var iter = 1;

  var kiv = new Buffer(totalLen);
  kiv.fill(0);

  while ( curLen < totalLen ) {
    prev = kdf_d(data, prev, 1 );
    prev.copy( kiv, curLen );
    ++iter;
    curLen += 16;
  }

  var k = kiv.slice( 0, keyLen );
  var i = kiv.slice( keyLen );

  return {
    key: kiv.slice( 0, keyLen ),
    iv:  kiv.slice( keyLen )
  };
}
Example #8
0
console.log(z.inspect());
console.log(z.length);
assert.equal(3, z.length);
assert.equal(0xa3, z[0]);

var z = x.slice(2, 4);
console.log(z.inspect());
console.log(z.length);
assert.equal(2, z.length);
assert.equal(0x66, z[0]);
assert.equal(0x6f, z[1]);

assert.equal(0, Buffer('hello').slice(0, 0).length);

b = new Buffer(50);
b.fill('h');
for (var i = 0; i < b.length; i++) {
  assert.equal('h'.charCodeAt(0), b[i]);
}

b.fill(0);
for (var i = 0; i < b.length; i++) {
  assert.equal(0, b[i]);
}

b.fill(1, 16, 32);
for (var i = 0; i < 16; i++) assert.equal(0, b[i]);
for (; i < 32; i++) assert.equal(1, b[i]);
for (; i < b.length; i++) assert.equal(0, b[i]);

var b = new SlowBuffer(10);
Example #9
0
    runs(function () {
      var data = new Buffer(200)
      data.fill(0)

      lz4.decode(lz4.encode(data))
    })
Example #10
0
// First check Buffer#fill() works as expected.

assert.throws(function() {
  Buffer(8).fill('a', -1);
});

assert.throws(function() {
  Buffer(8).fill('a', 0, 9);
});

// Make sure this doesn't hang indefinitely.
Buffer(8).fill('');

{
  const buf = new Buffer(64);
  buf.fill(10);
  for (let i = 0; i < buf.length; i++)
    assert.equal(buf[i], 10);

  buf.fill(11, 0, buf.length >> 1);
  for (let i = 0; i < buf.length >> 1; i++)
    assert.equal(buf[i], 11);
  for (let i = (buf.length >> 1) + 1; i < buf.length; i++)
    assert.equal(buf[i], 10);

  buf.fill('h');
  for (let i = 0; i < buf.length; i++)
    assert.equal('h'.charCodeAt(0), buf[i]);

  buf.fill(0);
  for (let i = 0; i < buf.length; i++)
Example #11
0
var tint = module.exports = function(png, options) {
    if (!png || !png.length || !png.readUInt32BE) throw new Error('Image must be a buffer');
    if (png.length < 67) throw new Error('Image size is too small');

    // Check header.
    if (png[0] !== 137 || png[1] !== 80 || png[2] !== 78 || png[3] !== 71 ||
        png[4] !== 13  || png[5] !== 10 || png[6] !== 26 || png[7] !== 10) throw new Error('Image is not a PNG file');

    if (!options) options = {};
    var hue = (options.hue || 0);
    var saturation = (options.saturation || 0) / 100;
    var opacity = 'opacity' in options ? +options.opacity : 1;
    var y0 = 'y0' in options ? +options.y0 : 0;
    var y1 = 'y1' in options ? +options.y1 : 1;

    if (hue >= 360 || hue < 0) throw new Error('Hue must be between 0 and 360 degrees');
    if (saturation > 1 || saturation < 0) throw new Error('Saturation must be between 0% and 100%');

    var lut = getLookupTable(hue, saturation, y0, y1);

    // Find PLTE chunk
    var i = 8;
    var palette = 0;
    while (i < png.length) {
        var length = png.readUInt32BE(i);
        var type = png.toString('ascii', i + 4, i + 8);
        if (!(length || type === 'IEND')) throw new Error('Image has invalid chunk with length 0');

        if (type === 'PLTE') {
            i += 8; // Length + type.

            for (var entry = 0; entry < length; entry += 3) {
                var r = png[i + entry], g = png[i + entry + 1], b = png[i + entry + 2];
                var lightness = Math.round(0.30*r + 0.59*g + 0.11*b);
                var color = lut[lightness];
                png[i + entry] = color[0];
                png[i + entry + 1] = color[1];
                png[i + entry + 2] = color[2];
                palette++;
            }

            // Update CRC
            var crc = crc32(png.slice(i - 4, i + length));
            // Don't use buffer copy because it fails in node 0.4 due to
            // different instances of the Buffer object...
            png[i + length] = crc[0];
            png[i + length + 1] = crc[1];
            png[i + length + 2] = crc[2];
            png[i + length + 3] = crc[3];

            // No opacity adjustment -- we're done.
            if (opacity === 1) return png;
        } else if (type === 'tRNS' || (palette && type === 'IDAT')) {
            // Normalize the tRNS chunk following the palette for opacity
            // adjustment. If there are more entries in the palette than the
            // tRNS chunk length these have an implicit A:255 values.
            // Normalize to ensure a tRNS entry for every palette entry.
            //
            // - Adds a tRNS chunk for images that do not have one.
            // - Ensures the tRNS chunk is of length `palette`.
            if (type !== 'tRNS') {
                var adjusted = new Buffer(png.length + 8 + palette + 4);
                png.copy(adjusted, 0, 0, i);
                // Update tRNS length to match palette.
                adjusted.writeUInt32BE(palette, i);
                // Add tRNS chunk header
                adjusted[i + 4] = 116; // t
                adjusted[i + 5] = 82;  // R
                adjusted[i + 6] = 78;  // N
                adjusted[i + 7] = 83;  // S
                // Fill palette indices with A:255.
                adjusted.fill(255, i + 8, i + 8 + palette);
                png.copy(adjusted, i + 8 + palette + 4, i);
                png = adjusted;
            } else if (palette > length) {
                var adjusted = new Buffer(png.length + (palette - length));
                png.copy(adjusted, 0, 0, i + 8 + length + 4);
                // Update tRNS length to match palette.
                adjusted.writeUInt32BE(palette, i);
                // Fill palette indices omitted from original tRNS with A:255.
                adjusted.fill(255, i + 8 + length, i + 8 + palette);
                png.copy(adjusted, i + 8 + palette + 4, i + 8 + length + 4);
                png = adjusted;
            }

            i += 8; // Length + type.

            // Apply opacity adjustment.
            for (var entry = 0; entry < palette; entry += 1) {
                var val = png[i + entry] * opacity | 0;
                png[i + entry] = val > 255 ? 255 : val;
            }

            // Update CRC
            var crc = crc32(png.slice(i - 4, i + palette));
            // Don't use buffer copy because it fails in node 0.4 due to
            // different instances of the Buffer object...
            png[i + palette] = crc[0];
            png[i + palette + 1] = crc[1];
            png[i + palette + 2] = crc[2];
            png[i + palette + 3] = crc[3];
            return png;
        } else {
            i += 8; // Length + type.
        }
        // Skip CRC.
        i += length + 4;
    }

    throw new Error('Image does not have a palette')
};
Example #12
0
test("Buffer.prototype.fill()", function () {
	var b = new Buffer(5);
	b.fill("a");
	assertEqual(b.toString(), "aaaaa");
});
Example #13
0
var z = x.slice(1, 4);
console.log(z.inspect())
console.log(z.length)
assert.equal(3, z.length);
assert.equal(0xa3, z[0]);

var z = x.slice(2, 4);
console.log(z.inspect())
console.log(z.length)
assert.equal(2, z.length);
assert.equal(0x66, z[0]);
assert.equal(0x6f, z[1]);
assert.equal(0, Buffer('hello').slice(0, 0).length)

b = new Buffer(50);
b.fill("h");
for (var i = 0; i < b.length; i++) {
  assert.equal("h".charCodeAt(0), b[i]);
}

b.fill(0);
for (var i = 0; i < b.length; i++) {
  assert.equal(0, b[i]);
}

b.fill(1, 16, 32);
for (var i = 0; i < 16; i++) assert.equal(0, b[i]);
for (; i < 32; i++) assert.equal(1, b[i]);
for (; i < b.length; i++) assert.equal(0, b[i]);
 pt._transform = function(c, e, cb) {
   var ret = new Buffer(c.length);
   ret.fill('x');
   pt.push(ret);
   cb();
 };
Example #15
0
const ab = new ArrayBuffer(LENGTH);
const dv = new DataView(ab);
const ui = new Uint8Array(ab);
const buf = new Buffer(ab);


assert.ok(buf instanceof Buffer);
// For backwards compatibility of old .parent property test that if buf is not
// a slice then .parent should be undefined.
assert.equal(buf.parent, undefined);
assert.equal(buf.buffer, ab);
assert.equal(buf.length, ab.byteLength);


buf.fill(0xC);
for (let i = 0; i < LENGTH; i++) {
  assert.equal(ui[i], 0xC);
  ui[i] = 0xF;
  assert.equal(buf[i], 0xF);
}

buf.writeUInt32LE(0xF00, 0);
buf.writeUInt32BE(0xB47, 4);
buf.writeDoubleLE(3.1415, 8);

assert.equal(dv.getUint32(0, true), 0xF00);
assert.equal(dv.getUint32(4), 0xB47);
assert.equal(dv.getFloat64(8, true), 3.1415);

for (var i = 0; i < 1024; i++) {
  b[i] = i % 256;
}

for (var i = 0; i < 1024; i++) {
  assert.strictEqual(i % 256, b[i]);
}

var c = new Buffer(512);
console.log('c.length == %d', c.length);
assert.strictEqual(512, c.length);

// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 512);
console.log('copied %d bytes from b into c', copied);
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
  assert.strictEqual(b[i], c[i]);
}

// copy c into b, without specifying sourceEnd
b.fill(++cntr);
c.fill(++cntr);
var copied = c.copy(b, 0, 0);
console.log('copied %d bytes from c into b w/o sourceEnd', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
  assert.strictEqual(c[i], b[i]);
Example #17
0
for (var i = 0; i < 1024; i++) {
  b[i] = i % 256;
}

for (var i = 0; i < 1024; i++) {
  assert.strictEqual(i % 256, b[i]);
}

var c = new Buffer(512);
console.log('c.length == %d', c.length);
assert.strictEqual(512, c.length);

// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 512);
console.log('copied %d bytes from b into c', copied);
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
  assert.strictEqual(b[i], c[i]);
}

// copy c into b, without specifying sourceEnd
b.fill(++cntr);
c.fill(++cntr);
var copied = c.copy(b, 0, 0);
console.log('copied %d bytes from c into b w/o sourceEnd', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
  assert.strictEqual(c[i], b[i]);
Example #18
0
// First check Buffer#fill() works as expected.

assert.throws(function() {
  Buffer(8).fill('a', -1);
});

assert.throws(function() {
  Buffer(8).fill('a', 0, 9);
});

// Make sure this doesn't hang indefinitely.
Buffer(8).fill('');

{
  const buf = new Buffer(64);
  buf.fill(10);
  for (let i = 0; i < buf.length; i++)
    assert.equal(buf[i], 10);

  buf.fill(11, 0, buf.length >> 1);
  for (let i = 0; i < buf.length >> 1; i++)
    assert.equal(buf[i], 11);
  for (let i = (buf.length >> 1) + 1; i < buf.length; i++)
    assert.equal(buf[i], 10);

  buf.fill('h');
  for (let i = 0; i < buf.length; i++)
    assert.equal('h'.charCodeAt(0), buf[i]);

  buf.fill(0);
  for (let i = 0; i < buf.length; i++)
test('steam2 - readable empty buffer to eof - 1', function (t) {
  var r = new Readable();

  // should not end when we get a Buffer(0) or '' as the _read result
  // that just means that there is *temporarily* no data, but to go
  // ahead and try again later.
  //
  // note that this is very unusual.  it only works for crypto streams
  // because the other side of the stream will call read(0) to cycle
  // data through openssl.  that's why we set the timeouts to call
  // r.read(0) again later, otherwise there is no more work being done
  // and the process just exits.

  var buf = new Buffer(5);
  buf.fill('x');
  var reads = 5;
  r._read = function(n) {
    switch (reads--) {
      case 0:
        return r.push(null); // EOF
      case 1:
        return r.push(buf);
      case 2:
        setTimeout(shims.bind(r.read, r, 0), 10);
        return r.push(new Buffer(0)); // Not-EOF!
      case 3:
        setTimeout(shims.bind(r.read, r, 0), 10);
        return timers.setImmediate(function() {
          return r.push(new Buffer(0));
        });
      case 4:
        setTimeout(shims.bind(r.read, r, 0), 10);
        return setTimeout(function() {
          return r.push(new Buffer(0));
        });
      case 5:
        return setTimeout(function() {
          return r.push(buf);
        });
      default:
        throw new Error('unreachable');
    }
  };

  var results = [];
  function flow() {
    var chunk;
    while (null !== (chunk = r.read()))
      results.push(chunk + '');
  }
  r.on('readable', flow);
  r.on('end', function() {
    results.push('EOF');
  });
  flow();

  function done() {
    t.deepEqual(results, [ 'xxxxx', 'xxxxx', 'EOF' ]);
    t.end();
  }
  r.on('end', done);
});
Example #20
0
var fillCapeEepromData = exports.fillCapeEepromData = function(data) {
    eepromData.fill();
    eepromData.hexWrite('aa5533ee', 0, 4);
    eepromData.write('A0', 4, 2, 'ascii');
    if(data.boardName.length > 32) {
        data.boardName.length = 32;
    }
    eepromData.write(data.boardName, 6, 32, 'ascii');
    if(data.version.length > 4) {
        data.version.length = 4;
    }
    eepromData.write(data.version, 38, 4, 'ascii');
    if(data.manufacturer.length > 16) {
        data.manufacturer.length = 16;
    }
    eepromData.write(data.manufacturer, 42, 16, 'ascii');
    if(data.partNumber.length > 16) {
        data.partNumber.length = 16;
    }
    eepromData.write(data.partNumber, 58, 16, 'ascii');
    eepromData.writeUint16BE(data.numPins, 74, 'ascii');
    if(data.serialNumber.length > 12) {
        data.serialNumber.length = 12;
    }
    eepromData.write(data.serialNumber, 76, 12, 'ascii');
    eepromData.writeUint16BE(data.currentVDD_3V3EXP, 236);
    eepromData.writeUint16BE(data.currentVDD_5V, 238);
    eepromData.writeUint16BE(data.currentSYS_5V, 240);
    eepromData.writeUint16BE(data.DCSupplied, 242);
    for(var pin in data.mux) {
        if(typeof pinmap[pin].eeprom != 'undefined') {
            var pinOffset = pinmap[pin].eeprom * 2 + 88;
            var pinObject = data.mux[pin];
            var pinData = 0;
            if(pinObject.used == 'used') pinData |= 0x8000;
            switch(pinObject.direction) {
            case 'in':
                pinData |= 0x2000;
                break;
            case 'out':
                pinData |= 0x4000;
                break;
            case 'bidir':
                pinData |= 0x6000;
                break;
            default:
                winston.error('Unknown direction value: '+pinObject.direction);
                pinData |= 0x2000;
            }
            if(pinObject.slew == 'slow') pinData |= 0x40;
            if(pinObject.rx == 'enabled') pinData |= 0x20;
            var pullup = (pinData & 0x18) >> 3;
            switch(pinObject.pullup) {
            case 'disabled':
                pinData |= 0x08;
                break;
            case 'pullup':
                pinData |= 0x10;
                break;
            case 'pulldown':
                break;
            default:
                winston.error('Unknown pullup value: '+pullup);
            }
            pinData |= (pinObject.mode & 0x0007);
            eepromData.writeUint16BE(pinData, pinOffset);
        }
    }
    return(eepromData);
};
Example #21
0
 beforeEach(function () {
   source = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
   target = new Buffer(15);
   target.fill(11);
 });
Example #22
0
var fw_enc_key = myArgs.fw_enc_key;
var fw_sign_algo = myArgs.fw_sign_algo;
var fw_hash_algo = myArgs.fw_hash_algo;
var fw_pub_key = myArgs.fw_pub_key;
var fw_prv_key = myArgs.fw_prv_key;
var fw_nonce = myArgs.fw_nonce;

/* tlv header elements */
var tlv_hdr_magic = new Uint32Array(1);
var tlv_hdr_len = new Uint32Array(1);
var tlv_hdr_crc = new Uint32Array(1);

/* tlv buffer parameters */
var tlv_buf_offset = 0;
var tlv_buf = new Buffer(100000);
tlv_buf.fill(0);

var tlv_fw_len_type = 40;
var tlv_fw_nonce_type = 39;
var tlv_fw_sig_type = 65;
var tlv_key_filler_type = 255;

var is_xip = false;

var app_fw_buf = new Buffer(fs.readFileSync(ip_file), 'hex');

/* Validate application firmware */
var magic_str = app_fw_buf.slice(0, 4);
if (magic_str != 'LVRM' && magic_str != 'MRVL') {
	console.error("Invalid application firmware. Exiting.");
	process.exit(1);