Example #1
0
  this.deflate(dict, function (err, chunks, size) {
    if (err) return callback(err);

    var offset = type === 'SYN_STREAM' ? 18 : 14,
        total = (type === 'SYN_STREAM' ? 10 : 6) + size,
        frame = new Buffer(offset + size);;

    frame.writeUInt16BE(0x8002, 0, true); // Control + Version
    frame.writeUInt16BE(type === 'SYN_STREAM' ? 1 : 2, 2, true); // type
    frame.writeUInt32BE(total & 0x00ffffff, 4, true); // No flag support
    frame.writeUInt32BE(id & 0x7fffffff, 8, true); // Stream-ID

    if (type === 'SYN_STREAM') {
      frame[4] = 2;
      frame.writeUInt32BE(assoc & 0x7fffffff, 12, true); // Stream-ID
    }

    frame.writeUInt8(priority & 0x3, 16, true); // Priority

    for (var i = 0; i < chunks.length; i++) {
      chunks[i].copy(frame, offset);
      offset += chunks[i].length;
    }

    callback(null, frame);
  });
Example #2
0
Framer.prototype.pingFrame = function pingFrame(id) {
  var header = new Buffer(12);

  header.writeUInt32BE(0x80020006, 0, true); // Version and type
  header.writeUInt32BE(0x00000004, 4, true); // Length
  id.copy(header, 8, 0, 4); // ID

  return header;
};
Example #3
0
Framer.prototype.windowUpdateFrame = function windowUpdateFrame(id, delta) {
  var header = new Buffer(16);

  header.writeUInt32BE(0x80030009, 0, true); // Version and type
  header.writeUInt32BE(0x00000008, 4, true); // Length
  header.writeUInt32BE(id & 0x7fffffff, 8, true); // ID
  header.writeUInt32BE(delta & 0x7fffffff, 12, true); // delta

  return header;
};
Example #4
0
Framer.prototype.dataFrame = function dataFrame(id, fin, data) {
  if (!fin && !data.length) return [];

  var frame = new Buffer(8 + data.length);

  frame.writeUInt32BE(id & 0x7fffffff, 0, true);
  frame.writeUInt32BE(data.length & 0x00ffffff, 4, true);
  frame.writeUInt8(fin ? 0x01 : 0x0, 4, true);

  if (data.length) data.copy(frame, 8);

  return frame;
};
Example #5
0
proto._buffer = function() {
  if(this._backing_store) {
    return this._backing_store
  }

  var buffer = new Buffer(this.data.length + 12)

  buffer.writeUInt32BE(this.data.length, 0)
  buffer.writeUInt32BE(this._type, 4)

  this.data.copy(buffer, 8)

  return this._backing_store = buffer
}
ClientSpdyConnection.prototype.ping = function() {
    /* It is a priority */
    var id = new Buffer(4);
    id.writeUInt32BE((this.pingId += 2) & 0x7fffffff, 0, true); // -ID
    return this.write(this.framer.pingFrame(id));

}
Example #7
0
Framer.prototype.rstFrame = function rstFrame(id, code) {
  var header;

  if (!(header = Framer.rstCache[code])) {
    header = new Buffer(16);

    header.writeUInt32BE(0x80020003, 0, true); // Version and type
    header.writeUInt32BE(0x00000008, 4, true); // Length
    header.writeUInt32BE(id & 0x7fffffff, 8, true); // Stream ID
    header.writeUInt32BE(code, 12, true); // Status Code

    Framer.rstCache[code] = header;
  }

  return header;
};
Example #8
0
            function (err, sv)
            {
                if (err)
                {
                    return callback(err);
                }

                var to_hash = [];

                var signature = Buffer.isBuffer(sv.signature) ?
                        sv.signature : new Buffer(sv.signature, 'binary');
                ths.push(signature);
                to_hash.push(signature);

                var data = Buffer.isBuffer(sv.data) ?
                        sv.data : new Buffer(sv.data, 'binary');
                ths.push(data);
                to_hash.push(data);

                var buf = new Buffer(4);
                buf.writeUInt32BE(sv.version, 0, true);
                ths.push(buf);
                to_hash.push(buf);

                prev_hash = This._sha256(to_hash);

                callback();
            });
Example #9
0
Framer.prototype.settingsFrame = function settingsFrame(options) {
  var settings;

  if (!(settings = Framer.settingsCache[options.maxStreams])) {
    settings = new Buffer(20);

    settings.writeUInt32BE(0x80020004, 0, true); // Version and type
    settings.writeUInt32BE(0x0000000C, 4, true); // length
    settings.writeUInt32BE(0x00000001, 8, true); // Count of entries
    settings.writeUInt32LE(0x01000004, 12, true); // Entry ID and Persist flag
    settings.writeUInt32BE(options.maxStreams, 16, true);

    Framer.settingsCache[options.maxStreams] = settings;
  }

  return settings;
};
ClientSpdyConnection.prototype.windowSizeFrame = function windowSizeFrame(size) {
  var settings;

  if (!(settings = ClientSpdyConnection.windowSizeCache[size])) {
    settings = new Buffer(20);

    settings.writeUInt32BE(0x80030004, 0, true); // Version and type
    settings.writeUInt32BE((4 + 8) & 0x00FFFFFF, 4, true); // length
    settings.writeUInt32BE(0x00000001, 8, true); // Count of entries

    settings.writeUInt32BE(0x01000007, 12, true); // Entry ID and Persist flag
    settings.writeUInt32BE(size & 0x7fffffff, 16, true); // Window Size (KB)

    ClientSpdyConnection.windowSizeCache[size] = settings;
  }

  return settings;
};
Example #11
0
Framer.prototype.maxStreamsFrame = function maxStreamsFrame(count) {
  var settings;

  if (!(settings = Framer.settingsCache[count])) {
    settings = new Buffer(20);

    settings.writeUInt32BE(0x80030004, 0, true); // Version and type
    settings.writeUInt32BE((4 + 8) & 0x00FFFFFF, 4, true); // length
    settings.writeUInt32BE(0x00000001, 8, true); // Count of entries

    settings.writeUInt32BE(0x01000004, 12, true); // Entry ID and Persist flag
    settings.writeUInt32BE(count, 16, true); // 100 Streams

    Framer.settingsCache[count] = settings;
  }

  return settings;
};
ClientSpdyConnection.prototype.goaway = function goaway(status) {

    var header = new Buffer(16);

    header.writeUInt32BE(0x80030007, 0, true); // Version and type
    header.writeUInt32BE(0x00000008, 4, true); // Length

    var id = new Buffer(4);
    // Server last good StreamID
    id.writeUInt32BE((this.lastGoodID) & 0x7fffffff, 0, true);
    id.copy(header, 8, 0, 4);

    var statusBuffer = new Buffer(4);
    statusBuffer.writeUInt32BE((status || 0) & 0x7fffffff, 0, true);
    statusBuffer.copy(header, 12, 0, 4);

    this.goAway = this.streamId;
    this.write(header);
}
Example #13
0
Notification.prototype.toNetwork = function () {
    var data = null
    ,   token = this.device.toNetwork()
    ,   tokenLength = token.length
    ,   payloadString = this.payloadString()
    ,   payloadLength = Buffer.byteLength(payloadString, this.encoding);

    var token_and_payload_size = 2 + tokenLength + 2 + payloadLength;
    
    if (typeof(this.identifier) !== "undefined") { /* extended notification format */

        data = new Buffer(1 + 4 + 4 + token_and_payload_size);
        
        data.writeUInt8(1,                  0);                                     
        data.writeUInt32BE(this.identifier, 1);                                     
        data.writeUInt32BE(this.expiry,     5); // Apple's doc says it could be negative but the C example uses uint32_t ??!
                                            // Comments ?...
                                            //
                                            // SOURCE: You can specify zero or a value less than zero to request that APNs not store the notification at all.
                                            //
                                            //     /* expiry date network order */
                                            //     memcpy(binaryMessagePt, &networkOrderExpiryEpochUTC, sizeof(uint32_t));

    } else { /* simple notification format */

        data = new Buffer(1 + token_and_payload_size);

        data.writeUInt8(0, 0);
        
    }

    var token_start_index = data.length - token_and_payload_size;

    data.writeUInt16BE(tokenLength,     token_start_index);
    token.copy(data,                    token_start_index + 2);
    data.writeUInt16BE(payloadLength,   token_start_index + 2 + tokenLength);
    data.write(payloadString,           token_start_index + 2 + tokenLength + 2, payloadLength, this.encoding);

    return data;
};
Example #14
0
/* Initialize TLV header */
function tlv_init() {
	tlv_hdr_len = tlv_hdr_magic.BYTES_PER_ELEMENT +
		tlv_hdr_len.BYTES_PER_ELEMENT +
		tlv_hdr_crc.BYTES_PER_ELEMENT;
	tlv_hdr_magic = 0x53424648;
	/* CRC is initialized to max tlv size(4K) */
	tlv_hdr_crc = 0x00001000;

	/* Write initial values to tlv buffer */
	tlv_buf.writeUInt32BE(tlv_hdr_magic, tlv_buf_offset, 4, 'hex');
	tlv_buf_offset += 4;
	tlv_buf.writeUInt32LE(tlv_hdr_len, tlv_buf_offset, 4, 'hex');
	tlv_buf_offset += 4;
	tlv_buf.writeUInt32LE(tlv_hdr_crc, tlv_buf_offset, 4, 'hex');
	tlv_buf_offset += 4;
}
Example #15
0
Framer.prototype.windowSizeFrame = function windowSizeFrame(size) {
  var settings;

  if (!(settings = Framer.settingsCache[size])) {
    settings = new Buffer(28);

    settings.writeUInt32BE(0x80030004, 0, true); // Version and type
    settings.writeUInt32BE((4 + 8 + 8) & 0x00FFFFFF, 4, true); // length
    settings.writeUInt32BE(0x00000002, 8, true); // Count of entries

    settings.writeUInt32BE(0x01000004, 12, true); // Entry ID and Persist flag
    settings.writeUInt32BE(100, 16, true); // 100 Streams

    settings.writeUInt32BE(0x01000007, 20, true); // Entry ID and Persist flag
    settings.writeUInt32BE(size & 0x7fffffff, 24, true); // Window Size (KB)

    Framer.settingsCache[size] = settings;
  }

  return settings;
};
Example #16
0
Framer.prototype.settingsFrame = function settingsFrame(options) {
  var settings,
      key = options.maxStreams + ':' + options.windowSize;

  if (!(settings = Framer.settingsCache[key])) {
    settings = new Buffer(28);

    settings.writeUInt32BE(0x80030004, 0, true); // Version and type
    settings.writeUInt32BE((4 + 8 * 2) & 0x00FFFFFF, 4, true); // length
    settings.writeUInt32BE(0x00000002, 8, true); // Count of entries

    settings.writeUInt32BE(0x01000004, 12, true); // Entry ID and Persist flag
    settings.writeUInt32BE(options.maxStreams & 0x7fffffff, 16, true);

    settings.writeUInt32BE(0x01000007, 20, true); // Entry ID and Persist flag
    settings.writeUInt32BE(options.windowSize & 0x7fffffff, 24, true);

    Framer.settingsCache[key] = settings;
  }

  return settings;
};
Example #17
0
            function (err, ev)
            {
                if (err)
                {
                    return callback(err);
                }

                var to_hash = [];

                var iv = Buffer.isBuffer(ev.iv) ?
                        ev.iv : new Buffer(ev.iv, 'binary');
                ths.push(iv);
                to_hash.push(iv);

                var data = Buffer.isBuffer(ev.data) ?
                        ev.data : new Buffer(ev.data, 'binary');
                ths.push(data);
                to_hash.push(data);

                var buf = new Buffer(4);
                buf.writeUInt32BE(ev.version, 0, true);
                ths.push(buf);
                to_hash.push(buf);

                buf = Buffer.concat(
                    ev.ekey ? [new Buffer([1]),
                               Buffer.isBuffer(ev.ekey) ?
                                    ev.ekey : new Buffer(ev.ekey, 'binary')] :
                              [new Buffer([0])]);
                ths.push(buf);
                to_hash.push(buf);

                prev_hash = This._sha256(to_hash);

                callback();
            });
Example #18
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 #19
0
//
// internal, converts object into spdy dictionary
//
function headersToDict(headers, preprocess) {
  function stringify(value) {
    if (value !== undefined) {
      if (Array.isArray(value)) {
        return value.join('\x00');
      } else if (typeof value === 'string') {
        return value;
      } else {
        return value.toString();
      }
    } else {
      return '';
    }
  }

  // Lower case of all headers keys
  var loweredHeaders = {};
  Object.keys(headers || {}).map(function(key) {
    loweredHeaders[key.toLowerCase()] = headers[key];
  });

  // Allow outer code to add custom headers or remove something
  if (preprocess) preprocess(loweredHeaders);

  // Transform object into kv pairs
  var len = 4,
      pairs = Object.keys(loweredHeaders).filter(function(key) {
        var lkey = key.toLowerCase();
        return lkey !== 'connection' && lkey !== 'keep-alive' &&
               lkey !== 'proxy-connection' && lkey !== 'transfer-encoding';
      }).map(function(key) {
        var klen = Buffer.byteLength(key),
            value = stringify(loweredHeaders[key]),
            vlen = Buffer.byteLength(value);

        len += 8 + klen + vlen;
        return [klen, key, vlen, value];
      }),
      result = new Buffer(len);

  result.writeUInt32BE(pairs.length, 0, true);

  var offset = 4;
  pairs.forEach(function(pair) {
    // Write key length
    result.writeUInt32BE(pair[0], offset, true);
    // Write key
    result.write(pair[1], offset + 4);

    offset += pair[0] + 4;

    // Write value length
    result.writeUInt32BE(pair[2], offset, true);
    // Write value
    result.write(pair[3], offset + 4);

    offset += pair[2] + 4;
  });

  return result;
};
Example #20
0
// 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);


// Now test protecting users from doing stupid things

assert.throws(function() {
  function AB() { }
  AB.__proto__ = ArrayBuffer;
  AB.prototype.__proto__ = ArrayBuffer.prototype;
  new Buffer(new AB());
}, TypeError);
Example #21
0
NetWriter.prototype.writeUInt32BE = function (value) {
    var buffer = new Buffer(4);
    buffer.writeUInt32BE(value, 0, true);
    this.content.push(buffer);
    this.size += 4;
};