Specify length when parsing octal fields (#62)

This commit is contained in:
Bob Zoller 2017-05-11 06:10:23 -07:00 committed by Mathias Buus
parent b4282a190c
commit 832371a43c

View file

@ -132,10 +132,13 @@ function parse256 (buf) {
return positive ? sum : -1 * sum return positive ? sum : -1 * sum
} }
var decodeOct = function (val, offset) { var decodeOct = function (val, offset, length) {
val = val.slice(offset, offset + length)
offset = 0
// If prefixed with 0x80 then parse as a base-256 integer // If prefixed with 0x80 then parse as a base-256 integer
if (val[offset] & 0x80) { if (val[offset] & 0x80) {
return parse256(val.slice(offset, offset + 8)) return parse256(val)
} else { } else {
// Older versions of tar can prefix with spaces // Older versions of tar can prefix with spaces
while (offset < val.length && val[offset] === 32) offset++ while (offset < val.length && val[offset] === 32) offset++
@ -241,17 +244,17 @@ 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 name = decodeStr(buf, 0, 100) var name = decodeStr(buf, 0, 100)
var mode = decodeOct(buf, 100) var mode = decodeOct(buf, 100, 8)
var uid = decodeOct(buf, 108) var uid = decodeOct(buf, 108, 8)
var gid = decodeOct(buf, 116) var gid = decodeOct(buf, 116, 8)
var size = decodeOct(buf, 124) var size = decodeOct(buf, 124, 12)
var mtime = decodeOct(buf, 136) var mtime = decodeOct(buf, 136, 12)
var type = toType(typeflag) var type = toType(typeflag)
var linkname = buf[157] === 0 ? null : decodeStr(buf, 157, 100) var linkname = buf[157] === 0 ? null : decodeStr(buf, 157, 100)
var uname = decodeStr(buf, 265, 32) var uname = decodeStr(buf, 265, 32)
var gname = decodeStr(buf, 297, 32) var gname = decodeStr(buf, 297, 32)
var devmajor = decodeOct(buf, 329) var devmajor = decodeOct(buf, 329, 8)
var devminor = decodeOct(buf, 337) var devminor = decodeOct(buf, 337, 8)
if (buf[345]) name = decodeStr(buf, 345, 155) + '/' + name if (buf[345]) name = decodeStr(buf, 345, 155) + '/' + name
@ -264,7 +267,7 @@ exports.decode = function (buf) {
if (c === 8 * 32) return null if (c === 8 * 32) return null
// valid checksum // valid checksum
if (c !== decodeOct(buf, 148)) throw new Error('Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?') if (c !== decodeOct(buf, 148, 8)) throw new Error('Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?')
return { return {
name: name, name: name,