Merge pull request #11 from dominictarr/master

emit error on invalid header
This commit is contained in:
Mathias Buus 2014-03-24 15:07:26 +01:00
commit 37a497ff7e
2 changed files with 32 additions and 20 deletions

View file

@ -75,7 +75,12 @@ var Extract = function(opts) {
}; };
var onheader = function() { 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); b.consume(512);
if (!header) { if (!header) {

View file

@ -176,8 +176,6 @@ exports.decode = function(buf) {
var typeflag = buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET; var typeflag = buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET;
var type = toType(typeflag); var type = toType(typeflag);
if (!type) return null;
var name = decodeStr(buf, 0, 100); var name = decodeStr(buf, 0, 100);
var mode = decodeOct(buf, 100); var mode = decodeOct(buf, 100);
var uid = decodeOct(buf, 108); var uid = decodeOct(buf, 108);
@ -192,8 +190,13 @@ exports.decode = function(buf) {
if (buf[345]) name = decodeStr(buf, 345, 155)+'/'+name; 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 { return {
name: name, name: name,
mode: mode, mode: mode,
@ -208,4 +211,8 @@ exports.decode = function(buf) {
devmajor: devmajor, devmajor: devmajor,
devminor: devminor devminor: devminor
}; };
//invalid checksum
throw new Error('invalid header');
}; };