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

fix data descriptor handling #338

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
43 changes: 36 additions & 7 deletions lib/PullStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ PullStream.prototype._write = function(chunk, e, cb) {
PullStream.prototype.stream = function(eof, includeEof) {
const p = Stream.PassThrough();
let done;
const self= this;
let consumedLength = 0;

const self = this;
const DATA_DESCRIPTOR_SIGNATURE = Buffer.alloc(4);
DATA_DESCRIPTOR_SIGNATURE.writeUInt32LE(0x08074b50, 0);

function cb() {
if (typeof self.cb === strFunction) {
Expand All @@ -39,23 +43,47 @@ PullStream.prototype.stream = function(eof, includeEof) {
}
}

function getDataDescriptorIndex(startIndex) {
const matchedIndex = self.buffer.indexOf(DATA_DESCRIPTOR_SIGNATURE, startIndex);
if (matchedIndex !== -1) {
if (self.buffer.length >= matchedIndex + 12) {
const compressedSize = self.buffer.readUInt32LE(matchedIndex + 8); // 8 bytes after the data descriptor signature is the size of the compressed file.

if (compressedSize === matchedIndex + consumedLength) {
return matchedIndex;
} else {
// Not a data descriptor signature for this buffer, so search after it.
return getDataDescriptorIndex(matchedIndex + 1);
}
}
}
return -1;
}

function pull() {
let packet;
if (self.buffer && self.buffer.length) {
if (typeof eof === 'number') {
packet = self.buffer.slice(0, eof);
self.buffer = self.buffer.slice(eof);
consumedLength += packet.length;
eof -= packet.length;
done = done || !eof;
} else {
let match = self.buffer.indexOf(eof);
if (match !== -1) {
let matchedIndex = -1;
if (DATA_DESCRIPTOR_SIGNATURE.equals(eof)) {
matchedIndex = getDataDescriptorIndex(0);
} else {
matchedIndex = self.buffer.indexOf(eof);
}
if (matchedIndex !== -1) {
// store signature match byte offset to allow us to reference
// this for zip64 offset
self.match = match;
if (includeEof) match = match + eof.length;
packet = self.buffer.slice(0, match);
self.buffer = self.buffer.slice(match);
self.match = matchedIndex;
if (includeEof) matchedIndex = matchedIndex + eof.length;
packet = self.buffer.slice(0, matchedIndex);
self.buffer = self.buffer.slice(matchedIndex);
consumedLength += packet.length;
done = true;
} else {
const len = self.buffer.length - eof.length;
Expand All @@ -64,6 +92,7 @@ PullStream.prototype.stream = function(eof, includeEof) {
} else {
packet = self.buffer.slice(0, len);
self.buffer = self.buffer.slice(len);
consumedLength += packet.length;
}
}
}
Expand Down