Skip to content

Commit

Permalink
inital "decoder" functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed Jun 12, 2024
1 parent 626dd94 commit 4d84157
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
13 changes: 9 additions & 4 deletions adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ module.exports = function (/**String*/ input, /** object */ options) {
// instanciate utils filesystem
const filetools = new Utils(opts);

opts.decoder = Utils.decoder;
if (typeof opts.decoder !== "object" || typeof opts.decoder.encode !== "function" || typeof opts.decoder.decode !== "function") {
opts.decoder = Utils.decoder;
}

// if input is file name we retrieve its content
if (input && "string" === typeof input) {
// load zip file
Expand Down Expand Up @@ -553,7 +558,7 @@ module.exports = function (/**String*/ input, /** object */ options) {

// prepare new entry
if (!update) {
entry = new ZipEntry();
entry = new ZipEntry(opts);
entry.entryName = entryName;
}
entry.comment = comment || "";
Expand Down Expand Up @@ -731,7 +736,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
throw new Error(Utils.Errors.NO_ZIP);
}
_zip.entries.forEach(function (entry) {
var entryName = sanitize(targetPath, canonical(entry.entryName.toString()));
var entryName = sanitize(targetPath, canonical(entry.entryName));
if (entry.isDirectory) {
filetools.makeDir(entryName);
return;
Expand Down Expand Up @@ -784,7 +789,7 @@ module.exports = function (/**String*/ input, /** object */ options) {

targetPath = pth.resolve(targetPath);
// convert entryName to
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName.toString())));
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName)));
const getError = (msg, file) => new Error(msg + ': "' + file + '"');

// separate directories from files
Expand Down Expand Up @@ -819,7 +824,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
if (err) {
next(err);
} else {
const entryName = pth.normalize(canonical(entry.entryName.toString()));
const entryName = pth.normalize(canonical(entry.entryName));
const filePath = sanitize(targetPath, entryName);
entry.getDataAsync(function (content, err_1) {
if (err_1) {
Expand Down
11 changes: 11 additions & 0 deletions headers/entryHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ module.exports = function () {
_flags = val;
},

get flags_efs() {
return (_flags & Constants.FLG_EFS) > 0;
},
set flags_efs(val) {
if (val) {
_flags |= Constants.FLG_EFS;
} else {
_flags &= ~Constants.FLG_EFS;
}
},

get method() {
return _method;
},
Expand Down
5 changes: 5 additions & 0 deletions util/decoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
efs: true,
encode: (data) => Buffer.from(data, "utf8"),
decode: (data) => data.toString("utf8")
};
1 change: 1 addition & 0 deletions util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ module.exports = require("./utils");
module.exports.Constants = require("./constants");
module.exports.Errors = require("./errors");
module.exports.FileAttr = require("./fattr");
module.exports.decoder = require("./decoder");
4 changes: 2 additions & 2 deletions util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ Utils.sanitize = function (/*string*/ prefix, /*string*/ name) {
};

// converts buffer, Uint8Array, string types to buffer
Utils.toBuffer = function toBuffer(/*buffer, Uint8Array, string*/ input) {
Utils.toBuffer = function toBuffer(/*buffer, Uint8Array, string*/ input, /* function */ encoder) {
if (Buffer.isBuffer(input)) {
return input;
} else if (input instanceof Uint8Array) {
return Buffer.from(input);
} else {
// expect string all other values are invalid and return empty buffer
return typeof input === "string" ? Buffer.from(input, "utf8") : Buffer.alloc(0);
return typeof input === "string" ? encoder(input) : Buffer.alloc(0);
}
};

Expand Down
30 changes: 23 additions & 7 deletions zipEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ var Utils = require("./util"),
Constants = Utils.Constants,
Methods = require("./methods");

module.exports = function (/*Buffer*/ input) {
module.exports = function (/** object */ options, /*Buffer*/ input) {
var _centralHeader = new Headers.EntryHeader(),
_entryName = Buffer.alloc(0),
_comment = Buffer.alloc(0),
_isDirectory = false,
uncompressedData = null,
_extra = Buffer.alloc(0);
_extra = Buffer.alloc(0),
_efs = true;

// assign options
const opts = options;

const decoder = typeof opts.decoder === "object" ? opts.decoder : Utils.decoder;
_efs = decoder.hasOwnProperty("efs") ? decoder.efs : false;

function getCompressedDataFromZip() {
//if (!input || !Buffer.isBuffer(input)) {
Expand Down Expand Up @@ -198,18 +205,26 @@ module.exports = function (/*Buffer*/ input) {

return {
get entryName() {
return _entryName.toString();
return decoder.decode(_entryName);
},
get rawEntryName() {
return _entryName;
},
set entryName(val) {
_entryName = Utils.toBuffer(val);
_entryName = Utils.toBuffer(val, decoder.encode);
var lastChar = _entryName[_entryName.length - 1];
_isDirectory = lastChar === 47 || lastChar === 92;
_centralHeader.fileNameLength = _entryName.length;
},

get efs() {
if (typeof _efs === "function") {
return _efs(this.entryName);
} else {
return _efs;
}
},

get extra() {
return _extra;
},
Expand All @@ -220,10 +235,10 @@ module.exports = function (/*Buffer*/ input) {
},

get comment() {
return _comment.toString();
return decoder.decode(_comment);
},
set comment(val) {
_comment = Utils.toBuffer(val);
_comment = Utils.toBuffer(val, decoder.encode);
_centralHeader.commentLength = _comment.length;
},

Expand All @@ -249,7 +264,7 @@ module.exports = function (/*Buffer*/ input) {
},

setData: function (value) {
uncompressedData = Utils.toBuffer(value);
uncompressedData = Utils.toBuffer(value, Utils.decoder.encode);
if (!_isDirectory && uncompressedData.length) {
_centralHeader.size = uncompressedData.length;
_centralHeader.method = Utils.Constants.DEFLATED;
Expand Down Expand Up @@ -293,6 +308,7 @@ module.exports = function (/*Buffer*/ input) {
},

packCentralHeader: function () {
_centralHeader.flag_efs = this.efs;
// 1. create header (buffer)
var header = _centralHeader.centralHeaderToBinary();
var addpos = Utils.Constants.CENHDR;
Expand Down
12 changes: 6 additions & 6 deletions zipFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
var password = null;

// assign options
const opts = Object.assign(Object.create(null), options);
const opts = options;

const { noSort } = opts;
const { noSort, decoder } = opts;

if (inBuffer) {
// is a memory buffer
Expand All @@ -29,7 +29,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {

for (let i = 0; i < totalEntries; i++) {
let tmp = index;
const entry = new ZipEntry(inBuffer);
const entry = new ZipEntry(opts, inBuffer);

entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));
entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
Expand All @@ -50,7 +50,7 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
var index = mainHeader.offset; // offset of first CEN header
for (var i = 0; i < entryList.length; i++) {
var tmp = index,
entry = new ZipEntry(inBuffer);
entry = new ZipEntry(opts, inBuffer);
entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));

entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
Expand Down Expand Up @@ -134,10 +134,10 @@ module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
* @return {String}
*/
get comment() {
return _comment.toString();
return decoder.decode(_comment);
},
set comment(val) {
_comment = Utils.toBuffer(val);
_comment = Utils.toBuffer(val, decoder.encode);
mainHeader.commentLength = _comment.length;
},

Expand Down

0 comments on commit 4d84157

Please sign in to comment.