/**
 * node-zip-stream
 *
 * Copyright (c) 2014 Chris Talkington, contributors.
 * Licensed under the MIT license.
 * https://github.com/ctalkington/node-zip-stream/blob/master/LICENSE-MIT
 */
var zlib = require('zlib');
var inherits = require('util').inherits;

var crc32 = require('buffer-crc32');

function DeflateRawChecksum(options) {
  zlib.DeflateRaw.call(this, options);

  this.checksum = new Buffer(4);
  this.checksum.writeInt32BE(0, 0);

  this.rawSize = 0;
  this.compressedSize = 0;

  // BC v0.8
  if (typeof zlib.DeflateRaw.prototype.push !== 'function') {
    this.on('data', function(chunk) {
      if (chunk) {
        this.compressedSize += chunk.length;
      }
    });
  }
}

inherits(DeflateRawChecksum, zlib.DeflateRaw);

DeflateRawChecksum.prototype.push = function(chunk, encoding) {
  if (chunk) {
    this.compressedSize += chunk.length;
  }

  return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding);
};

DeflateRawChecksum.prototype.write = function(chunk, cb) {
  if (chunk) {
    this.checksum = crc32(chunk, this.checksum);
    this.rawSize += chunk.length;
  }

  return zlib.DeflateRaw.prototype.write.call(this, chunk, cb);
};

DeflateRawChecksum.prototype.digest = function() {
  return crc32.unsigned(0, this.checksum);
};

DeflateRawChecksum.prototype.size = function() {
  return this.rawSize;
};

module.exports = DeflateRawChecksum;

/**
 * node-zip-stream
 *
 * Copyright (c) 2014 Chris Talkington, contributors.
 * Licensed under the MIT license.
 * https://github.com/ctalkington/node-zip-stream/blob/master/LICENSE-MIT
 */
var zlib = require('zlib');
var inherits = require('util').inherits;

var crc32 = require('buffer-crc32');

function DeflateRawChecksum(options) {
  zlib.DeflateRaw.call(this, options);

  this.checksum = new Buffer(4);
  this.checksum.writeInt32BE(0, 0);

  this.rawSize = 0;
  this.compressedSize = 0;

  // BC v0.8
  if (typeof zlib.DeflateRaw.prototype.push !== 'function') {
    this.on('data', function(chunk) {
      if (chunk) {
        this.compressedSize += chunk.length;
      }
    });
  }
}

inherits(DeflateRawChecksum, zlib.DeflateRaw);

DeflateRawChecksum.prototype.push = function(chunk, encoding) {
  if (chunk) {
    this.compressedSize += chunk.length;
  }

  return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding);
};

DeflateRawChecksum.prototype.write = function(chunk, cb) {
  if (chunk) {
    this.checksum = crc32(chunk, this.checksum);
    this.rawSize += chunk.length;
  }

  return zlib.DeflateRaw.prototype.write.call(this, chunk, cb);
};

DeflateRawChecksum.prototype.digest = function() {
  return crc32.unsigned(0, this.checksum);
};

DeflateRawChecksum.prototype.hex = function() {
  return this.digest().toString(16).toUpperCase();
};

DeflateRawChecksum.prototype.size = function() {
  return this.rawSize;
};

module.exports = DeflateRawChecksum;

add hex method to DeflateRawChecksum to closer match crc32-stream.