version bump 0.12.0: refresh

- pin dependencies
- flow and typescript improvements
- updated repo infrastructure
This commit is contained in:
SheetJS 2017-07-28 13:53:08 -04:00
parent 16eebaeab4
commit 5a769874f4
33 changed files with 782 additions and 650 deletions

18
.eslintrc Normal file

@ -0,0 +1,18 @@
{
"env": { "shared-node-browser":true },
"globals": {},
"parserOptions": {
"ecmaVersion": 3,
},
"plugins": [ "html", "json" ],
"!extends": "eslint:recommended",
"rules": {
"no-console": 0,
"no-bitwise": 0,
"curly": 0,
"comma-style": [ 2, "last" ],
"no-trailing-spaces": 2,
"semi": [ 2, "always" ],
"comma-dangle": [ 2, "never" ]
}
}

@ -12,6 +12,7 @@ DEPS=$(sort $(wildcard bits/*.js))
TARGET=$(LIB).js
FLOWTARGET=$(LIB).flow.js
FLOWTGTS=$(TARGET) $(AUXTARGETS)
UGLIFYOPTS=--support-ie8
CLOSURE=/usr/local/lib/node_modules/google-closure-compiler/compiler.jar
## Main Targets
@ -47,7 +48,7 @@ init: ## Initial setup for development
dist: dist-deps $(TARGET) ## Prepare JS files for distribution
cp $(TARGET) dist/
cp LICENSE dist/
uglifyjs $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)"
uglifyjs $(UGLIFYOPTS) $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)"
misc/strip_sourcemap.sh dist/$(LIB).min.js
.PHONY: dist-deps
@ -81,7 +82,12 @@ $(TESTFMT): test_%:
## Code Checking
.PHONY: lint
lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks
lint: $(TARGET) $(AUXTARGETS) ## Run eslint checks
@eslint --ext .js,.njs,.json,.html,.htm $(TARGET) $(AUXTARGETS) $(CMDS) $(HTMLLINT) package.json
if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi
.PHONY: old-lint
old-lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks
@jshint --show-non-errors $(TARGET) $(AUXTARGETS)
@jshint --show-non-errors $(CMDS)
@jshint --show-non-errors package.json
@ -89,6 +95,11 @@ lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks
@jscs $(TARGET) $(AUXTARGETS)
if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi
.PHONY: tslint
tslint: $(TARGET) ## Run typescript checks
#@npm install dtslint typescript
@npm run-script dtslint
.PHONY: flow
flow: lint ## Run flow checker
@flow check --all --show-all-errors
@ -96,12 +107,6 @@ flow: lint ## Run flow checker
.PHONY: cov
cov: misc/coverage.html ## Run coverage test
#* To run coverage tests for one format, make cov_<fmt>
COVFMT=$(patsubst %,cov_%,$(FMT))
.PHONY: $(COVFMT)
$(COVFMT): cov_%:
FMTS=$* make cov
misc/coverage.html: $(TARGET) test.js
mocha --require blanket -R html-cov -t 20000 > $@

@ -39,7 +39,7 @@ var workbook = cfb.find('Workbook')
# API
Typescript definitions are maintained in `misc/cfb.d.ts`.
Typescript definitions are maintained in `types/index.d.ts`.
The CFB object exposes the following methods and properties:

@ -25,12 +25,12 @@ if(program.dump) {
console.log("Full Path Directory:");
console.log(cfb.FullPathDir);
}
if(!program.quiet && !program.dump) for(var i=0; i!=cfb.FullPaths.length; ++i) {
if(!program.quiet && !program.dump) for(var i=0; i!==cfb.FullPaths.length; ++i) {
if(cfb.FullPaths[i].slice(-1) === "/") {
console.error("mkdir " + cfb.FullPaths[i]);
fs.mkdirSync(cfb.FullPaths[i]);
} else {
console.error("writing " + cfb.FullPaths[i]);
fs.writeFileSync(cfb.FullPaths[i], cfb.FileIndex[i].content);
fs.writeFileSync(cfb.FullPaths[i], /*::new Buffer((*/cfb.FileIndex[i].content/*:: :any))*/);
}
}

@ -1,10 +1,10 @@
var Base64 = (function(){
var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
return {
decode: function(input) {
decode: function(input/*:string*/)/*:string*/ {
var o = "";
var c1, c2, c3;
var e1, e2, e3, e4;
var c1/*:number*/, c2/*:number*/, c3/*:number*/;
var e1/*:number*/, e2/*:number*/, e3/*:number*/, e4/*:number*/;
input = input.replace(/[^\w\+\/\=]/g, "");
for(var i = 0; i < input.length;) {
e1 = map.indexOf(input.charAt(i++));
@ -27,37 +27,45 @@ var Base64 = (function(){
var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/;
var s2a, _s2a;
s2a = _s2a = function _s2a(s/*:string*/) { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var __toBuffer, ___toBuffer;
__toBuffer = ___toBuffer = function(bufs/*:any*/) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var __utf16le, ___utf16le;
__utf16le = ___utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var __hexlify, ___hexlify;
__hexlify = ___hexlify = function(b,s,l) { return b.slice(s,(s+l)).map(function(x){return (x<16?"0":"") + x.toString(16);}).join(""); };
var bconcat = function(bufs/*:any*/) { return [].concat.apply([], bufs); };
var s2a = function _s2a(s/*:string*/)/*:RawBytes*/ { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var _s2a = s2a;
var __toBuffer = function(bufs/*:Array<Array<RawBytes> >*/)/*:RawBytes*/ { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var ___toBuffer = __toBuffer;
var __utf16le = function(b/*:RawBytes|CFBlob*/,s/*:number*/,e/*:number*/)/*:string*/ { var ss/*:Array<string>*/=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var ___utf16le = __utf16le;
var __hexlify = function(b/*:RawBytes|CFBlob*/,s/*:number*/,l/*:number*/)/*:string*/ { var ss/*:Array<string>*/=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
var ___hexlify = __hexlify;
var __bconcat = function(bufs/*:Array<RawBytes>*/)/*:RawBytes*/ {
if(Array.isArray(bufs[0])/*:: && bufs[0] instanceof Array*/) return /*::(*/[].concat.apply([], bufs)/*:: :any)*/;
var maxlen = 0, i = 0;
for(i = 0; i < bufs.length; ++i) maxlen += bufs[i].length;
var o = new Uint8Array(maxlen);
for(i = 0, maxlen = 0; i < bufs.length; maxlen += bufs[i].length, ++i) o.set(bufs[i], maxlen);
return o;
};
var bconcat = __bconcat;
if(typeof Buffer !== "undefined") {
__utf16le = function(b,s,e) {
if(!Buffer.isBuffer(b)) return ___utf16le(b,s,e);
__utf16le = function(b/*:RawBytes|CFBlob*/,s/*:number*/,e/*:number*/)/*:string*/ {
if(!Buffer.isBuffer(b)/*:: || !(b instanceof Buffer)*/) return ___utf16le(b,s,e);
return b.toString('utf16le',s,e).replace(chr0,'').replace(chr1,'!');
};
__hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
__toBuffer = function(bufs/*:any*/) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
s2a = function(s/*:string*/) { return new Buffer(s, "binary"); };
bconcat = function(bufs/*:any*/) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
__hexlify = function(b/*:RawBytes|CFBlob*/,s/*:number*/,l/*:number*/)/*:string*/ { return Buffer.isBuffer(b)/*:: && b instanceof Buffer*/ ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
__toBuffer = function(bufs/*:Array<Array<RawBytes>>*/)/*:RawBytes*/ { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat((bufs[0]/*:any*/)) : ___toBuffer(bufs);};
s2a = function(s/*:string*/)/*:RawBytes*/ { return new Buffer(s, "binary"); };
bconcat = function(bufs/*:Array<RawBytes>*/)/*:RawBytes*/ { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(/*::(*/bufs/*:: :any)*/) : __bconcat(bufs); };
}
var __readUInt8 = function(b, idx) { return b[idx]; };
var __readUInt16LE = function(b, idx) { return b[idx+1]*(1<<8)+b[idx]; };
var __readInt16LE = function(b, idx) { var u = b[idx+1]*(1<<8)+b[idx]; return (u < 0x8000) ? u : (0xffff - u + 1) * -1; };
var __readUInt32LE = function(b, idx) { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
var __readInt32LE = function(b, idx) { return (b[idx+3]<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
var __readUInt8 = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx]; };
var __readUInt16LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx+1]*(1<<8)+b[idx]; };
var __readInt16LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { var u = b[idx+1]*(1<<8)+b[idx]; return (u < 0x8000) ? u : (0xffff - u + 1) * -1; };
var __readUInt32LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
var __readInt32LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return (b[idx+3]<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
function ReadShift(size/*:number*/, t/*:?any*/) {
var oI, oS, type = 0;
function ReadShift(size/*:number*/, t/*:?string*/)/*:number|string*/ {
var oI/*:: :number = 0*/, oS/*:: :string = ""*/, type = 0;
switch(size) {
case 1: oI = __readUInt8(this, this.l); break;
case 2: oI = (t !== 'i' ? __readUInt16LE : __readInt16LE)(this, this.l); break;
@ -67,15 +75,15 @@ function ReadShift(size/*:number*/, t/*:?any*/) {
this.l+=size; if(type === 0) return oI; return oS;
}
function CheckField(hexstr/*:string*/, fld/*:string*/) {
function CheckField(hexstr/*:string*/, fld/*:string*/)/*:void*/ {
var m = __hexlify(this,this.l,hexstr.length>>1);
if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
this.l += hexstr.length>>1;
}
function prep_blob(blob/*:any*/, pos/*:number*/) {
function prep_blob(blob/*:CFBlob*/, pos/*:number*/)/*:void*/ {
blob.l = pos;
blob.read_shift = ReadShift;
blob.read_shift = /*::(*/ReadShift/*:: :any)*/;
blob.chk = CheckField;
}

@ -1,10 +1,15 @@
/*::
declare var DO_NOT_EXPORT_CFB:any;
type SectorEntry = any;
declare var DO_NOT_EXPORT_CFB:?boolean;
type SectorEntry = {
name?:string;
nodes?:Array<number>;
data:RawBytes;
};
type SectorList = {
(k:string|number):SectorEntry;
[k:string|number]:SectorEntry;
name:?string;
fat_addrs:any;
fat_addrs:Array<number>;
ssz:number;
}
type CFBFiles = {[n:string]:CFBEntry};
*/

@ -1,3 +1,3 @@
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/;

@ -1 +1 @@
exports.version = '0.11.1';
exports.version = '0.12.0';

@ -1,4 +1,4 @@
function parse(file) {
function parse(file/*:RawBytes*/, options/*:CFBReadOpts*/)/*:CFBContainer*/ {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -7,10 +7,10 @@ var dir_start = 0; // first directory sector location
var minifat_start = 0; // first mini FAT sector location
var difat_start = 0; // first mini FAT sector location
var fat_addrs = []; // locations of FAT sectors
var fat_addrs/*:Array<number>*/ = []; // locations of FAT sectors
/* [MS-CFB] 2.2 Compound File Header */
var blob/*:any*/ = file.slice(0,512);
var blob/*:CFBlob*/ = /*::(*/file.slice(0,512)/*:: :any)*/;
prep_blob(blob, 0);
/* major version */
@ -22,14 +22,14 @@ switch(mver) {
}
/* reprocess header */
if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
if(ssz !== 512) { blob = /*::(*/file.slice(0,ssz)/*:: :any)*/; prep_blob(blob, 28 /* blob.l */); }
/* Save header for final object */
var header = file.slice(0,ssz);
var header/*:RawBytes*/ = file.slice(0,ssz);
check_shifts(blob, mver);
// Number of Directory Sectors
var nds = blob.read_shift(4, 'i');
var nds/*:number*/ = blob.read_shift(4, 'i');
if(mver === 3 && nds !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + nds);
// Number of FAT Sectors
@ -58,14 +58,14 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
}
/** Break the file up into sectors */
var sectors = sectorify(file, ssz);
var sectors/*:Array<RawBytes>*/ = sectorify(file, ssz);
sleuth_fat(difat_start, ndfs, sectors, ssz, fat_addrs);
@ -79,13 +79,12 @@ sector_list.fat_addrs = fat_addrs;
sector_list.ssz = ssz;
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
var files = {}, Paths/*:any*/ = [], FileIndex = [], FullPaths = [], FullPathDir = {};
var files/*:CFBFiles*/ = {}, Paths/*:Array<string>*/ = [], FileIndex/*:CFBFileIndex*/ = [], FullPaths/*:Array<string>*/ = [], FullPathDir = {};
read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
var root_name/*:string*/ = Paths.shift();
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);

@ -1,5 +1,5 @@
/* [MS-CFB] 2.2 Compound File Header -- read up to major version */
function check_get_mver(blob) {
function check_get_mver(blob/*:CFBlob*/)/*:[number, number]*/ {
// header signature 8
blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
@ -7,11 +7,11 @@ function check_get_mver(blob) {
blob.chk(HEADER_CLSID, 'CLSID: ');
// minor version 2
var mver = blob.read_shift(2, 'u');
var mver/*:number*/ = blob.read_shift(2, 'u');
return [blob.read_shift(2,'u'), mver];
}
function check_shifts(blob, mver) {
function check_shifts(blob/*:CFBlob*/, mver/*:number*/)/*:void*/ {
var shift = 0x09;
// Byte Order

@ -1,7 +1,7 @@
/** Break the file up into sectors */
function sectorify(file, ssz) {
function sectorify(file/*:RawBytes*/, ssz/*:number*/)/*:Array<RawBytes>*/ {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors/*:Array<RawBytes>*/ = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;

@ -1,7 +1,7 @@
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
function build_full_paths(FI/*:CFBFileIndex*/, FPD/*:CFBFullPathDir*/, FP/*:Array<string>*/, Paths/*:Array<string>*/)/*:void*/ {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad/*:Array<number>*/ = [], q/*:Array<number>*/ = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }

@ -1,15 +1,15 @@
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
function make_find_path(FullPaths/*:Array<string>*/, Paths/*:Array<string>*/, FileIndex/*:CFBFileIndex*/, files/*:CFBFiles*/, root_name/*:string*/)/*:CFBFindPath*/ {
var UCFullPaths/*:Array<string>*/ = [];
var UCPaths/*:Array<string>*/ = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path/*:string*/) {
var k;
return function find_path(path/*:string*/)/*:?CFBEntry*/ {
var k/*:boolean*/ = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
var UCPath/*:string*/ = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
var w/*:number*/ = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
if(w === -1) return null;
return k === true ? FileIndex[w] : files[Paths[w]];
};

@ -1,7 +1,7 @@
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
function sleuth_fat(idx/*:number*/, cnt/*:number*/, sectors/*:Array<RawBytes>*/, ssz/*:number*/, fat_addrs)/*:void*/ {
var q/*:number*/ = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
@ -16,13 +16,11 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
}
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
function get_sector_list(sectors/*:Array<RawBytes>*/, start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/, chkd/*:?Array<boolean>*/)/*:SectorEntry*/ {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf/*:Array<number>*/ = [], buf_chain/*:Array<any>*/ = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
@ -37,26 +35,26 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
}
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz/*:number*/)/*:any*/ {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
function make_sector_list(sectors/*:Array<RawBytes>*/, dir_start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/)/*:SectorList*/ {
var sl = sectors.length, sector_list/*:SectorList*/ = ([]/*:any*/);
var chkd/*:Array<boolean>*/ = [], buf/*:Array<number>*/ = [], buf_chain/*:Array<RawBytes>*/ = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]/*:Array<number>*/);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
var addr/*:number*/ = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/);
}
return sector_list;
}

@ -1,17 +1,16 @@
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
function read_directory(dir_start/*:number*/, sector_list/*:SectorList*/, sectors/*:Array<RawBytes>*/, Paths/*:Array<string>*/, nmfs, files, FileIndex) {
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob/*:CFBlob*/ = /*::(*/sector.slice(i, i+128)/*:: :any)*/;
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o/*:CFBEntry*/ = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -19,16 +18,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
}/*:any*/);
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
var ctime/*:number*/ = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime/*:number*/ = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -39,12 +36,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size)/*:any*/);
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size)/*:any*/);
prep_blob(o.content, 0);
}
}
@ -53,7 +50,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
}
}
function read_date(blob, offset) {
function read_date(blob/*:RawBytes|CFBlob*/, offset/*:number*/)/*:Date*/ {
return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
}

@ -1,15 +1,15 @@
var fs;
function readFileSync(filename/*:string*/, options/*:any*/) {
if(fs === undefined) fs = require('fs');
var fs/*:: = require('fs'); */;
function readFileSync(filename/*:string*/, options/*:CFBReadOpts*/) {
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob/*:any*/, options/*:any*/) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
function readSync(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) {
switch(options && options.type || "base64") {
case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return readFileSync(blob, options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options);
case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options);
}
return parse(blob);
return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options);
}

@ -5,10 +5,10 @@
var Base64 = (function(){
var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
return {
decode: function(input) {
decode: function(input/*:string*/)/*:string*/ {
var o = "";
var c1, c2, c3;
var e1, e2, e3, e4;
var c1/*:number*/, c2/*:number*/, c3/*:number*/;
var e1/*:number*/, e2/*:number*/, e3/*:number*/, e4/*:number*/;
input = input.replace(/[^\w\+\/\=]/g, "");
for(var i = 0; i < input.length;) {
e1 = map.indexOf(input.charAt(i++));
@ -31,37 +31,45 @@ var Base64 = (function(){
var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/;
var s2a, _s2a;
s2a = _s2a = function _s2a(s/*:string*/) { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var __toBuffer, ___toBuffer;
__toBuffer = ___toBuffer = function(bufs/*:any*/) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var __utf16le, ___utf16le;
__utf16le = ___utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var __hexlify, ___hexlify;
__hexlify = ___hexlify = function(b,s,l) { return b.slice(s,(s+l)).map(function(x){return (x<16?"0":"") + x.toString(16);}).join(""); };
var bconcat = function(bufs/*:any*/) { return [].concat.apply([], bufs); };
var s2a = function _s2a(s/*:string*/)/*:RawBytes*/ { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var _s2a = s2a;
var __toBuffer = function(bufs/*:Array<Array<RawBytes> >*/)/*:RawBytes*/ { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var ___toBuffer = __toBuffer;
var __utf16le = function(b/*:RawBytes|CFBlob*/,s/*:number*/,e/*:number*/)/*:string*/ { var ss/*:Array<string>*/=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var ___utf16le = __utf16le;
var __hexlify = function(b/*:RawBytes|CFBlob*/,s/*:number*/,l/*:number*/)/*:string*/ { var ss/*:Array<string>*/=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
var ___hexlify = __hexlify;
var __bconcat = function(bufs/*:Array<RawBytes>*/)/*:RawBytes*/ {
if(Array.isArray(bufs[0])/*:: && bufs[0] instanceof Array*/) return /*::(*/[].concat.apply([], bufs)/*:: :any)*/;
var maxlen = 0, i = 0;
for(i = 0; i < bufs.length; ++i) maxlen += bufs[i].length;
var o = new Uint8Array(maxlen);
for(i = 0, maxlen = 0; i < bufs.length; maxlen += bufs[i].length, ++i) o.set(bufs[i], maxlen);
return o;
};
var bconcat = __bconcat;
if(typeof Buffer !== "undefined") {
__utf16le = function(b,s,e) {
if(!Buffer.isBuffer(b)) return ___utf16le(b,s,e);
__utf16le = function(b/*:RawBytes|CFBlob*/,s/*:number*/,e/*:number*/)/*:string*/ {
if(!Buffer.isBuffer(b)/*:: || !(b instanceof Buffer)*/) return ___utf16le(b,s,e);
return b.toString('utf16le',s,e).replace(chr0,'').replace(chr1,'!');
};
__hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
__toBuffer = function(bufs/*:any*/) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
s2a = function(s/*:string*/) { return new Buffer(s, "binary"); };
bconcat = function(bufs/*:any*/) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
__hexlify = function(b/*:RawBytes|CFBlob*/,s/*:number*/,l/*:number*/)/*:string*/ { return Buffer.isBuffer(b)/*:: && b instanceof Buffer*/ ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
__toBuffer = function(bufs/*:Array<Array<RawBytes>>*/)/*:RawBytes*/ { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat((bufs[0]/*:any*/)) : ___toBuffer(bufs);};
s2a = function(s/*:string*/)/*:RawBytes*/ { return new Buffer(s, "binary"); };
bconcat = function(bufs/*:Array<RawBytes>*/)/*:RawBytes*/ { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(/*::(*/bufs/*:: :any)*/) : __bconcat(bufs); };
}
var __readUInt8 = function(b, idx) { return b[idx]; };
var __readUInt16LE = function(b, idx) { return b[idx+1]*(1<<8)+b[idx]; };
var __readInt16LE = function(b, idx) { var u = b[idx+1]*(1<<8)+b[idx]; return (u < 0x8000) ? u : (0xffff - u + 1) * -1; };
var __readUInt32LE = function(b, idx) { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
var __readInt32LE = function(b, idx) { return (b[idx+3]<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
var __readUInt8 = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx]; };
var __readUInt16LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx+1]*(1<<8)+b[idx]; };
var __readInt16LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { var u = b[idx+1]*(1<<8)+b[idx]; return (u < 0x8000) ? u : (0xffff - u + 1) * -1; };
var __readUInt32LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return b[idx+3]*(1<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
var __readInt32LE = function(b/*:RawBytes|CFBlob*/, idx/*:number*/)/*:number*/ { return (b[idx+3]<<24)+(b[idx+2]<<16)+(b[idx+1]<<8)+b[idx]; };
function ReadShift(size/*:number*/, t/*:?any*/) {
var oI, oS, type = 0;
function ReadShift(size/*:number*/, t/*:?string*/)/*:number|string*/ {
var oI/*:: :number = 0*/, oS/*:: :string = ""*/, type = 0;
switch(size) {
case 1: oI = __readUInt8(this, this.l); break;
case 2: oI = (t !== 'i' ? __readUInt16LE : __readInt16LE)(this, this.l); break;
@ -71,33 +79,38 @@ function ReadShift(size/*:number*/, t/*:?any*/) {
this.l+=size; if(type === 0) return oI; return oS;
}
function CheckField(hexstr/*:string*/, fld/*:string*/) {
function CheckField(hexstr/*:string*/, fld/*:string*/)/*:void*/ {
var m = __hexlify(this,this.l,hexstr.length>>1);
if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
this.l += hexstr.length>>1;
}
function prep_blob(blob/*:any*/, pos/*:number*/) {
function prep_blob(blob/*:CFBlob*/, pos/*:number*/)/*:void*/ {
blob.l = pos;
blob.read_shift = ReadShift;
blob.read_shift = /*::(*/ReadShift/*:: :any)*/;
blob.chk = CheckField;
}
/*::
declare var DO_NOT_EXPORT_CFB:any;
type SectorEntry = any;
declare var DO_NOT_EXPORT_CFB:?boolean;
type SectorEntry = {
name?:string;
nodes?:Array<number>;
data:RawBytes;
};
type SectorList = {
(k:string|number):SectorEntry;
[k:string|number]:SectorEntry;
name:?string;
fat_addrs:any;
fat_addrs:Array<number>;
ssz:number;
}
type CFBFiles = {[n:string]:CFBEntry};
*/
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '0.11.1';
function parse(file) {
var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/;
exports.version = '0.12.0';
function parse(file/*:RawBytes*/, options/*:CFBReadOpts*/)/*:CFBContainer*/ {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -106,10 +119,10 @@ var dir_start = 0; // first directory sector location
var minifat_start = 0; // first mini FAT sector location
var difat_start = 0; // first mini FAT sector location
var fat_addrs = []; // locations of FAT sectors
var fat_addrs/*:Array<number>*/ = []; // locations of FAT sectors
/* [MS-CFB] 2.2 Compound File Header */
var blob/*:any*/ = file.slice(0,512);
var blob/*:CFBlob*/ = /*::(*/file.slice(0,512)/*:: :any)*/;
prep_blob(blob, 0);
/* major version */
@ -121,14 +134,14 @@ switch(mver) {
}
/* reprocess header */
if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
if(ssz !== 512) { blob = /*::(*/file.slice(0,ssz)/*:: :any)*/; prep_blob(blob, 28 /* blob.l */); }
/* Save header for final object */
var header = file.slice(0,ssz);
var header/*:RawBytes*/ = file.slice(0,ssz);
check_shifts(blob, mver);
// Number of Directory Sectors
var nds = blob.read_shift(4, 'i');
var nds/*:number*/ = blob.read_shift(4, 'i');
if(mver === 3 && nds !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + nds);
// Number of FAT Sectors
@ -157,14 +170,14 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
}
/** Break the file up into sectors */
var sectors = sectorify(file, ssz);
var sectors/*:Array<RawBytes>*/ = sectorify(file, ssz);
sleuth_fat(difat_start, ndfs, sectors, ssz, fat_addrs);
@ -178,13 +191,12 @@ sector_list.fat_addrs = fat_addrs;
sector_list.ssz = ssz;
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
var files = {}, Paths/*:any*/ = [], FileIndex = [], FullPaths = [], FullPathDir = {};
var files/*:CFBFiles*/ = {}, Paths/*:Array<string>*/ = [], FileIndex/*:CFBFileIndex*/ = [], FullPaths/*:Array<string>*/ = [], FullPathDir = {};
read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
var root_name/*:string*/ = Paths.shift();
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);
@ -199,7 +211,7 @@ return {
} // parse
/* [MS-CFB] 2.2 Compound File Header -- read up to major version */
function check_get_mver(blob) {
function check_get_mver(blob/*:CFBlob*/)/*:[number, number]*/ {
// header signature 8
blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
@ -207,11 +219,11 @@ function check_get_mver(blob) {
blob.chk(HEADER_CLSID, 'CLSID: ');
// minor version 2
var mver = blob.read_shift(2, 'u');
var mver/*:number*/ = blob.read_shift(2, 'u');
return [blob.read_shift(2,'u'), mver];
}
function check_shifts(blob, mver) {
function check_shifts(blob/*:CFBlob*/, mver/*:number*/)/*:void*/ {
var shift = 0x09;
// Byte Order
@ -233,18 +245,18 @@ function check_shifts(blob, mver) {
}
/** Break the file up into sectors */
function sectorify(file, ssz) {
function sectorify(file/*:RawBytes*/, ssz/*:number*/)/*:Array<RawBytes>*/ {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors/*:Array<RawBytes>*/ = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;
}
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
function build_full_paths(FI/*:CFBFileIndex*/, FPD/*:CFBFullPathDir*/, FP/*:Array<string>*/, Paths/*:Array<string>*/)/*:void*/ {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad/*:Array<number>*/ = [], q/*:Array<number>*/ = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
@ -283,17 +295,17 @@ function build_full_paths(FI, FPD, FP, Paths) {
}
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
function make_find_path(FullPaths/*:Array<string>*/, Paths/*:Array<string>*/, FileIndex/*:CFBFileIndex*/, files/*:CFBFiles*/, root_name/*:string*/)/*:CFBFindPath*/ {
var UCFullPaths/*:Array<string>*/ = [];
var UCPaths/*:Array<string>*/ = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path/*:string*/) {
var k;
return function find_path(path/*:string*/)/*:?CFBEntry*/ {
var k/*:boolean*/ = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
var UCPath/*:string*/ = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
var w/*:number*/ = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
if(w === -1) return null;
return k === true ? FileIndex[w] : files[Paths[w]];
};
@ -301,8 +313,8 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
function sleuth_fat(idx/*:number*/, cnt/*:number*/, sectors/*:Array<RawBytes>*/, ssz/*:number*/, fat_addrs)/*:void*/ {
var q/*:number*/ = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
@ -317,13 +329,11 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
}
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
function get_sector_list(sectors/*:Array<RawBytes>*/, start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/, chkd/*:?Array<boolean>*/)/*:SectorEntry*/ {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf/*:Array<number>*/ = [], buf_chain/*:Array<any>*/ = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
@ -338,44 +348,43 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
}
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz/*:number*/)/*:any*/ {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
function make_sector_list(sectors/*:Array<RawBytes>*/, dir_start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/)/*:SectorList*/ {
var sl = sectors.length, sector_list/*:SectorList*/ = ([]/*:any*/);
var chkd/*:Array<boolean>*/ = [], buf/*:Array<number>*/ = [], buf_chain/*:Array<RawBytes>*/ = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]/*:Array<number>*/);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
var addr/*:number*/ = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/);
}
return sector_list;
}
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
function read_directory(dir_start/*:number*/, sector_list/*:SectorList*/, sectors/*:Array<RawBytes>*/, Paths/*:Array<string>*/, nmfs, files, FileIndex) {
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob/*:CFBlob*/ = /*::(*/sector.slice(i, i+128)/*:: :any)*/;
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o/*:CFBEntry*/ = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -383,16 +392,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
}/*:any*/);
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
var ctime/*:number*/ = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime/*:number*/ = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -403,12 +410,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size)/*:any*/);
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size)/*:any*/);
prep_blob(o.content, 0);
}
}
@ -417,23 +424,23 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
}
}
function read_date(blob, offset) {
function read_date(blob/*:RawBytes|CFBlob*/, offset/*:number*/)/*:Date*/ {
return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
}
var fs;
function readFileSync(filename/*:string*/, options/*:any*/) {
if(fs === undefined) fs = require('fs');
var fs/*:: = require('fs'); */;
function readFileSync(filename/*:string*/, options/*:CFBReadOpts*/) {
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob/*:any*/, options/*:any*/) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
function readSync(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) {
switch(options && options.type || "base64") {
case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return readFileSync(blob, options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options);
case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options);
}
return parse(blob);
return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options);
}
/** CFB Constants */

102
cfb.js

@ -31,15 +31,23 @@ var Base64 = (function(){
var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/;
var s2a, _s2a;
s2a = _s2a = function _s2a(s) { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var __toBuffer, ___toBuffer;
__toBuffer = ___toBuffer = function(bufs) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var __utf16le, ___utf16le;
__utf16le = ___utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var __hexlify, ___hexlify;
__hexlify = ___hexlify = function(b,s,l) { return b.slice(s,(s+l)).map(function(x){return (x<16?"0":"") + x.toString(16);}).join(""); };
var bconcat = function(bufs) { return [].concat.apply([], bufs); };
var s2a = function _s2a(s) { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var _s2a = s2a;
var __toBuffer = function(bufs) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var ___toBuffer = __toBuffer;
var __utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var ___utf16le = __utf16le;
var __hexlify = function(b,s,l) { var ss=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
var ___hexlify = __hexlify;
var __bconcat = function(bufs) {
if(Array.isArray(bufs[0])) return [].concat.apply([], bufs);
var maxlen = 0, i = 0;
for(i = 0; i < bufs.length; ++i) maxlen += bufs[i].length;
var o = new Uint8Array(maxlen);
for(i = 0, maxlen = 0; i < bufs.length; maxlen += bufs[i].length, ++i) o.set(bufs[i], maxlen);
return o;
};
var bconcat = __bconcat;
if(typeof Buffer !== "undefined") {
@ -48,9 +56,9 @@ if(typeof Buffer !== "undefined") {
return b.toString('utf16le',s,e).replace(chr0,'').replace(chr1,'!');
};
__hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
__toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
__toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat((bufs[0])) : ___toBuffer(bufs);};
s2a = function(s) { return new Buffer(s, "binary"); };
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : __bconcat(bufs); };
}
@ -86,8 +94,8 @@ function prep_blob(blob, pos) {
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '0.11.1';
function parse(file) {
exports.version = '0.12.0';
function parse(file, options) {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -147,7 +155,7 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
@ -174,7 +182,6 @@ read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);
@ -225,7 +232,7 @@ function check_shifts(blob, mver) {
/** Break the file up into sectors */
function sectorify(file, ssz) {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;
@ -234,7 +241,7 @@ function sectorify(file, ssz) {
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad = [], q = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
@ -274,12 +281,12 @@ function build_full_paths(FI, FPD, FP, Paths) {
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
var UCFullPaths = [];
var UCPaths = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path) {
var k;
var k = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
@ -292,7 +299,7 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
var q = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
@ -309,11 +316,9 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf = [], buf_chain = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
@ -329,13 +334,13 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
var sl = sectors.length, sector_list = ([]);
var chkd = [], buf = [], buf_chain = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
@ -347,25 +352,24 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
}
return sector_list;
}
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob = sector.slice(i, i+128);
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -373,16 +377,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -393,12 +395,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size));
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size));
prep_blob(o.content, 0);
}
}
@ -413,17 +415,17 @@ function read_date(blob, offset) {
var fs;
function readFileSync(filename, options) {
if(fs === undefined) fs = require('fs');
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob, options) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
switch(options && options.type || "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
}
return parse(blob);
return parse(blob, options);
}
/** CFB Constants */

123
dist/cfb.js vendored

@ -31,15 +31,23 @@ var Base64 = (function(){
var chr0 = /\u0000/g, chr1 = /[\u0001-\u0006]/;
var s2a, _s2a;
s2a = _s2a = function _s2a(s) { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var __toBuffer, ___toBuffer;
__toBuffer = ___toBuffer = function(bufs) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var __utf16le, ___utf16le;
__utf16le = ___utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var __hexlify, ___hexlify;
__hexlify = ___hexlify = function(b,s,l) { return b.slice(s,(s+l)).map(function(x){return (x<16?"0":"") + x.toString(16);}).join(""); };
var bconcat = function(bufs) { return [].concat.apply([], bufs); };
var s2a = function _s2a(s) { return s.split("").map(function(x){ return x.charCodeAt(0) & 0xff; }); };
var _s2a = s2a;
var __toBuffer = function(bufs) { var x = []; for(var i = 0; i < bufs[0].length; ++i) { x.push.apply(x, bufs[0][i]); } return x; };
var ___toBuffer = __toBuffer;
var __utf16le = function(b,s,e) { var ss=[]; for(var i=s; i<e; i+=2) ss.push(String.fromCharCode(__readUInt16LE(b,i))); return ss.join("").replace(chr0,'').replace(chr1,'!'); };
var ___utf16le = __utf16le;
var __hexlify = function(b,s,l) { var ss=[]; for(var i=s; i<s+l; ++i) ss.push(("0" + b[i].toString(16)).slice(-2)); return ss.join(""); };
var ___hexlify = __hexlify;
var __bconcat = function(bufs) {
if(Array.isArray(bufs[0])) return [].concat.apply([], bufs);
var maxlen = 0, i = 0;
for(i = 0; i < bufs.length; ++i) maxlen += bufs[i].length;
var o = new Uint8Array(maxlen);
for(i = 0, maxlen = 0; i < bufs.length; maxlen += bufs[i].length, ++i) o.set(bufs[i], maxlen);
return o;
};
var bconcat = __bconcat;
if(typeof Buffer !== "undefined") {
@ -48,9 +56,9 @@ if(typeof Buffer !== "undefined") {
return b.toString('utf16le',s,e).replace(chr0,'').replace(chr1,'!');
};
__hexlify = function(b,s,l) { return Buffer.isBuffer(b) ? b.toString('hex',s,s+l) : ___hexlify(b,s,l); };
__toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat(bufs[0]) : ___toBuffer(bufs);};
__toBuffer = function(bufs) { return (bufs[0].length > 0 && Buffer.isBuffer(bufs[0][0])) ? Buffer.concat((bufs[0])) : ___toBuffer(bufs);};
s2a = function(s) { return new Buffer(s, "binary"); };
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : __bconcat(bufs); };
}
@ -73,7 +81,7 @@ function ReadShift(size, t) {
function CheckField(hexstr, fld) {
var m = __hexlify(this,this.l,hexstr.length>>1);
if(m !== hexstr) throw fld + 'Expected ' + hexstr + ' saw ' + m;
if(m !== hexstr) throw new Error(fld + 'Expected ' + hexstr + ' saw ' + m);
this.l += hexstr.length>>1;
}
@ -86,8 +94,8 @@ function prep_blob(blob, pos) {
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '0.11.0';
function parse(file) {
exports.version = '0.12.0';
function parse(file, options) {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -107,7 +115,7 @@ var mv = check_get_mver(blob);
mver = mv[0];
switch(mver) {
case 3: ssz = 512; break; case 4: ssz = 4096; break;
default: throw "Major Version: Expected 3 or 4 saw " + mver;
default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
}
/* reprocess header */
@ -119,7 +127,7 @@ check_shifts(blob, mver);
// Number of Directory Sectors
var nds = blob.read_shift(4, 'i');
if(mver === 3 && nds !== 0) throw '# Directory Sectors: Expected 0 saw ' + nds;
if(mver === 3 && nds !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + nds);
// Number of FAT Sectors
//var nfs = blob.read_shift(4, 'i');
@ -147,7 +155,7 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
@ -174,7 +182,6 @@ read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);
@ -205,13 +212,14 @@ function check_shifts(blob, mver) {
var shift = 0x09;
// Byte Order
blob.chk('feff', 'Byte Order: ');
//blob.chk('feff', 'Byte Order: '); // note: some writers put 0xffff
blob.l += 2;
// Sector Shift
switch((shift = blob.read_shift(2))) {
case 0x09: if(mver !== 3) throw 'MajorVersion/SectorShift Mismatch'; break;
case 0x0c: if(mver !== 4) throw 'MajorVersion/SectorShift Mismatch'; break;
default: throw 'Sector Shift: Expected 9 or 12 saw ' + shift;
case 0x09: if(mver != 3) throw new Error('Sector Shift: Expected 9 saw ' + shift); break;
case 0x0c: if(mver != 4) throw new Error('Sector Shift: Expected 12 saw ' + shift); break;
default: throw new Error('Sector Shift: Expected 9 or 12 saw ' + shift);
}
// Mini Sector Shift
@ -224,7 +232,7 @@ function check_shifts(blob, mver) {
/** Break the file up into sectors */
function sectorify(file, ssz) {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;
@ -233,7 +241,7 @@ function sectorify(file, ssz) {
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad = [], q = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
@ -273,12 +281,12 @@ function build_full_paths(FI, FPD, FP, Paths) {
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
var UCFullPaths = [];
var UCPaths = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path) {
var k;
var k = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
@ -291,9 +299,9 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
var q = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw "DIFAT chain shorter than expected";
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
var sector = sectors[idx], m = (ssz>>>2)-1;
if(!sector) return;
@ -308,18 +316,16 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf = [], buf_chain = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw "FAT boundary crossed: " + j + " 4 "+ssz;
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
@ -328,13 +334,13 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
var sl = sectors.length, sector_list = ([]);
var chkd = [], buf = [], buf_chain = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
@ -342,29 +348,28 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw "FAT boundary crossed: " + j + " 4 "+ssz;
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
}
return sector_list;
}
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob = sector.slice(i, i+128);
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -372,16 +377,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -392,12 +395,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size));
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size));
prep_blob(o.content, 0);
}
}
@ -412,17 +415,17 @@ function read_date(blob, offset) {
var fs;
function readFileSync(filename, options) {
if(fs === undefined) fs = require('fs');
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob, options) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
switch(options && options.type || "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
}
return parse(blob);
return parse(blob, options);
}
/** CFB Constants */

2
dist/cfb.min.js vendored

File diff suppressed because one or more lines are too long

2
dist/cfb.min.map vendored

File diff suppressed because one or more lines are too long

91
dist/xlscfb.js vendored

@ -6,8 +6,8 @@ var DO_NOT_EXPORT_CFB = true;
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '0.11.0';
function parse(file) {
exports.version = '0.12.0';
function parse(file, options) {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -27,7 +27,7 @@ var mv = check_get_mver(blob);
mver = mv[0];
switch(mver) {
case 3: ssz = 512; break; case 4: ssz = 4096; break;
default: throw "Major Version: Expected 3 or 4 saw " + mver;
default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
}
/* reprocess header */
@ -39,7 +39,7 @@ check_shifts(blob, mver);
// Number of Directory Sectors
var nds = blob.read_shift(4, 'i');
if(mver === 3 && nds !== 0) throw '# Directory Sectors: Expected 0 saw ' + nds;
if(mver === 3 && nds !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + nds);
// Number of FAT Sectors
//var nfs = blob.read_shift(4, 'i');
@ -67,7 +67,7 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
@ -94,7 +94,6 @@ read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);
@ -125,13 +124,14 @@ function check_shifts(blob, mver) {
var shift = 0x09;
// Byte Order
blob.chk('feff', 'Byte Order: ');
//blob.chk('feff', 'Byte Order: '); // note: some writers put 0xffff
blob.l += 2;
// Sector Shift
switch((shift = blob.read_shift(2))) {
case 0x09: if(mver !== 3) throw 'MajorVersion/SectorShift Mismatch'; break;
case 0x0c: if(mver !== 4) throw 'MajorVersion/SectorShift Mismatch'; break;
default: throw 'Sector Shift: Expected 9 or 12 saw ' + shift;
case 0x09: if(mver != 3) throw new Error('Sector Shift: Expected 9 saw ' + shift); break;
case 0x0c: if(mver != 4) throw new Error('Sector Shift: Expected 12 saw ' + shift); break;
default: throw new Error('Sector Shift: Expected 9 or 12 saw ' + shift);
}
// Mini Sector Shift
@ -144,7 +144,7 @@ function check_shifts(blob, mver) {
/** Break the file up into sectors */
function sectorify(file, ssz) {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;
@ -153,7 +153,7 @@ function sectorify(file, ssz) {
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad = [], q = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
@ -193,12 +193,12 @@ function build_full_paths(FI, FPD, FP, Paths) {
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
var UCFullPaths = [];
var UCPaths = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path) {
var k;
var k = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
@ -211,9 +211,9 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
var q = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw "DIFAT chain shorter than expected";
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
var sector = sectors[idx], m = (ssz>>>2)-1;
if(!sector) return;
@ -228,18 +228,16 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf = [], buf_chain = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw "FAT boundary crossed: " + j + " 4 "+ssz;
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
@ -248,13 +246,13 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
var sl = sectors.length, sector_list = ([]);
var chkd = [], buf = [], buf_chain = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
@ -262,29 +260,28 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw "FAT boundary crossed: " + j + " 4 "+ssz;
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
}
return sector_list;
}
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob = sector.slice(i, i+128);
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -292,16 +289,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -312,12 +307,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size));
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size));
prep_blob(o.content, 0);
}
}
@ -332,17 +327,17 @@ function read_date(blob, offset) {
var fs;
function readFileSync(filename, options) {
if(fs === undefined) fs = require('fs');
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob, options) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
switch(options && options.type || "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
}
return parse(blob);
return parse(blob, options);
}
/** CFB Constants */

104
misc/cfb.d.ts vendored

@ -1,104 +0,0 @@
declare enum CFBEntryType { unknown, storage, stream, lockbytes, property, root }
declare enum CFBStorageType { fat, minifat }
/* CFB Entry Object demanded by write functions */
interface CFBEntryMin {
/* Raw Content (Buffer when available, Array of bytes otherwise) */
content:any;
}
/* CFB Entry Object returned by parse functions */
interface CFBEntry extends CFBEntryMin {
/* Case-sensitive internal name */
name:string;
/* CFB type (salient types: stream, storage) -- see CFBEntryType */
type:string;
/* Creation Time */
ct:Date;
/* Modification Time */
mt:Date;
/* Raw creation time -- see [MS-DTYP] 2.3.3 FILETIME */
mtime:string;
/* Raw modification time -- see [MS-DTYP] 2.3.3 FILETIME */
ctime:string;
/* RBT color: 0 = red, 1 = black */
color:number;
/* Class ID represented as hex string */
clsid:string;
/* User-Defined State Bits */
state:number;
/* Starting Sector */
start:number;
/* Data Size */
size:number;
/* Storage location -- see CFBStorageType */
storage:string;
}
/* cfb.FullPathDir as demanded by write functions */
interface CFBDirectoryMin {
/* keys are unix-style paths */
[key:string]: CFBEntryMin;
}
/* cfb.FullPathDir Directory object */
interface CFBDirectory extends CFBDirectoryMin {
/* cfb.FullPathDir keys are paths; cfb.Directory keys are file names */
[key:string]: CFBEntry;
}
/* cfb object demanded by write functions */
interface CFBContainerMin {
/* Path -> CFB object mapping */
FullPathDir:CFBDirectoryMin;
}
/* cfb object returned by read and parse functions */
interface CFBContainer extends CFBContainerMin {
/* search by path or file name */
find(string):CFBEntry;
/* list of streams and storages */
FullPaths:string[];
/* Path -> CFB object mapping */
FullPathDir:CFBDirectory;
/* Array of entries in the same order as FullPaths */
FileIndex:CFBEntry[];
/* Raw Content, in chunks (Buffer when available, Array of bytes otherwise) */
raw:any[];
}
interface CFB {
read(f:any, options:any):CFBContainer;
parse(f:any):CFBContainer;
utils: {
ReadShift(size:any,t?:any):any;
WarnField(hexstr:string,fld?:string);
CheckField(hexstr:string,fld?:string);
prep_blob(blob:any, pos?:number):any;
bconcat(bufs:any[]):any;
};
main;
}

@ -1,35 +0,0 @@
///<reference path='node.d.ts'/>
///<reference path='cfb.d.ts'/>
/* vim: set ts=2: */
var CFB = <CFB>require('../cfb');
var fs = require('fs'), program = require('commander');
program
.version('0.8.1')
.usage('[options] <file>')
.option('-q, --quiet', 'print but do not extract')
.parse(process.argv);
if(program.args.length === 0 || !fs.existsSync(program.args[0])) {
console.error("Usage: " + process.argv[1] + " [-q] <cfb_file>");
process.exit(1);
}
var cfb = CFB.read(program.args[0], {type:'file'});
if(program.quiet) {
console.log("Full Paths:")
console.log(cfb.FullPaths.map(function(x) { return " " + x; }).join("\n"));
console.log("Full Path Directory:")
console.log(cfb.FullPathDir);
return;
}
for(var i=0; i != cfb.FullPaths.length; ++i) {
if(cfb.FullPaths[i].slice(-1) === "/") {
console.error("mkdir " + cfb.FullPaths[i]);
fs.mkdirSync(cfb.FullPaths[i]);
} else {
console.error("writing " + cfb.FullPaths[i]);
fs.writeFileSync(cfb.FullPaths[i], cfb.FileIndex[i].content);
}
}

@ -1,5 +1,89 @@
/*::
type CFBModule = any;
type CFBModule = {
version:string;
read:(blob:RawBytes|string, opts:CFBReadOpts)=>CFBContainer;
parse:(file:RawBytes, opts:CFBReadOpts)=>CFBContainer;
utils:CFBUtils;
};
type CFBFullPathDir = {
[n:string]: CFBEntry;
}
type CFBUtils = any;
type ReadShiftFunc = {
//(size:number, t:?string):number|string;
(size:16):string;
(size:1|2|4, t:?string):number;
};
type CheckFieldFunc = {(hexstr:string, fld:string):void;};
type RawBytes = Array<number> | Buffer | Uint8Array;
class CFBlobArray extends Array<number> {
l:number;
read_shift:ReadShiftFunc;
chk:CheckFieldFunc;
};
interface CFBlobBuffer extends Buffer {
l:number;
slice:(start:number, end:?number)=>Buffer;
read_shift:ReadShiftFunc;
chk:CheckFieldFunc;
};
interface CFBlobUint8 extends Uint8Array {
l:number;
slice:(start:number, end:?number)=>Uint8Array;
read_shift:ReadShiftFunc;
chk:CheckFieldFunc;
};
interface CFBlobber {
[n:number]:number;
l:number;
length:number;
slice:(start:number, end:?number)=>RawBytes;
read_shift:ReadShiftFunc;
chk:CheckFieldFunc;
};
type CFBlob = CFBlobArray | CFBlobBuffer | CFBlobUint8;
interface CFBReadOpts {
type:?string;
};
type CFBFileIndex = Array<CFBEntry>;
type CFBFindPath = (n:string)=>?CFBEntry;
type CFBContainer = {
raw:{
header:any;
sectors:Array<any>;
};
FileIndex:CFBFileIndex;
FullPathDir:CFBDirectory;
FullPaths:Array<string>;
find:CFBFindPath;
}
type CFBEntry = {
name: string;
type: number;
ct?: Date;
mt?: Date;
color: number;
clsid: string;
state: number;
start: number;
size: number;
storage?: "fat" | "minifat";
L: number;
R: number;
C: number;
content?: CFBlob;
}
*/

8
misc/node.d.ts vendored

@ -1,8 +0,0 @@
declare var require: {
(id: string): any;
}
declare var process: {
argv: string[];
exit(status: number): void;
}

@ -1,6 +1,6 @@
{
"name": "cfb",
"version": "0.11.1",
"version": "0.12.0",
"author": "sheetjs",
"description": "Compound File Binary File Format extractor",
"keywords": [ "cfb", "compression", "office" ],
@ -8,32 +8,37 @@
"cfb": "./bin/cfb.njs"
},
"main": "./cfb",
"files": [
"LICENSE",
"README.md",
"bin/",
"dist/",
"cfb.js",
"xlscfb.flow.js"
],
"types": "types",
"browser": {
"node": false,
"process": false,
"fs": false
},
"dependencies": {
"commander":""
"commander":"~2.11.0"
},
"devDependencies": {
"mocha":"",
"xlsjs":"",
"uglify-js":""
"mocha":"~2.5.3",
"blanket": "~1.2.3",
"@sheetjs/uglify-js":"~2.7.3",
"@types/node":"^8.0.7",
"@types/commander":"^2.9.0",
"dtslint": "^0.1.2",
"typescript": "2.2.0"
},
"repository": { "type":"git", "url":"git://github.com/SheetJS/js-cfb.git" },
"scripts": {
"pretest": "make init",
"test": "make test"
"test": "make test",
"dtslint": "dtslint types"
},
"config": {
"blanket": {
"pattern": "cfb.js"
}
},
"files": [ "LICENSE", "README.md", "bin/", "dist/", "types/index.d.ts", "types/tsconfig.json", "cfb.js", "xlscfb.flow.js" ],
"homepage": "http://sheetjs.com/opensource",
"bugs": { "url": "https://github.com/SheetJS/js-cfb/issues" },
"license": "Apache-2.0",
"engines": { "node": ">=0.8" }

35
types/bin_cfb.ts Executable file

@ -0,0 +1,35 @@
/* vim: set ts=2: */
import * as CFB from 'cfb';
import fs = require('fs');
import program = require('commander');
program
.version(CFB.version)
.usage('[options] <file>')
.option('-q, --quiet', 'process but do not report')
.option('-d, --dump', 'dump internal representation but do not extract')
.parse(process.argv);
if(program.args.length === 0 || !fs.existsSync(program.args[0])) {
console.error("Usage: " + process.argv[1] + " [-q] <cfb_file>");
process.exit(1);
}
const opts: CFB.CFBParsingOptions = {type:'file'};
const cfb: CFB.CFBContainer = CFB.read(program.args[0], opts);
if(program.dump) {
console.log("Full Paths:");
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
console.log("Full Path Directory:");
console.log(cfb.FullPathDir);
}
if(!program.quiet && !program.dump) for(let i=0; i!==cfb.FullPaths.length; ++i) {
if(cfb.FullPaths[i].slice(-1) === "/") {
console.error("mkdir " + cfb.FullPaths[i]);
fs.mkdirSync(cfb.FullPaths[i]);
} else {
console.error("writing " + cfb.FullPaths[i]);
fs.writeFileSync(cfb.FullPaths[i], cfb.FileIndex[i].content);
}
}

100
types/index.d.ts vendored Normal file

@ -0,0 +1,100 @@
/* index.d.ts (C) 2013-present SheetJS */
// TypeScript Version: 2.2
/** Version string */
export const version: string;
/** Parse a buffer or array */
export function parse(f: CFB$Blob, options?: CFBParsingOptions): CFBContainer;
/** Read a blob or file or binary string */
export function read(f: CFB$Blob | string, options?: CFBParsingOptions): CFBContainer;
/** Utility functions */
export const utils: CFB$Utils;
/** Options for read and readFile */
export interface CFBParsingOptions {
/** Input data encoding */
type?: 'base64' | 'binary' | 'buffer' | 'file' | 'array';
}
export type CFB$Blob = Buffer | number[] | Uint8Array;
export enum CFBEntryType { unknown, storage, stream, lockbytes, property, root }
export enum CFBStorageType { fat, minifat }
/** CFB File Entry Object */
export interface CFBEntry {
/** Case-sensitive internal name */
name: string;
/** CFB type (salient types: stream, storage) -- see CFBEntryType */
type: number;
/** Raw Content (Buffer when available, Array of bytes otherwise) */
content: CFB$Blob;
/** Creation Time */
ct?: Date;
/** Modification Time */
mt?: Date;
/** Red/Black Tree color: 0 = red, 1 = black */
color: number;
/** Class ID represented as hex string */
clsid: string;
/** User-Defined State Bits */
state: number;
/** Starting Sector */
start: number;
/** Data Size */
size: number;
/** Storage location -- see CFBStorageType */
storage?: string;
}
/* cfb.FullPathDir Directory object */
export interface CFBDirectory {
/* cfb.FullPathDir keys are paths; cfb.Directory keys are file names */
[key: string]: CFBEntry;
}
/* File object */
export interface CFBContainer {
/* search by path or file name */
find(name: string): CFBEntry;
/* list of streams and storages */
FullPaths: string[];
/* Path -> CFB object mapping */
FullPathDir: CFBDirectory;
/* Array of entries in the same order as FullPaths */
FileIndex: CFBEntry[];
/* Raw Content, in chunks (Buffer when available, Array of bytes otherwise) */
raw: {
header: CFB$Blob,
sectors: CFB$Blob[];
};
}
/** General utilities */
export interface CFB$Utils {
ReadShift(size: number, t?: string): number|string;
WarnField(hexstr: string, fld?: string): void;
CheckField(hexstr: string, fld?: string): void;
prep_blob(blob: any, pos?: number): CFB$Blob;
bconcat(bufs: any[]): any;
}

14
types/tsconfig.json Normal file

@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [ "es5" ],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"baseUrl": ".",
"paths": { "cfb": ["."] },
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
}
}

11
types/tslint.json Normal file

@ -0,0 +1,11 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"no-sparse-arrays": false,
"only-arrow-functions": false,
"no-consecutive-blank-lines": false,
"prefer-conditional-expression": false,
"one-variable-per-declaration": false
}
}

@ -18,20 +18,25 @@ declare var chr1:any;
/*jshint eqnull:true */
/*::
declare var DO_NOT_EXPORT_CFB:any;
type SectorEntry = any;
declare var DO_NOT_EXPORT_CFB:?boolean;
type SectorEntry = {
name?:string;
nodes?:Array<number>;
data:RawBytes;
};
type SectorList = {
(k:string|number):SectorEntry;
[k:string|number]:SectorEntry;
name:?string;
fat_addrs:any;
fat_addrs:Array<number>;
ssz:number;
}
type CFBFiles = {[n:string]:CFBEntry};
*/
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '0.11.1';
function parse(file) {
var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/;
exports.version = '0.12.0';
function parse(file/*:RawBytes*/, options/*:CFBReadOpts*/)/*:CFBContainer*/ {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -40,10 +45,10 @@ var dir_start = 0; // first directory sector location
var minifat_start = 0; // first mini FAT sector location
var difat_start = 0; // first mini FAT sector location
var fat_addrs = []; // locations of FAT sectors
var fat_addrs/*:Array<number>*/ = []; // locations of FAT sectors
/* [MS-CFB] 2.2 Compound File Header */
var blob/*:any*/ = file.slice(0,512);
var blob/*:CFBlob*/ = /*::(*/file.slice(0,512)/*:: :any)*/;
prep_blob(blob, 0);
/* major version */
@ -55,14 +60,14 @@ switch(mver) {
}
/* reprocess header */
if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
if(ssz !== 512) { blob = /*::(*/file.slice(0,ssz)/*:: :any)*/; prep_blob(blob, 28 /* blob.l */); }
/* Save header for final object */
var header = file.slice(0,ssz);
var header/*:RawBytes*/ = file.slice(0,ssz);
check_shifts(blob, mver);
// Number of Directory Sectors
var nds = blob.read_shift(4, 'i');
var nds/*:number*/ = blob.read_shift(4, 'i');
if(mver === 3 && nds !== 0) throw new Error('# Directory Sectors: Expected 0 saw ' + nds);
// Number of FAT Sectors
@ -91,14 +96,14 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
}
/** Break the file up into sectors */
var sectors = sectorify(file, ssz);
var sectors/*:Array<RawBytes>*/ = sectorify(file, ssz);
sleuth_fat(difat_start, ndfs, sectors, ssz, fat_addrs);
@ -112,13 +117,12 @@ sector_list.fat_addrs = fat_addrs;
sector_list.ssz = ssz;
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
var files = {}, Paths/*:any*/ = [], FileIndex = [], FullPaths = [], FullPathDir = {};
var files/*:CFBFiles*/ = {}, Paths/*:Array<string>*/ = [], FileIndex/*:CFBFileIndex*/ = [], FullPaths/*:Array<string>*/ = [], FullPathDir = {};
read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
var root_name/*:string*/ = Paths.shift();
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);
@ -133,7 +137,7 @@ return {
} // parse
/* [MS-CFB] 2.2 Compound File Header -- read up to major version */
function check_get_mver(blob) {
function check_get_mver(blob/*:CFBlob*/)/*:[number, number]*/ {
// header signature 8
blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
@ -141,11 +145,11 @@ function check_get_mver(blob) {
blob.chk(HEADER_CLSID, 'CLSID: ');
// minor version 2
var mver = blob.read_shift(2, 'u');
var mver/*:number*/ = blob.read_shift(2, 'u');
return [blob.read_shift(2,'u'), mver];
}
function check_shifts(blob, mver) {
function check_shifts(blob/*:CFBlob*/, mver/*:number*/)/*:void*/ {
var shift = 0x09;
// Byte Order
@ -167,18 +171,18 @@ function check_shifts(blob, mver) {
}
/** Break the file up into sectors */
function sectorify(file, ssz) {
function sectorify(file/*:RawBytes*/, ssz/*:number*/)/*:Array<RawBytes>*/ {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors/*:Array<RawBytes>*/ = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;
}
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
function build_full_paths(FI/*:CFBFileIndex*/, FPD/*:CFBFullPathDir*/, FP/*:Array<string>*/, Paths/*:Array<string>*/)/*:void*/ {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad/*:Array<number>*/ = [], q/*:Array<number>*/ = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
@ -217,17 +221,17 @@ function build_full_paths(FI, FPD, FP, Paths) {
}
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
function make_find_path(FullPaths/*:Array<string>*/, Paths/*:Array<string>*/, FileIndex/*:CFBFileIndex*/, files/*:CFBFiles*/, root_name/*:string*/)/*:CFBFindPath*/ {
var UCFullPaths/*:Array<string>*/ = [];
var UCPaths/*:Array<string>*/ = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path/*:string*/) {
var k;
return function find_path(path/*:string*/)/*:?CFBEntry*/ {
var k/*:boolean*/ = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
var w = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
var UCPath/*:string*/ = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
var w/*:number*/ = k === true ? UCFullPaths.indexOf(UCPath) : UCPaths.indexOf(UCPath);
if(w === -1) return null;
return k === true ? FileIndex[w] : files[Paths[w]];
};
@ -235,8 +239,8 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
function sleuth_fat(idx/*:number*/, cnt/*:number*/, sectors/*:Array<RawBytes>*/, ssz/*:number*/, fat_addrs)/*:void*/ {
var q/*:number*/ = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
@ -251,13 +255,11 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
}
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
function get_sector_list(sectors/*:Array<RawBytes>*/, start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/, chkd/*:?Array<boolean>*/)/*:SectorEntry*/ {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf/*:Array<number>*/ = [], buf_chain/*:Array<any>*/ = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
@ -272,44 +274,43 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
}
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz/*:number*/)/*:any*/ {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
function make_sector_list(sectors/*:Array<RawBytes>*/, dir_start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/)/*:SectorList*/ {
var sl = sectors.length, sector_list/*:SectorList*/ = ([]/*:any*/);
var chkd/*:Array<boolean>*/ = [], buf/*:Array<number>*/ = [], buf_chain/*:Array<RawBytes>*/ = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]/*:Array<number>*/);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
buf_chain.push(sectors[j]);
var addr = fat_addrs[Math.floor(j*4/ssz)];
var addr/*:number*/ = fat_addrs[Math.floor(j*4/ssz)];
jj = ((j*4) & modulus);
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/);
}
return sector_list;
}
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
function read_directory(dir_start/*:number*/, sector_list/*:SectorList*/, sectors/*:Array<RawBytes>*/, Paths/*:Array<string>*/, nmfs, files, FileIndex) {
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob/*:CFBlob*/ = /*::(*/sector.slice(i, i+128)/*:: :any)*/;
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o/*:CFBEntry*/ = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -317,16 +318,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
}/*:any*/);
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
var ctime/*:number*/ = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime/*:number*/ = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -337,12 +336,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size)/*:any*/);
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size)/*:any*/);
prep_blob(o.content, 0);
}
}
@ -351,23 +350,23 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
}
}
function read_date(blob, offset) {
function read_date(blob/*:RawBytes|CFBlob*/, offset/*:number*/)/*:Date*/ {
return new Date(( ( (__readUInt32LE(blob,offset+4)/1e7)*Math.pow(2,32)+__readUInt32LE(blob,offset)/1e7 ) - 11644473600)*1000);
}
var fs;
function readFileSync(filename/*:string*/, options/*:any*/) {
if(fs === undefined) fs = require('fs');
var fs/*:: = require('fs'); */;
function readFileSync(filename/*:string*/, options/*:CFBReadOpts*/) {
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob/*:any*/, options/*:any*/) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
function readSync(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) {
switch(options && options.type || "base64") {
case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return readFileSync(blob, options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options);
case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options);
}
return parse(blob);
return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options);
}
/** CFB Constants */

@ -6,8 +6,8 @@ var DO_NOT_EXPORT_CFB = true;
/* [MS-CFB] v20130118 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '0.11.1';
function parse(file) {
exports.version = '0.12.0';
function parse(file, options) {
var mver = 3; // major version
var ssz = 512; // sector size
var nmfs = 0; // number of mini FAT sectors
@ -67,7 +67,7 @@ difat_start = blob.read_shift(4, 'i');
ndfs = blob.read_shift(4, 'i');
// Grab FAT Sector Locations
for(var q, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
for(var q = -1, j = 0; j < 109; ++j) { /* 109 = (512 - blob.l)>>>2; */
q = blob.read_shift(4, 'i');
if(q<0) break;
fat_addrs[j] = q;
@ -94,7 +94,6 @@ read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
var root_name = Paths.shift();
Paths.root = root_name;
/* [MS-CFB] 2.6.4 (Unicode 3.0.1 case conversion) */
var find_path = make_find_path(FullPaths, Paths, FileIndex, files, root_name);
@ -145,7 +144,7 @@ function check_shifts(blob, mver) {
/** Break the file up into sectors */
function sectorify(file, ssz) {
var nsectors = Math.ceil(file.length/ssz)-1;
var sectors = new Array(nsectors);
var sectors = [];
for(var i=1; i < nsectors; ++i) sectors[i-1] = file.slice(i*ssz,(i+1)*ssz);
sectors[nsectors-1] = file.slice(nsectors*ssz);
return sectors;
@ -154,7 +153,7 @@ function sectorify(file, ssz) {
/* [MS-CFB] 2.6.4 Red-Black Tree */
function build_full_paths(FI, FPD, FP, Paths) {
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
var dad = new Array(pl), q = new Array(pl);
var dad = [], q = [];
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
@ -194,12 +193,12 @@ function build_full_paths(FI, FPD, FP, Paths) {
/* [MS-CFB] 2.6.4 */
function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
var UCFullPaths = new Array(FullPaths.length);
var UCPaths = new Array(Paths.length), i;
var UCFullPaths = [];
var UCPaths = [], i = 0;
for(i = 0; i < FullPaths.length; ++i) UCFullPaths[i] = FullPaths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
for(i = 0; i < Paths.length; ++i) UCPaths[i] = Paths[i].toUpperCase().replace(chr0,'').replace(chr1,'!');
return function find_path(path) {
var k;
var k = false;
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
else k = path.indexOf("/") !== -1;
var UCPath = path.toUpperCase().replace(chr0,'').replace(chr1,'!');
@ -212,7 +211,7 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
/** Chase down the rest of the DIFAT chain to build a comprehensive list
DIFAT chains by storing the next sector number as the last 32 bytes */
function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
var q;
var q = ENDOFCHAIN;
if(idx === ENDOFCHAIN) {
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
} else if(idx !== -1 /*FREESECT*/) {
@ -229,11 +228,9 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
/** Follow the linked list of sectors for a given starting point */
function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
var sl = sectors.length;
var buf, buf_chain;
if(!chkd) chkd = new Array(sl);
var modulus = ssz - 1, j, jj;
buf = [];
buf_chain = [];
var buf = [], buf_chain = [];
if(!chkd) chkd = [];
var modulus = ssz - 1, j = 0, jj = 0;
for(j=start; j>=0;) {
chkd[j] = true;
buf[buf.length] = j;
@ -249,13 +246,13 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
/** Chase down the sector linked lists */
function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
var sl = sectors.length, sector_list = new Array(sl);
var chkd = new Array(sl), buf, buf_chain;
var modulus = ssz - 1, i, j, k, jj;
var sl = sectors.length, sector_list = ([]);
var chkd = [], buf = [], buf_chain = [];
var modulus = ssz - 1, i=0, j=0, k=0, jj=0;
for(i=0; i < sl; ++i) {
buf = [];
buf = ([]);
k = (i + dir_start); if(k >= sl) k-=sl;
if(chkd[k] === true) continue;
if(chkd[k]) continue;
buf_chain = [];
for(j=k; j>=0;) {
chkd[j] = true;
@ -267,25 +264,24 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
if(!sectors[addr]) break;
j = __readInt32LE(sectors[addr], jj);
}
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])});
}
return sector_list;
}
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex) {
var blob;
var minifat_store = 0, pl = (Paths.length?2:0);
var sector = sector_list[dir_start].data;
var i = 0, namelen = 0, name, o, ctime, mtime;
var i = 0, namelen = 0, name;
for(; i < sector.length; i+= 128) {
blob = sector.slice(i, i+128);
var blob = sector.slice(i, i+128);
prep_blob(blob, 64);
namelen = blob.read_shift(2);
if(namelen === 0) continue;
name = __utf16le(blob,0,namelen-pl);
Paths.push(name);
o = ({
var o = ({
name: name,
type: blob.read_shift(1),
color: blob.read_shift(1),
@ -293,16 +289,14 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
R: blob.read_shift(4, 'i'),
C: blob.read_shift(4, 'i'),
clsid: blob.read_shift(16),
state: blob.read_shift(4, 'i')
state: blob.read_shift(4, 'i'),
start: 0,
size: 0
});
ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) {
o.ctime = ctime; o.ct = read_date(blob, blob.l-8);
}
mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) {
o.mtime = mtime; o.mt = read_date(blob, blob.l-8);
}
var ctime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(ctime !== 0) o.ct = read_date(blob, blob.l-8);
var mtime = blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2) + blob.read_shift(2);
if(mtime !== 0) o.mt = read_date(blob, blob.l-8);
o.start = blob.read_shift(4, 'i');
o.size = blob.read_shift(4, 'i');
if(o.type === 5) { /* root */
@ -313,12 +307,12 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
o.storage = 'fat';
if(sector_list[o.start] === undefined) sector_list[o.start] = get_sector_list(sectors, o.start, sector_list.fat_addrs, sector_list.ssz);
sector_list[o.start].name = o.name;
o.content = sector_list[o.start].data.slice(0,o.size);
o.content = (sector_list[o.start].data.slice(0,o.size));
prep_blob(o.content, 0);
} else {
o.storage = 'minifat';
if(minifat_store !== ENDOFCHAIN && o.start !== ENDOFCHAIN) {
o.content = sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size);
o.content = (sector_list[minifat_store].data.slice(o.start*MSSZ,o.start*MSSZ+o.size));
prep_blob(o.content, 0);
}
}
@ -333,17 +327,17 @@ function read_date(blob, offset) {
var fs;
function readFileSync(filename, options) {
if(fs === undefined) fs = require('fs');
if(fs == null) fs = require('fs');
return parse(fs.readFileSync(filename), options);
}
function readSync(blob, options) {
switch(options !== undefined && options.type !== undefined ? options.type : "base64") {
switch(options && options.type || "base64") {
case "file": return readFileSync(blob, options);
case "base64": return parse(s2a(Base64.decode(blob)), options);
case "binary": return parse(s2a(blob), options);
}
return parse(blob);
return parse(blob, options);
}
/** CFB Constants */