Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* eslint-env node */
/* vim: set ts=2 ft=javascript: */
const n = "cfb";
import * as X from 'cfb';
import fs = require('fs');
import program = require('commander');
import PRINTJ = require("printj");
const sprintf = PRINTJ.sprintf;
program
.version(X.version)
.usage('[options] [subfiles...]')
.option('-q, --quiet', 'process but do not report')
.option('-l, --list-files', 'list files')
.option('-z, --dump', 'dump internal representation but do not extract')
.option('-r, --repair', 'attempt to repair and garbage-collect archive')
.option('-c, --create', 'create file')
.option('-a, --append', 'add files to CFB (overwrite existing data)')
.option('-d, --delete', 'delete files from CFB')
.option('--dev', 'development mode')
.option('--read', 'read but do not print out contents');
program.parse(process.argv);
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)); };
encryptionInfo.key.cipherAlgorithm,
encryptionInfo.key.cipherChaining,
verifierHashValueKey,
encryptionInfo.key.saltValue,
verifierHashValue
);
// Build the encryption info buffer
const encryptionInfoBuffer = this._buildEncryptionInfo(encryptionInfo);
// Create a new CFB
let output = cfb.utils.cfb_new();
// Add the encryption info and encrypted package
cfb.utils.cfb_add(output, "EncryptionInfo", encryptionInfoBuffer);
cfb.utils.cfb_add(output, "EncryptedPackage", encryptedPackage);
// Delete the SheetJS entry that is added at initialization
cfb.utils.cfb_del(output, "\u0001Sh33tJ5");
// Write to a buffer and return
output = cfb.write(output);
// The cfb library writes to a Uint8array in the browser. Convert to a Buffer.
if (!Buffer.isBuffer(output)) output = Buffer.from(output);
return output;
}
// Use the key to encrypt the hash value
encryptionInfo.key.encryptedVerifierHashValue = this._crypt(
true,
encryptionInfo.key.cipherAlgorithm,
encryptionInfo.key.cipherChaining,
verifierHashValueKey,
encryptionInfo.key.saltValue,
verifierHashValue
);
// Build the encryption info buffer
const encryptionInfoBuffer = this._buildEncryptionInfo(encryptionInfo);
// Create a new CFB
let output = cfb.utils.cfb_new();
// Add the encryption info and encrypted package
cfb.utils.cfb_add(output, "EncryptionInfo", encryptionInfoBuffer);
cfb.utils.cfb_add(output, "EncryptedPackage", encryptedPackage);
// Delete the SheetJS entry that is added at initialization
cfb.utils.cfb_del(output, "\u0001Sh33tJ5");
// Write to a buffer and return
output = cfb.write(output);
// The cfb library writes to a Uint8array in the browser. Convert to a Buffer.
if (!Buffer.isBuffer(output)) output = Buffer.from(output);
return output;
}
decryptAsync(data, password) {
// Parse the CFB input and pull out the encryption info and encrypted package entries.
const parsed = cfb.parse(data);
let encryptionInfoBuffer = _.find(parsed.FileIndex, { name: "EncryptionInfo" }).content;
let encryptedPackageBuffer = _.find(parsed.FileIndex, { name: "EncryptedPackage" }).content;
// In the browser the CFB content is an array. Convert to a Buffer.
if (!Buffer.isBuffer(encryptionInfoBuffer)) encryptionInfoBuffer = Buffer.from(encryptionInfoBuffer);
if (!Buffer.isBuffer(encryptedPackageBuffer)) encryptedPackageBuffer = Buffer.from(encryptedPackageBuffer);
return externals.Promise.resolve()
.then(() => this._parseEncryptionInfoAsync(encryptionInfoBuffer)) // Parse the encryption info XML into an object
.then(encryptionInfo => {
// Convert the password into an encryption key
const key = this._convertPasswordToKey(
password,
encryptionInfo.key.hashAlgorithm,
encryptionInfo.key.saltValue,
encryptionInfo.key.spinCount,
// Build the encryption info buffer
const encryptionInfoBuffer = this._buildEncryptionInfo(encryptionInfo);
// Create a new CFB
let output = cfb.utils.cfb_new();
// Add the encryption info and encrypted package
cfb.utils.cfb_add(output, "EncryptionInfo", encryptionInfoBuffer);
cfb.utils.cfb_add(output, "EncryptedPackage", encryptedPackage);
// Delete the SheetJS entry that is added at initialization
cfb.utils.cfb_del(output, "\u0001Sh33tJ5");
// Write to a buffer and return
output = cfb.write(output);
// The cfb library writes to a Uint8array in the browser. Convert to a Buffer.
if (!Buffer.isBuffer(output)) output = Buffer.from(output);
return output;
}
.option('-a, --append', 'add files to CFB (overwrite existing data)')
.option('-d, --delete', 'delete files from CFB')
.option('--dev', 'development mode')
.option('--read', 'read but do not print out contents');
program.parse(process.argv);
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]);
}
if(!fs.existsSync(program.args[0])) die(1, "must specify a filename");
const opts: X.CFB$ParsingOptions = {type:'file'};
if(program.dev) opts.WTF = true;
const cfb: X.CFB$Container = X.read(program.args[0], opts);
if(program.quiet) exit(0);
if(program.dump) {
console.log("Full Paths:");
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
console.log("File Index:");
console.log(cfb.FileIndex);
.option('-d, --delete', 'delete files from CFB')
.option('--dev', 'development mode')
.option('--read', 'read but do not print out contents');
program.parse(process.argv);
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]);
}
if(!fs.existsSync(program.args[0])) die(1, "must specify a filename");
const opts: X.CFB$ParsingOptions = {type:'file'};
if(program.dev) opts.WTF = true;
const cfb: X.CFB$Container = X.read(program.args[0], opts);
if(program.quiet) exit(0);
if(program.dump) {
console.log("Full Paths:");
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
console.log("File Index:");
console.log(cfb.FileIndex);
exit(0);
if(!fs.existsSync(program.args[0])) die(1, "must specify a filename");
const opts: X.CFB$ParsingOptions = {type:'file'};
if(program.dev) opts.WTF = true;
const cfb: X.CFB$Container = X.read(program.args[0], opts);
if(program.quiet) exit(0);
if(program.dump) {
console.log("Full Paths:");
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
console.log("File Index:");
console.log(cfb.FileIndex);
exit(0);
}
if(program.repair) { X.writeFile(cfb, program.args[0]); exit(0); }
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());
};
if(program.listFiles) {
let basetime = new Date(1980,0,1);
let cnt = 0, rootsize = 0, filesize = 0;
console.log(" Length Date Time Name");
console.log(" -------- ---- ---- ----");
cfb.FileIndex.forEach((file: X.CFB$Entry, i: number) => {
switch(file.type) {
case 5:
basetime = file.ct || file.mt || basetime;
rootsize = file.size;
program.args.slice(1).forEach((x: string) => {
const data: X.CFB$Entry = X.find(cfb, x);
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];
mkdirp(path.slice(0, path.lastIndexOf("/")));
write(path, data);
});
exit(0);
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]);
}
if(!fs.existsSync(program.args[0])) die(1, "must specify a filename");
const opts: X.CFB$ParsingOptions = {type:'file'};
if(program.dev) opts.WTF = true;
const cfb: X.CFB$Container = X.read(program.args[0], opts);
if(program.quiet) exit(0);
if(program.dump) {
console.log("Full Paths:");
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
console.log("File Index:");
console.log(cfb.FileIndex);
exit(0);
}
if(program.repair) { X.writeFile(cfb, program.args[0]); exit(0); }
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());
};