Do not process if checksum errors

This commit is contained in:
Luke Page 2015-08-03 16:35:05 +01:00
parent b89ef0384f
commit 2914659815
2 changed files with 20 additions and 8 deletions

View file

@ -34,7 +34,7 @@ var ParserAsync = module.exports = function(options) {
this._parser = new Parser(options, { this._parser = new Parser(options, {
read: this.read.bind(this), read: this.read.bind(this),
error: this.emit.bind(this, "error"), error: this._handleError.bind(this),
metadata: this.emit.bind(this, "metadata"), metadata: this.emit.bind(this, "metadata"),
gamma: this.emit.bind(this, "gamma"), gamma: this.emit.bind(this, "gamma"),
finished: this._finished.bind(this), finished: this._finished.bind(this),
@ -44,13 +44,14 @@ var ParserAsync = module.exports = function(options) {
this._options = options; this._options = options;
this.writable = true; this.writable = true;
this.on('error', this._handleError.bind(this));
this._parser.start(); this._parser.start();
}; };
util.inherits(ParserAsync, ChunkStream); util.inherits(ParserAsync, ChunkStream);
ParserAsync.prototype._handleError = function() { ParserAsync.prototype._handleError = function(err) {
this.emit('error', err);
this.writable = false; this.writable = false;
@ -59,6 +60,8 @@ ParserAsync.prototype._handleError = function() {
if (this._inflate && this._inflate.destroy) { if (this._inflate && this._inflate.destroy) {
this._inflate.destroy(); this._inflate.destroy();
} }
this.errord = true;
}; };
ParserAsync.prototype._inflateData = function(data) { ParserAsync.prototype._inflateData = function(data) {
@ -89,6 +92,10 @@ ParserAsync.prototype._createData = function(width, height, bpp, depth, interlac
}; };
ParserAsync.prototype._finished = function(data) { ParserAsync.prototype._finished = function(data) {
if (this.errord) {
return;
}
if (!this._inflate) { if (!this._inflate) {
this.emit('error', 'No Inflate block'); this.emit('error', 'No Inflate block');
} else { } else {
@ -100,6 +107,10 @@ ParserAsync.prototype._finished = function(data) {
ParserAsync.prototype._complete = function(data, width, height) { ParserAsync.prototype._complete = function(data, width, height) {
if (this.errord) {
return;
}
try { try {
data = bitmapper.dataToBitMap(data, width, height, data = bitmapper.dataToBitMap(data, width, height,
this._bpp, this._bpp,
@ -109,7 +120,8 @@ ParserAsync.prototype._complete = function(data, width, height) {
data = this._parser.reverseFiltered(data, this._depth, width, height); data = this._parser.reverseFiltered(data, this._depth, width, height);
} }
catch(e) { catch(e) {
this.emit('error', e); this._handleError();
return;
} }
this.emit('parsed', data); this.emit('parsed', data);

View file

@ -35,7 +35,7 @@ module.exports = function(done) {
var png = PNG.sync.read(data); var png = PNG.sync.read(data);
} catch (e) { } catch (e) {
if (!expectedError) { if (!expectedError) {
console.log("Unexpected error parsing.." + file); console.log("Sync: Unexpected error parsing.." + file);
console.log(e); console.log(e);
console.log(e.stack); console.log(e.stack);
} }
@ -45,7 +45,7 @@ module.exports = function(done) {
if (!syncError) { if (!syncError) {
if (expectedError) { if (expectedError) {
console.log("Error expected, parsed fine ..", file); console.log("Sync: Error expected, parsed fine ..", file);
complete(); complete();
} else { } else {
@ -66,14 +66,14 @@ module.exports = function(done) {
.pipe(new PNG()) .pipe(new PNG())
.on('error', function (err) { .on('error', function (err) {
if (!expectedError) { if (!expectedError) {
console.log("Error reading " + file, err); console.log("Async: Unexpected error parsing.." + file, err);
} }
complete(); complete();
}) })
.on('parsed', function () { .on('parsed', function () {
if (expectedError) { if (expectedError) {
console.log("Error expected, parsed fine", file); console.log("Async: Error expected, parsed fine ..", file);
} }
//this.adjustGamma(); //this.adjustGamma();