Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanning a local file headers is not necessary (except in the case of corrupted archives) #451

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions headers/entryHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,39 +168,60 @@ module.exports = function () {
},

get realDataOffset() {
return _offset + Constants.LOCHDR + _dataHeader.fnameLen + _dataHeader.extraLen;
return _offset + Constants.LOCHDR + _fnameLen + _extraLen;
},

get dataHeader() {
return _dataHeader;
},

loadDataHeaderFromBinary: function (/*Buffer*/ input) {
var data = input.slice(_offset, _offset + Constants.LOCHDR);
// 30 bytes and should start with "PK\003\004"
if (data.readUInt32LE(0) !== Constants.LOCSIG) {
throw new Error(Utils.Errors.INVALID_LOC);
}
_dataHeader = {
// version needed to extract
version: data.readUInt16LE(Constants.LOCVER),
version: _version,
// general purpose bit flag
flags: data.readUInt16LE(Constants.LOCFLG),
flags: _flags,
// compression method
method: data.readUInt16LE(Constants.LOCHOW),
method: _method,
// modification time (2 bytes time, 2 bytes date)
time: data.readUInt32LE(Constants.LOCTIM),
time: _time,
// uncompressed file crc-32 value
crc: data.readUInt32LE(Constants.LOCCRC),
crc: _crc,
// compressed size
compressedSize: data.readUInt32LE(Constants.LOCSIZ),
compressedSize: _compressedSize,
// uncompressed size
size: data.readUInt32LE(Constants.LOCLEN),
size: _size,
// filename length
fnameLen: data.readUInt16LE(Constants.LOCNAM),
fnameLen: _fnameLen,
// extra field length
extraLen: data.readUInt16LE(Constants.LOCEXT)
extraLen: _extraLen
};

return _dataHeader;
},

loadDataHeaderFromBinary: function (/*Buffer*/ input) {
var data = input.slice(_offset, _offset + Constants.LOCHDR);
// 30 bytes and should start with "PK\003\004"
if (data.readUInt32LE(0) !== Constants.LOCSIG) {
throw new Error(Utils.Errors.INVALID_LOC);
}

// version needed to extract
_version = data.readUInt16LE(Constants.LOCVER);
// general purpose bit flag
_flags = data.readUInt16LE(Constants.LOCFLG);
// compression method
_method = data.readUInt16LE(Constants.LOCHOW);
// modification time (2 bytes time, 2 bytes date)
_time = data.readUInt32LE(Constants.LOCTIM);
// uncompressed file crc-32 value
_crc = data.readUInt32LE(Constants.LOCCRC);
// compressed size
_compressedSize = data.readUInt32LE(Constants.LOCSIZ);
// uncompressed size
_size = data.readUInt32LE(Constants.LOCLEN);
// filename length
_fnameLen = data.readUInt16LE(Constants.LOCNAM);
// extra field length
_extraLen = data.readUInt16LE(Constants.LOCEXT);

},

loadFromBinary: function (/*Buffer*/ data) {
Expand Down
3 changes: 2 additions & 1 deletion zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function (/*Buffer*/ input) {
if (!input || !Buffer.isBuffer(input)) {
return Buffer.alloc(0);
}
_entryHeader.loadDataHeaderFromBinary(input);
//Scanning a local file headers is not necessary (except in the case of corrupted archives)
if(!_entryHeader.compressedSize) _entryHeader.loadDataHeaderFromBinary(input);
return input.slice(_entryHeader.realDataOffset, _entryHeader.realDataOffset + _entryHeader.compressedSize);
}

Expand Down