From 81ddf7dcb69efafb37fcf08c5b0eccdb54fa8ed3 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Mon, 24 Mar 2014 19:57:01 +1300 Subject: [PATCH] emit error on invalid header --- extract.js | 9 +++++++-- headers.js | 43 +++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/extract.js b/extract.js index 6f3d3c6..280ef55 100644 --- a/extract.js +++ b/extract.js @@ -75,7 +75,12 @@ var Extract = function(opts) { }; var onheader = function() { - var header = self._header = headers.decode(b.slice(0, 512)); + var header + try { + header = self._header = headers.decode(b.slice(0, 512)); + } catch (err) { + self.emit('error', err) + } b.consume(512); if (!header) { @@ -172,4 +177,4 @@ Extract.prototype._write = function(data, enc, cb) { this._onparse(); }; -module.exports = Extract; \ No newline at end of file +module.exports = Extract; diff --git a/headers.js b/headers.js index e831a0f..cbc8697 100644 --- a/headers.js +++ b/headers.js @@ -176,8 +176,6 @@ exports.decode = function(buf) { var typeflag = buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET; var type = toType(typeflag); - if (!type) return null; - var name = decodeStr(buf, 0, 100); var mode = decodeOct(buf, 100); var uid = decodeOct(buf, 108); @@ -192,20 +190,29 @@ exports.decode = function(buf) { if (buf[345]) name = decodeStr(buf, 345, 155)+'/'+name; - if (cksum(buf) !== decodeOct(buf, 148)) return null; + var c = cksum(buf) + + //checksum is still initial value if header was null. + if (c === 8*32) return null; + + //valid checksum + if (c === decodeOct(buf, 148)) + return { + name: name, + mode: mode, + uid: uid, + gid: gid, + size: size, + mtime: new Date(1000 * mtime), + type: toType(typeflag), + linkname: linkname, + uname: uname, + gname: gname, + devmajor: devmajor, + devminor: devminor + }; + + //invalid checksum + throw new Error('invalid header'); +}; - return { - name: name, - mode: mode, - uid: uid, - gid: gid, - size: size, - mtime: new Date(1000 * mtime), - type: toType(typeflag), - linkname: linkname, - uname: uname, - gname: gname, - devmajor: devmajor, - devminor: devminor - }; -}; \ No newline at end of file