Skip to content

Commit

Permalink
centralHeaders update
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed May 19, 2024
1 parent f0d945b commit ad3afa9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 61 deletions.
6 changes: 3 additions & 3 deletions headers/entryHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ module.exports = function () {
return (_flags & 1) === 1;
},

get entryHeaderSize() {
get centralHeaderSize() {
return Constants.CENHDR + _fnameLen + _extraLen + _comLen;
},

Expand Down Expand Up @@ -268,7 +268,7 @@ module.exports = function () {
return data;
},

entryHeaderToBinary: function () {
centralHeaderToBinary: function () {
// CEN header size (46 bytes)
var data = Buffer.alloc(Constants.CENHDR + _fnameLen + _extraLen + _comLen);
// "PK\001\002"
Expand Down Expand Up @@ -329,7 +329,7 @@ module.exports = function () {
inAttr: _inattr,
attr: _attr,
offset: _offset,
entryHeaderSize: bytes(Constants.CENHDR + _fnameLen + _extraLen + _comLen)
centralHeaderSize: bytes(Constants.CENHDR + _fnameLen + _extraLen + _comLen)
};
},

Expand Down
28 changes: 14 additions & 14 deletions test/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("headers", () => {
});

describe("entry-header", () => {
const entryHeader = require("../headers/entryHeader");
const centralHeader = require("../headers/entryHeader");
const datestamp = [1981, 3, 1, 12, 10, 10];
const readBuf = Buffer.from("504b0102140014000008080045618102efbeadde0001000000020000000000000000000000000000000000000000", "hex");

Expand All @@ -106,15 +106,15 @@ describe("headers", () => {
};

it("compare binary header values with some predetermined values", () => {
const head = new entryHeader();
const head = new centralHeader();
head.loadFromBinary(readBuf);

for (const name in readBufValues) {
expect(head[name]).to.equal(readBufValues[name]);
head[name] = readBufValues[name];
}

expect(head.entryHeaderSize).to.equal(46);
expect(head.centralHeaderSize).to.equal(46);

// split into individual values by local time or timezone messes up our results
expect([head.time.getFullYear(), head.time.getMonth(), head.time.getDate(), head.time.getHours(), head.time.getMinutes(), head.time.getSeconds()]).to.eql(datestamp);
Expand All @@ -135,24 +135,24 @@ describe("headers", () => {
inAttr: 0,
attr: 0,
offset: 0,
entryHeaderSize: "46 bytes"
centralHeaderSize: "46 bytes"
};

headerdata.time = head.time;
expect(head.toJSON()).to.eql(headerdata);
});

it("read binary and create new binary from it, they have to be equal", () => {
const head = new entryHeader();
const head = new centralHeader();
head.loadFromBinary(readBuf);
const buf = head.entryHeaderToBinary();
const buf = head.centralHeaderToBinary();

expect(buf.length).to.equal(readBuf.length);
expect(buf).to.eql(readBuf);
});

it("construct header with values and compare, binaries have to be equal", () => {
const head = new entryHeader();
const head = new centralHeader();

// Set Values
for (const name in readBufValues) {
Expand All @@ -164,20 +164,20 @@ describe("headers", () => {
// if time is constructed by new Date() it is also in local zone and so it cancels possible timezone difference
head.time = new Date(...datestamp);

const buf = head.entryHeaderToBinary();
const buf = head.centralHeaderToBinary();

expect(buf.length).to.equal(readBuf.length);
expect(buf).to.eql(readBuf);
});

it("entryHeaderSize results if postdata is specified", () => {
const head = new entryHeader();
it("centralHeaderSize results if postdata is specified", () => {
const head = new centralHeader();

head.fileNameLength = 100;
head.commentLength = 200;
head.extraLength = 100;

expect(head.entryHeaderSize).to.equal(446);
expect(head.centralHeaderSize).to.equal(446);
});

describe("local-header", () => {
Expand All @@ -195,7 +195,7 @@ describe("headers", () => {
};

it("compare binary header values with predetermined values", () => {
const head = new entryHeader();
const head = new centralHeader();
head.loadFromBinary(readBuf);
head.loadLocalHeaderFromBinary(localHeader);

Expand All @@ -205,7 +205,7 @@ describe("headers", () => {
});

it("read binary and create new binary from it, they have to be equal", () => {
const head = new entryHeader();
const head = new centralHeader();
head.loadFromBinary(readBuf);
head.loadLocalHeaderFromBinary(localHeader);

Expand All @@ -216,7 +216,7 @@ describe("headers", () => {
});

it("construct header by values and compare binaries have to be equal", () => {
const head = new entryHeader();
const head = new centralHeader();
head.loadFromBinary(readBuf);

// Set Values
Expand Down
84 changes: 42 additions & 42 deletions zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var Utils = require("./util"),
Methods = require("./methods");

module.exports = function (/*Buffer*/ input) {
var _entryHeader = new Headers.EntryHeader(),
var _centralHeader = new Headers.EntryHeader(),
_entryName = Buffer.alloc(0),
_comment = Buffer.alloc(0),
_isDirectory = false,
Expand All @@ -15,14 +15,14 @@ module.exports = function (/*Buffer*/ input) {
if (!input || !Buffer.isBuffer(input)) {
return Buffer.alloc(0);
}
_entryHeader.loadLocalHeaderFromBinary(input);
return input.slice(_entryHeader.realDataOffset, _entryHeader.realDataOffset + _entryHeader.compressedSize);
_centralHeader.loadLocalHeaderFromBinary(input);
return input.slice(_centralHeader.realDataOffset, _centralHeader.realDataOffset + _centralHeader.compressedSize);
}

function crc32OK(data) {
// if bit 3 (0x08) of the general-purpose flags field is set, then the CRC-32 and file sizes are not known when the header is written
if ((_entryHeader.flags & 0x8) !== 0x8) {
if (Utils.crc32(data) !== _entryHeader.localHeader.crc) {
if ((_centralHeader.flags & 0x8) !== 0x8) {
if (Utils.crc32(data) !== _centralHeader.localHeader.crc) {
return false;
}
} else {
Expand Down Expand Up @@ -53,16 +53,16 @@ module.exports = function (/*Buffer*/ input) {
return compressedData;
}

if (_entryHeader.encrypted) {
if (_centralHeader.encrypted) {
if ("string" !== typeof pass && !Buffer.isBuffer(pass)) {
throw new Error("ADM-ZIP: Incompatible password parameter");
}
compressedData = Methods.ZipCrypto.decrypt(compressedData, _entryHeader, pass);
compressedData = Methods.ZipCrypto.decrypt(compressedData, _centralHeader, pass);
}

var data = Buffer.alloc(_entryHeader.size);
var data = Buffer.alloc(_centralHeader.size);

switch (_entryHeader.method) {
switch (_centralHeader.method) {
case Utils.Constants.STORED:
compressedData.copy(data);
if (!crc32OK(data)) {
Expand All @@ -74,7 +74,7 @@ module.exports = function (/*Buffer*/ input) {
return data;
}
case Utils.Constants.DEFLATED:
var inflater = new Methods.Inflater(compressedData, _entryHeader.size);
var inflater = new Methods.Inflater(compressedData, _centralHeader.size);
if (!async) {
const result = inflater.inflate(data);
result.copy(data, 0);
Expand Down Expand Up @@ -111,9 +111,9 @@ module.exports = function (/*Buffer*/ input) {
if (uncompressedData.length && !_isDirectory) {
var compressedData;
// Local file header
switch (_entryHeader.method) {
switch (_centralHeader.method) {
case Utils.Constants.STORED:
_entryHeader.compressedSize = _entryHeader.size;
_centralHeader.compressedSize = _centralHeader.size;

compressedData = Buffer.alloc(uncompressedData.length);
uncompressedData.copy(compressedData);
Expand All @@ -125,12 +125,12 @@ module.exports = function (/*Buffer*/ input) {
var deflater = new Methods.Deflater(uncompressedData);
if (!async) {
var deflated = deflater.deflate();
_entryHeader.compressedSize = deflated.length;
_centralHeader.compressedSize = deflated.length;
return deflated;
} else {
deflater.deflateAsync(function (data) {
compressedData = Buffer.alloc(data.length);
_entryHeader.compressedSize = data.length;
_centralHeader.compressedSize = data.length;
data.copy(compressedData);
callback && callback(compressedData);
});
Expand Down Expand Up @@ -171,26 +171,26 @@ module.exports = function (/*Buffer*/ input) {

if (data.length >= Constants.EF_ZIP64_SCOMP) {
size = readUInt64LE(data, Constants.EF_ZIP64_SUNCOMP);
if (_entryHeader.size === Constants.EF_ZIP64_OR_32) {
_entryHeader.size = size;
if (_centralHeader.size === Constants.EF_ZIP64_OR_32) {
_centralHeader.size = size;
}
}
if (data.length >= Constants.EF_ZIP64_RHO) {
compressedSize = readUInt64LE(data, Constants.EF_ZIP64_SCOMP);
if (_entryHeader.compressedSize === Constants.EF_ZIP64_OR_32) {
_entryHeader.compressedSize = compressedSize;
if (_centralHeader.compressedSize === Constants.EF_ZIP64_OR_32) {
_centralHeader.compressedSize = compressedSize;
}
}
if (data.length >= Constants.EF_ZIP64_DSN) {
offset = readUInt64LE(data, Constants.EF_ZIP64_RHO);
if (_entryHeader.offset === Constants.EF_ZIP64_OR_32) {
_entryHeader.offset = offset;
if (_centralHeader.offset === Constants.EF_ZIP64_OR_32) {
_centralHeader.offset = offset;
}
}
if (data.length >= Constants.EF_ZIP64_DSN + 4) {
diskNumStart = data.readUInt32LE(Constants.EF_ZIP64_DSN);
if (_entryHeader.diskNumStart === Constants.EF_ZIP64_OR_16) {
_entryHeader.diskNumStart = diskNumStart;
if (_centralHeader.diskNumStart === Constants.EF_ZIP64_OR_16) {
_centralHeader.diskNumStart = diskNumStart;
}
}
}
Expand All @@ -206,15 +206,15 @@ module.exports = function (/*Buffer*/ input) {
_entryName = Utils.toBuffer(val);
var lastChar = _entryName[_entryName.length - 1];
_isDirectory = lastChar === 47 || lastChar === 92;
_entryHeader.fileNameLength = _entryName.length;
_centralHeader.fileNameLength = _entryName.length;
},

get extra() {
return _extra;
},
set extra(val) {
_extra = val;
_entryHeader.extraLength = val.length;
_centralHeader.extraLength = val.length;
parseExtra(val);
},

Expand All @@ -223,7 +223,7 @@ module.exports = function (/*Buffer*/ input) {
},
set comment(val) {
_comment = Utils.toBuffer(val);
_entryHeader.commentLength = _comment.length;
_centralHeader.commentLength = _comment.length;
},

get name() {
Expand All @@ -250,61 +250,61 @@ module.exports = function (/*Buffer*/ input) {
setData: function (value) {
uncompressedData = Utils.toBuffer(value);
if (!_isDirectory && uncompressedData.length) {
_entryHeader.size = uncompressedData.length;
_entryHeader.method = Utils.Constants.DEFLATED;
_entryHeader.crc = Utils.crc32(value);
_entryHeader.changed = true;
_centralHeader.size = uncompressedData.length;
_centralHeader.method = Utils.Constants.DEFLATED;
_centralHeader.crc = Utils.crc32(value);
_centralHeader.changed = true;
} else {
// folders and blank files should be stored
_entryHeader.method = Utils.Constants.STORED;
_centralHeader.method = Utils.Constants.STORED;
}
},

getData: function (pass) {
if (_entryHeader.changed) {
if (_centralHeader.changed) {
return uncompressedData;
} else {
return decompress(false, null, pass);
}
},

getDataAsync: function (/*Function*/ callback, pass) {
if (_entryHeader.changed) {
if (_centralHeader.changed) {
callback(uncompressedData);
} else {
decompress(true, callback, pass);
}
},

set attr(attr) {
_entryHeader.attr = attr;
_centralHeader.attr = attr;
},
get attr() {
return _entryHeader.attr;
return _centralHeader.attr;
},

set header(/*Buffer*/ data) {
_entryHeader.loadFromBinary(data);
_centralHeader.loadFromBinary(data);
},

get header() {
return _entryHeader;
return _centralHeader;
},

packCentralHeader: function () {
// 1. create header (buffer)
var header = _entryHeader.entryHeaderToBinary();
var header = _centralHeader.centralHeaderToBinary();
var addpos = Utils.Constants.CENHDR;
// 2. add file name
_entryName.copy(header, addpos);
addpos += _entryName.length;
// 3. add extra data
if (_entryHeader.extraLength) {
if (_centralHeader.extraLength) {
_extra.copy(header, addpos);
addpos += _entryHeader.extraLength;
addpos += _centralHeader.extraLength;
}
// 4. add file comment
if (_entryHeader.commentLength) {
if (_centralHeader.commentLength) {
_comment.copy(header, addpos);
}
return header;
Expand All @@ -314,7 +314,7 @@ module.exports = function (/*Buffer*/ input) {
let addpos = 0;

// 1. construct local header Buffer
const localHeaderBuf = _entryHeader.localHeaderToBinary();
const localHeaderBuf = _centralHeader.localHeaderToBinary();
// 2. localHeader - crate header buffer
const localHeader = Buffer.alloc(localHeaderBuf.length + _entryName.length + _extra.length);
// 2.1 add localheader
Expand All @@ -340,7 +340,7 @@ module.exports = function (/*Buffer*/ input) {
name: this.name,
comment: this.comment,
isDirectory: this.isDirectory,
header: _entryHeader.toJSON(),
header: _centralHeader.toJSON(),
compressedData: bytes(input),
data: bytes(uncompressedData)
};
Expand Down
4 changes: 2 additions & 2 deletions zipFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));
entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));

index += entry.header.entryHeaderSize;
index += entry.header.centralHeaderSize;

callback(entry);
}
Expand All @@ -58,7 +58,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {

if (entry.header.commentLength) entry.comment = inBuffer.slice(tmp, tmp + entry.header.commentLength);

index += entry.header.entryHeaderSize;
index += entry.header.centralHeaderSize;

entryList[i] = entry;
entryTable[entry.entryName] = entry;
Expand Down

0 comments on commit ad3afa9

Please sign in to comment.