2017-08-09 06:50:59 +00:00
|
|
|
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
|
|
|
/* eslint-env node */
|
|
|
|
/* vim: set ts=2 ft=javascript: */
|
2018-09-04 07:14:20 +00:00
|
|
|
/// <reference types="../node_modules/@types/node" />
|
2017-10-20 20:36:54 +00:00
|
|
|
const n = "cfb";
|
2017-09-14 21:14:22 +00:00
|
|
|
import * as X from 'cfb';
|
2017-07-28 17:53:08 +00:00
|
|
|
import fs = require('fs');
|
|
|
|
import program = require('commander');
|
2017-08-09 06:50:59 +00:00
|
|
|
import PRINTJ = require("printj");
|
2017-10-20 20:36:54 +00:00
|
|
|
const sprintf = PRINTJ.sprintf;
|
2017-07-28 17:53:08 +00:00
|
|
|
program
|
2017-09-14 21:14:22 +00:00
|
|
|
.version(X.version)
|
2017-10-20 20:36:54 +00:00
|
|
|
.usage('[options] <file> [subfiles...]')
|
2017-08-09 06:50:59 +00:00
|
|
|
.option('-l, --list-files', 'list files')
|
2017-09-14 21:14:22 +00:00
|
|
|
.option('-r, --repair', 'attempt to repair and garbage-collect archive')
|
2017-10-20 20:36:54 +00:00
|
|
|
.option('-c, --create', 'create file')
|
|
|
|
.option('-a, --append', 'add files to CFB (overwrite existing data)')
|
|
|
|
.option('-d, --delete', 'delete files from CFB')
|
2018-03-05 03:49:40 +00:00
|
|
|
.option('-O, --to-stdout', 'extract raw contents to stdout')
|
|
|
|
.option('-z, --dump', 'dump internal representation but do not extract')
|
|
|
|
.option('-q, --quiet', 'process but do not report')
|
2017-08-09 06:50:59 +00:00
|
|
|
.option('--dev', 'development mode')
|
|
|
|
.option('--read', 'read but do not print out contents');
|
|
|
|
|
|
|
|
program.parse(process.argv);
|
2017-07-28 17:53:08 +00:00
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
const exit = process.exit;
|
|
|
|
const die = (errno: number, msg: string) => { console.error(n + ": " + msg); exit(errno); };
|
|
|
|
const logit = (cmd: string, f: string) => { console.error(sprintf("%-6s %s", cmd, f)); };
|
|
|
|
|
|
|
|
if(program.args.length === 0) die(1, "must specify a filename");
|
|
|
|
|
|
|
|
if(program.create) {
|
|
|
|
logit("create", program.args[0]);
|
|
|
|
const newcfb = X.utils.cfb_new();
|
|
|
|
X.writeFile(newcfb, program.args[0]);
|
2017-07-28 17:53:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
if(!fs.existsSync(program.args[0])) die(1, "must specify a filename");
|
|
|
|
|
2018-02-12 07:30:44 +00:00
|
|
|
const opts: X.CFB$ParsingOptions = {type:'file'};
|
2017-08-09 06:50:59 +00:00
|
|
|
if(program.dev) opts.WTF = true;
|
2017-07-28 17:53:08 +00:00
|
|
|
|
2018-02-12 07:30:44 +00:00
|
|
|
const cfb: X.CFB$Container = X.read(program.args[0], opts);
|
2017-10-20 20:36:54 +00:00
|
|
|
if(program.quiet) exit(0);
|
2017-07-28 17:53:08 +00:00
|
|
|
|
|
|
|
if(program.dump) {
|
|
|
|
console.log("Full Paths:");
|
|
|
|
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
|
2017-10-20 20:36:54 +00:00
|
|
|
console.log("File Index:");
|
|
|
|
console.log(cfb.FileIndex);
|
|
|
|
exit(0);
|
2017-09-14 21:14:22 +00:00
|
|
|
}
|
2017-10-20 20:36:54 +00:00
|
|
|
if(program.repair) { X.writeFile(cfb, program.args[0]); exit(0); }
|
2017-08-09 06:50:59 +00:00
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
const fix_string = (x: string): string => x.replace(/[\u0000-\u001f]/, ($$) => sprintf("\\u%04X", $$.charCodeAt(0)));
|
|
|
|
const format_date = (date: Date): string => {
|
|
|
|
return sprintf("%02u-%02u-%02u %02u:%02u", date.getUTCMonth()+1, date.getUTCDate(), date.getUTCFullYear()%100, date.getUTCHours(), date.getUTCMinutes());
|
|
|
|
};
|
2017-08-09 06:50:59 +00:00
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
if(program.listFiles) {
|
2017-08-09 06:50:59 +00:00
|
|
|
let basetime = new Date(1980,0,1);
|
2017-09-14 21:14:22 +00:00
|
|
|
let cnt = 0, rootsize = 0, filesize = 0;
|
2017-08-09 06:50:59 +00:00
|
|
|
console.log(" Length Date Time Name");
|
|
|
|
console.log(" -------- ---- ---- ----");
|
2018-02-12 07:30:44 +00:00
|
|
|
cfb.FileIndex.forEach((file: X.CFB$Entry, i: number) => {
|
2017-08-09 06:50:59 +00:00
|
|
|
switch(file.type) {
|
|
|
|
case 5:
|
|
|
|
basetime = file.ct || file.mt || basetime;
|
|
|
|
rootsize = file.size;
|
|
|
|
break;
|
|
|
|
case 2:
|
2017-09-14 21:14:22 +00:00
|
|
|
console.log(sprintf("%9lu %s %s", file.size, format_date(basetime), fix_string(cfb.FullPaths[i])));
|
2017-08-09 06:50:59 +00:00
|
|
|
filesize += file.size;
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log(" -------- -------");
|
|
|
|
console.log(sprintf("%9lu %lu file%s", rootsize || filesize, cnt, (cnt !== 1 ? "s" : "")));
|
|
|
|
|
2017-10-20 20:36:54 +00:00
|
|
|
exit(0);
|
2017-07-28 17:53:08 +00:00
|
|
|
}
|
2017-10-20 20:36:54 +00:00
|
|
|
|
|
|
|
const mkdirp = (path: string) => { path.split("/").reduce((acc: string, p: string): string => {
|
|
|
|
acc += p + "/";
|
|
|
|
if(!fs.existsSync(acc)) { logit("mkdir", acc); fs.mkdirSync(acc); }
|
|
|
|
return acc;
|
|
|
|
}, ""); };
|
|
|
|
|
2018-02-12 07:30:44 +00:00
|
|
|
const write = (path: string, data: X.CFB$Entry) => {
|
2017-10-20 20:36:54 +00:00
|
|
|
logit("write", fix_string(path));
|
|
|
|
fs.writeFileSync(path, /*::new Buffer((*/data.content/*:: :any))*/);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(program.create || program.append) {
|
|
|
|
program.args.slice(1).forEach((x: string) => {
|
|
|
|
logit("append", x);
|
|
|
|
X.utils.cfb_add(cfb, "/" + x, fs.readFileSync(x));
|
|
|
|
});
|
|
|
|
X.writeFile(cfb, program.args[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(program.delete) {
|
|
|
|
program.args.slice(1).forEach((x: string) => {
|
|
|
|
logit("delete", x);
|
|
|
|
X.utils.cfb_del(cfb, "/" + x);
|
|
|
|
});
|
|
|
|
X.writeFile(cfb, program.args[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(program.args.length > 1) {
|
|
|
|
program.args.slice(1).forEach((x: string) => {
|
2018-03-05 03:49:40 +00:00
|
|
|
const data: X.CFB$Entry = X.find(cfb, x.replace(/\\u000\d/g,"!"));
|
2017-10-20 20:36:54 +00:00
|
|
|
if(!data) { console.error(x + ": file not found"); return; }
|
|
|
|
if(data.type !== 2) { console.error(x + ": not a file"); return; }
|
|
|
|
const idx = cfb.FileIndex.indexOf(data), path = cfb.FullPaths[idx];
|
2018-03-05 03:49:40 +00:00
|
|
|
if(program.toStdout) return process.stdout.write(new Buffer(<any>data.content));
|
2017-10-20 20:36:54 +00:00
|
|
|
mkdirp(path.slice(0, path.lastIndexOf("/")));
|
|
|
|
write(path, data);
|
|
|
|
});
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2018-03-05 03:49:40 +00:00
|
|
|
if(program.toStdout) exit(0);
|
2017-08-09 06:50:59 +00:00
|
|
|
for(let i=0; i!==cfb.FullPaths.length; ++i) {
|
2017-10-20 20:36:54 +00:00
|
|
|
if(!cfb.FileIndex[i].name) continue;
|
|
|
|
if(cfb.FullPaths[i].slice(-1) === "/") mkdirp(cfb.FullPaths[i]);
|
|
|
|
else write(cfb.FullPaths[i], cfb.FileIndex[i]);
|
2017-07-28 17:53:08 +00:00
|
|
|
}
|