ar
Version:
ar - Read Unix archive files.
138 lines (117 loc) • 4.3 kB
JavaScript
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Archive = (function () {
function Archive(data) {
this.data = data;
this.files = [];
if (data.toString('utf8', 0, 8) !== "!<arch>\n") {
throw new Error("Invalid archive file: Missing magic header '!<arch>\\n'");
}
this.createFiles();
}
Archive.prototype.createFiles = function () {
if (this.files.length > 0)
return;
var offset = 8, file;
while (offset < this.data.length) {
file = new BSDARFile(this.data.slice(offset));
this.files.push(file);
offset += file.totalSize();
}
};
Archive.prototype.getFiles = function () {
return this.files;
};
return Archive;
})();
exports.Archive = Archive;
function getPaddingBytes(size, alignment) {
return (alignment - (size % alignment)) % alignment;
}
function trimWhitespace(str) {
return String.prototype.trim ? str.trim() : str.replace(/^\s+|\s+$/gm, '');
}
function trimNulls(str) {
return str.replace(/\0/g, '');
}
var ARCommonFile = (function () {
function ARCommonFile(data) {
this.data = data;
if (this.fmag() !== "`\n") {
throw new Error("Record is missing header trailer string; instead, it has: " + this.fmag());
}
}
ARCommonFile.prototype.name = function () {
return trimWhitespace(this.data.toString('utf8', 0, 16));
};
ARCommonFile.prototype.date = function () {
return new Date(parseInt(this.data.toString('ascii', 16, 28), 10));
};
ARCommonFile.prototype.uid = function () {
return parseInt(this.data.toString('ascii', 28, 34), 10);
};
ARCommonFile.prototype.gid = function () {
return parseInt(this.data.toString('ascii', 34, 40), 10);
};
ARCommonFile.prototype.mode = function () {
return parseInt(this.data.toString('ascii', 40, 48), 8);
};
ARCommonFile.prototype.dataSize = function () {
return parseInt(this.data.toString('ascii', 48, 58), 10);
};
ARCommonFile.prototype.fileSize = function () {
return this.dataSize();
};
ARCommonFile.prototype.fmag = function () {
return this.data.toString('ascii', 58, 60);
};
ARCommonFile.prototype.headerSize = function () {
return 60;
};
ARCommonFile.prototype.totalSize = function () {
var headerSize = this.headerSize(), dataSize = this.dataSize();
return headerSize + dataSize + getPaddingBytes(dataSize, 2);
};
ARCommonFile.prototype.fileData = function () {
var headerSize = this.headerSize();
return this.data.slice(headerSize, headerSize + this.dataSize());
};
return ARCommonFile;
})();
exports.ARCommonFile = ARCommonFile;
var BSDARFile = (function (_super) {
__extends(BSDARFile, _super);
function BSDARFile(data) {
_super.call(this, data);
this.appendedFileName = _super.prototype.name.call(this).substr(0, 3) === "#1/";
}
BSDARFile.prototype.appendedNameSize = function () {
if (this.appendedFileName) {
return parseInt(_super.prototype.name.call(this).substr(3), 10);
}
return 0;
};
BSDARFile.prototype.name = function () {
var length, name = _super.prototype.name.call(this), headerSize;
if (this.appendedFileName) {
length = this.appendedNameSize();
headerSize = _super.prototype.headerSize.call(this);
name = trimNulls(this.data.toString('utf8', headerSize, headerSize + length));
}
return name;
};
BSDARFile.prototype.fileSize = function () {
return this.dataSize() - this.appendedNameSize();
};
BSDARFile.prototype.fileData = function () {
var headerSize = this.headerSize(), appendedNameSize = this.appendedNameSize();
return this.data.slice(headerSize + appendedNameSize, headerSize + appendedNameSize + this.fileSize());
};
return BSDARFile;
})(ARCommonFile);
exports.BSDARFile = BSDARFile;
//# sourceMappingURL=ar.js.map