Skip to content

Commit

Permalink
Merge pull request #490 from 5saviahv/headers
Browse files Browse the repository at this point in the history
Header updates
  • Loading branch information
5saviahv authored May 19, 2024
2 parents 21a4023 + ad3afa9 commit 52facfc
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 114 deletions.
20 changes: 10 additions & 10 deletions headers/entryHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function () {
// Without it file names may be corrupted for other apps when file names use unicode chars
_flags |= Constants.FLG_EFS;

var _dataHeader = {};
var _localHeader = {};

function setTime(val) {
val = new Date(val);
Expand Down Expand Up @@ -165,25 +165,25 @@ module.exports = function () {
return (_flags & 1) === 1;
},

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

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

get dataHeader() {
return _dataHeader;
get localHeader() {
return _localHeader;
},

loadDataHeaderFromBinary: function (/*Buffer*/ input) {
loadLocalHeaderFromBinary: 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 = {
_localHeader = {
// version needed to extract
version: data.readUInt16LE(Constants.LOCVER),
// general purpose bit flag
Expand Down Expand Up @@ -242,7 +242,7 @@ module.exports = function () {
_offset = data.readUInt32LE(Constants.CENOFF);
},

dataHeaderToBinary: function () {
localHeaderToBinary: function () {
// LOC header size (30 bytes)
var data = Buffer.alloc(Constants.LOCHDR);
// "PK\003\004"
Expand All @@ -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
54 changes: 27 additions & 27 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,26 +164,26 @@ 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("data-header", () => {
const dataheader = Buffer.from("504b030414000008080045618102efbeadde000100000002000000000000", "hex");
describe("local-header", () => {
const localHeader = Buffer.from("504b030414000008080045618102efbeadde000100000002000000000000", "hex");

const dataHeaderValues = {
const localHeaderValues = {
compressedSize: 0x100,
crc: 0xdeadbeef,
extraLen: 0,
Expand All @@ -195,28 +195,28 @@ describe("headers", () => {
};

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

for (const name in dataHeaderValues) {
expect(head.dataHeader[name]).to.equal(dataHeaderValues[name]);
for (const name in localHeaderValues) {
expect(head.localHeader[name]).to.equal(localHeaderValues[name]);
}
});

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.loadDataHeaderFromBinary(dataheader);
head.loadLocalHeaderFromBinary(localHeader);

const buf = head.dataHeaderToBinary();
const buf = head.localHeaderToBinary();

expect(buf.length).to.equal(dataheader.length);
expect(buf).to.eql(dataheader);
expect(buf.length).to.equal(localHeader.length);
expect(buf).to.eql(localHeader);
});

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 All @@ -229,10 +229,10 @@ 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.dataHeaderToBinary();
const buf = head.localHeaderToBinary();

expect(buf.length).to.equal(dataheader.length);
expect(buf).to.eql(dataheader);
expect(buf.length).to.equal(localHeader.length);
expect(buf).to.eql(localHeader);
});
});
});
Expand Down
Loading

0 comments on commit 52facfc

Please sign in to comment.