version bump 0.10.3: ignore bad FAT
- sector checked before reading (h/t @e12009) See https://github.com/SheetJS/js-xlsx/issues/569 - flow type checking
This commit is contained in:
parent
0ef16e1ad1
commit
1a428c8f5b
35
.flowconfig
Normal file
35
.flowconfig
Normal file
@ -0,0 +1,35 @@
|
||||
[ignore]
|
||||
.*/node_modules/.*
|
||||
.*/dist/.*
|
||||
.*/test.js
|
||||
|
||||
.*/bits/.*
|
||||
.*/ctest/.*
|
||||
.*/misc/.*
|
||||
.*/perf/.*
|
||||
|
||||
.*/demo/browser.js
|
||||
.*/shim.js
|
||||
|
||||
.*/odsbits/.*
|
||||
.*/xlscfb.js
|
||||
.*/cfb.js
|
||||
.*/jszip.js
|
||||
.*/tests/.*
|
||||
.*/demos/.*
|
||||
|
||||
[include]
|
||||
cfb.flow.js
|
||||
xlscfb.flow.js
|
||||
.*/bin/.*.njs
|
||||
|
||||
[libs]
|
||||
bits/10_types.js
|
||||
misc/flow.js
|
||||
misc/flowdeps.js
|
||||
|
||||
[options]
|
||||
module.file_ext=.js
|
||||
module.file_ext=.njs
|
||||
module.ignore_non_literal_requires=true
|
||||
suppress_comment= \\(.\\|\n\\)*\\$FlowIgnore
|
@ -1,9 +1,14 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.11"
|
||||
- "7"
|
||||
- "6"
|
||||
- "5"
|
||||
- "4"
|
||||
- "0.12"
|
||||
- "0.10"
|
||||
- "0.8"
|
||||
before_install:
|
||||
- "npm install -g npm@next"
|
||||
- "npm install -g mocha"
|
||||
- "npm install blanket"
|
||||
- "npm install xlsjs"
|
||||
|
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright (C) 2013-2014 SheetJS
|
||||
Copyright (C) 2013-present SheetJS
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
123
Makefile
123
Makefile
@ -1,84 +1,58 @@
|
||||
SHELL=/bin/bash
|
||||
LIB=cfb
|
||||
FMT=xls doc ppt misc full
|
||||
REQS=
|
||||
ADDONS=
|
||||
AUXTARGETS=xlscfb.js
|
||||
CMDS=bin/cfb.njs
|
||||
HTMLLINT=index.html
|
||||
|
||||
ULIB=$(shell echo $(LIB) | tr a-z A-Z)
|
||||
DEPS=$(sort $(wildcard bits/*.js))
|
||||
TARGET=$(LIB).js
|
||||
FLOWTARGET=$(LIB).flow.js
|
||||
FLOWTGTS=$(TARGET) $(AUXTARGETS)
|
||||
|
||||
## Main Targets
|
||||
|
||||
.PHONY: all
|
||||
all: $(TARGET) $(AUXTARGETS)
|
||||
all: $(TARGET) $(AUXTARGETS) ## Build library and auxiliary scripts
|
||||
|
||||
$(TARGET): $(DEPS)
|
||||
$(FLOWTGTS): %.js : %.flow.js
|
||||
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^[ \t]*\/\*[:#][^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*[:#][^*]*\*\//gm,""))' > $@
|
||||
|
||||
$(FLOWTARGET): $(DEPS)
|
||||
cat $^ | tr -d '\15\32' > $@
|
||||
|
||||
bits/31_version.js: package.json
|
||||
echo "exports.version = '"`grep version package.json | awk '{gsub(/[^0-9a-z\.-]/,"",$$2); print $$2}'`"';" > $@
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
clean: ## Remove targets and build artifacts
|
||||
rm -f $(TARGET) $(FLOWTARGET)
|
||||
|
||||
.PHONY: clean-data
|
||||
clean-data:
|
||||
rm -fr ./test_files/ ./test_files_pres/
|
||||
|
||||
.PHONY: init
|
||||
init:
|
||||
init: ## Initial setup for development
|
||||
if [ ! -e test_files ]; then git clone https://github.com/SheetJS/test_files; fi
|
||||
cd test_files; git pull; make
|
||||
if [ ! -e test_files_pres ]; then git clone https://github.com/SheetJS/test_files_pres; fi
|
||||
cd test_files_pres; git pull
|
||||
|
||||
.PHONY: test mocha
|
||||
test mocha: test.js
|
||||
mocha -R spec -t 20000
|
||||
|
||||
.PHONY: prof
|
||||
prof:
|
||||
cat misc/prof.js test.js > prof.js
|
||||
node --prof prof.js
|
||||
|
||||
TESTFMT=$(patsubst %,test_%,$(FMT))
|
||||
.PHONY: $(TESTFMT)
|
||||
$(TESTFMT): test_%:
|
||||
FMTS=$* make test
|
||||
|
||||
|
||||
.PHONY: lint
|
||||
lint: $(TARGET)
|
||||
jshint --show-non-errors $(TARGET) $(AUXTARGETS)
|
||||
jscs $(TARGET) $(AUXTARGETS)
|
||||
|
||||
.PHONY: cov cov-spin
|
||||
cov: misc/coverage.html
|
||||
cov-spin:
|
||||
make cov & bash misc/spin.sh $$!
|
||||
|
||||
COVFMT=$(patsubst %,cov_%,$(FMT))
|
||||
.PHONY: $(COVFMT)
|
||||
$(COVFMT): cov_%:
|
||||
FMTS=$* make cov
|
||||
|
||||
misc/coverage.html: $(TARGET) test.js
|
||||
mocha --require blanket -R html-cov > $@
|
||||
|
||||
.PHONY: coveralls coveralls-spin
|
||||
coveralls:
|
||||
mocha --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
|
||||
|
||||
coveralls-spin:
|
||||
make coveralls & bash misc/spin.sh $$!
|
||||
|
||||
.PHONY: dist
|
||||
dist: dist-deps $(TARGET)
|
||||
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)"
|
||||
misc/strip_sourcemap.sh dist/$(LIB).min.js
|
||||
|
||||
.PHONY: dist-deps
|
||||
dist-deps: xlscfb.js ## Copy dependencies for distribution
|
||||
cp xlscfb.js dist/xlscfb.js
|
||||
|
||||
.PHONY: aux
|
||||
aux: $(AUXTARGETS)
|
||||
|
||||
@ -86,9 +60,58 @@ aux: $(AUXTARGETS)
|
||||
xls: xlscfb.js
|
||||
|
||||
XLSDEPS=misc/suppress_export.js $(filter-out bits/08_blob.js,$(DEPS))
|
||||
xlscfb.js: $(XLSDEPS)
|
||||
xlscfb.flow.js: $(XLSDEPS) ## Build support library
|
||||
cat $^ | tr -d '\15\32' > $@
|
||||
|
||||
.PHONY: dist-deps
|
||||
dist-deps: xlscfb.js
|
||||
cp xlscfb.js dist/xlscfb.js
|
||||
|
||||
## Testing
|
||||
|
||||
.PHONY: test mocha
|
||||
test mocha: test.js $(TARGET) ## Run test suite
|
||||
mocha -R spec -t 20000
|
||||
|
||||
#* To run tests for one format, make test_<fmt>
|
||||
TESTFMT=$(patsubst %,test_%,$(FMT))
|
||||
.PHONY: $(TESTFMT)
|
||||
$(TESTFMT): test_%:
|
||||
FMTS=$* make test
|
||||
|
||||
|
||||
## Code Checking
|
||||
|
||||
.PHONY: lint
|
||||
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
|
||||
@jshint --show-non-errors --extract=always $(HTMLLINT)
|
||||
@jscs $(TARGET) $(AUXTARGETS)
|
||||
|
||||
.PHONY: flow
|
||||
flow: lint ## Run flow checker
|
||||
@flow check --all --show-all-errors
|
||||
|
||||
.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 > $@
|
||||
|
||||
.PHONY: coveralls
|
||||
coveralls: ## Coverage Test + Send to coveralls.io
|
||||
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js
|
||||
|
||||
|
||||
.PHONY: help
|
||||
help:
|
||||
@grep -hE '(^[a-zA-Z_-][ a-zA-Z_-]*:.*?|^#[#*])' $(MAKEFILE_LIST) | bash misc/help.sh
|
||||
|
||||
#* To show a spinner, append "-spin" to any target e.g. cov-spin
|
||||
%-spin:
|
||||
@make $* & bash misc/spin.sh $$!
|
||||
|
37
README.md
37
README.md
@ -1,37 +1,41 @@
|
||||
# Compound File Binary Format
|
||||
|
||||
This is a Pure-JS implementation of MS-CFB: Compound File Binary File Format, a
|
||||
format used in many Microsoft file types (such as XLS, DOC, and other Microsoft
|
||||
Office file types).
|
||||
format used in many Microsoft file types (such as XLS and DOC)
|
||||
|
||||
# Utility Installation and Usage
|
||||
|
||||
The package is available on NPM:
|
||||
|
||||
```
|
||||
```bash
|
||||
$ npm install -g cfb
|
||||
$ cfb path/to/CFB/file
|
||||
```
|
||||
|
||||
The command will extract the storages and streams in the container, generating
|
||||
files that line up with the tree-based structure of the storage. Metadata
|
||||
such as the red-black tree are discarded (and in the future, new CFB containers
|
||||
will exclusively use black nodes)
|
||||
files that line up with the tree-based structure of the storage. Metadata such
|
||||
as the red-black tree are discarded.
|
||||
|
||||
# Library Installation and Usage
|
||||
|
||||
In the browser:
|
||||
|
||||
<script src="cfb.js" type="text/javascript"></script>
|
||||
```html
|
||||
<script src="cfb.js" type="text/javascript"></script>
|
||||
```
|
||||
|
||||
In node:
|
||||
|
||||
var CFB = require('cfb');
|
||||
```js
|
||||
var CFB = require('cfb');
|
||||
```
|
||||
|
||||
For example, to get the Workbook content from an XLS file:
|
||||
|
||||
var cfb = CFB.read(filename, {type: 'file'});
|
||||
var workbook = cfb.find('Workbook')
|
||||
```js
|
||||
var cfb = CFB.read(filename, {type: 'file'});
|
||||
var workbook = cfb.find('Workbook')
|
||||
```
|
||||
|
||||
# API
|
||||
|
||||
@ -86,7 +90,7 @@ Case comparison has not been verified for non-ASCII characters
|
||||
|
||||
Writing is not supported. It is in the works, but it has not yet been released.
|
||||
|
||||
The `xlscfb.js` file is designed to be embedded in [js-xls](http://git.io/xls)
|
||||
The `xlscfb.js` file is designed to be embedded in [js-xlsx](http://git.io/xlsx)
|
||||
|
||||
# License
|
||||
|
||||
@ -95,7 +99,14 @@ This implementation is covered under Apache 2.0 license. It complies with the
|
||||
|
||||
[![Build Status](https://travis-ci.org/SheetJS/js-cfb.svg?branch=master)](https://travis-ci.org/SheetJS/js-cfb)
|
||||
|
||||
[![Coverage Status](https://coveralls.io/repos/SheetJS/js-cfb/badge.png?branch=master)](https://coveralls.io/r/SheetJS/js-cfb?branch=master)
|
||||
[![Coverage Status](http://img.shields.io/coveralls/SheetJS/js-cfb/master.svg)](https://coveralls.io/r/SheetJS/js-cfb?branch=master)
|
||||
|
||||
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-cfb?pixel)](https://github.com/SheetJS/js-cfb)
|
||||
|
||||
[![NPM Downloads](https://img.shields.io/npm/dt/cfb.svg)](https://npmjs.org/package/cfb)
|
||||
|
||||
[![Dependencies Status](https://david-dm.org/sheetjs/js-cfb/status.svg)](https://david-dm.org/sheetjs/js-cfb)
|
||||
|
||||
[![ghit.me](https://ghit.me/badge.svg?repo=sheetjs/js-cfb)](https://ghit.me/repo/sheetjs/js-cfb)
|
||||
|
||||
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/88c2e1fd637653cd780b3c6d3dcd70ad "githalytics.com")](http://githalytics.com/SheetJS/js-cfb)
|
||||
|
||||
|
@ -15,14 +15,14 @@ if(program.args.length === 0 || !fs.existsSync(program.args[0])) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
var opts = {type:'file'};
|
||||
var opts = ({type:'file'}/*:any*/);
|
||||
if(program.dev) opts.WTF = true;
|
||||
|
||||
var cfb = CFB.read(program.args[0], opts);
|
||||
if(program.dump) {
|
||||
console.log("Full Paths:")
|
||||
console.log("Full Paths:");
|
||||
console.log(cfb.FullPaths.map(function(x) { return " " + x; }).join("\n"));
|
||||
console.log("Full Path Directory:")
|
||||
console.log("Full Path Directory:");
|
||||
console.log(cfb.FullPathDir);
|
||||
}
|
||||
if(!program.quiet && !program.dump) for(var i=0; i!=cfb.FullPaths.length; ++i) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* cfb.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
|
@ -28,14 +28,14 @@ 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; }); };
|
||||
s2a = _s2a = function _s2a(s/*:string*/) { 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; };
|
||||
__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) { return [].concat.apply([], bufs); };
|
||||
var bconcat = function(bufs/*:any*/) { return [].concat.apply([], bufs); };
|
||||
|
||||
|
||||
if(typeof Buffer !== "undefined") {
|
||||
@ -44,9 +44,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);};
|
||||
s2a = function(s) { return Buffer(s, "binary"); };
|
||||
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
|
||||
__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); };
|
||||
}
|
||||
|
||||
|
||||
@ -56,7 +56,7 @@ var __readInt16LE = function(b, idx) { var u = b[idx+1]*(1<<8)+b[idx]; return (u
|
||||
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]; };
|
||||
|
||||
function ReadShift(size, t) {
|
||||
function ReadShift(size/*:number*/, t/*:?any*/) {
|
||||
var oI, oS, type = 0;
|
||||
switch(size) {
|
||||
case 1: oI = __readUInt8(this, this.l); break;
|
||||
@ -67,13 +67,13 @@ function ReadShift(size, t) {
|
||||
this.l+=size; if(type === 0) return oI; return oS;
|
||||
}
|
||||
|
||||
function CheckField(hexstr, fld) {
|
||||
function CheckField(hexstr/*:string*/, fld/*:string*/) {
|
||||
var m = __hexlify(this,this.l,hexstr.length>>1);
|
||||
if(m !== hexstr) throw fld + 'Expected ' + hexstr + ' saw ' + m;
|
||||
this.l += hexstr.length>>1;
|
||||
}
|
||||
|
||||
function prep_blob(blob, pos) {
|
||||
function prep_blob(blob/*:any*/, pos/*:number*/) {
|
||||
blob.l = pos;
|
||||
blob.read_shift = ReadShift;
|
||||
blob.chk = CheckField;
|
||||
|
10
bits/10_types.js
Normal file
10
bits/10_types.js
Normal file
@ -0,0 +1,10 @@
|
||||
/*::
|
||||
declare var DO_NOT_EXPORT_CFB:any;
|
||||
type SectorEntry = any;
|
||||
type SectorList = {
|
||||
(k:string|number):SectorEntry;
|
||||
name:?string;
|
||||
fat_addrs:any;
|
||||
ssz:number;
|
||||
}
|
||||
*/
|
@ -1 +1 @@
|
||||
exports.version = '0.10.2';
|
||||
exports.version = '0.10.3';
|
||||
|
@ -10,7 +10,7 @@ var difat_start = 0; // first mini FAT sector location
|
||||
var fat_addrs = []; // locations of FAT sectors
|
||||
|
||||
/* [MS-CFB] 2.2 Compound File Header */
|
||||
var blob = file.slice(0,512);
|
||||
var blob/*:any*/ = file.slice(0,512);
|
||||
prep_blob(blob, 0);
|
||||
|
||||
/* major version */
|
||||
@ -70,7 +70,7 @@ var sectors = sectorify(file, ssz);
|
||||
sleuth_fat(difat_start, ndfs, sectors, ssz, fat_addrs);
|
||||
|
||||
/** Chains */
|
||||
var sector_list = make_sector_list(sectors, dir_start, fat_addrs, ssz);
|
||||
var sector_list/*:SectorList*/ = make_sector_list(sectors, dir_start, fat_addrs, ssz);
|
||||
|
||||
sector_list[dir_start].name = "!Directory";
|
||||
if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
|
||||
@ -79,7 +79,7 @@ sector_list.fat_addrs = fat_addrs;
|
||||
sector_list.ssz = ssz;
|
||||
|
||||
/* [MS-CFB] 2.6.1 Compound File Directory Entry */
|
||||
var files = {}, Paths = [], FileIndex = [], FullPaths = [], FullPathDir = {};
|
||||
var files = {}, Paths/*:any*/ = [], FileIndex = [], FullPaths = [], FullPathDir = {};
|
||||
read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, FileIndex);
|
||||
|
||||
build_full_paths(FileIndex, FullPathDir, FullPaths, Paths);
|
||||
|
@ -4,7 +4,7 @@ function make_find_path(FullPaths, Paths, FileIndex, files, root_name) {
|
||||
var UCPaths = new Array(Paths.length), i;
|
||||
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) {
|
||||
return function find_path(path/*:string*/) {
|
||||
var k;
|
||||
if(path.charCodeAt(0) === 47 /* "/" */) { k=true; path = root_name + path; }
|
||||
else k = path.indexOf("/") !== -1;
|
||||
|
@ -6,6 +6,7 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
@ -29,13 +30,14 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
}
|
||||
|
||||
/** Chase down the sector linked lists */
|
||||
function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
|
||||
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;
|
||||
@ -51,6 +53,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
|
@ -11,7 +11,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
if(namelen === 0) continue;
|
||||
name = __utf16le(blob,0,namelen-pl);
|
||||
Paths.push(name);
|
||||
o = {
|
||||
o = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
@ -20,7 +20,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
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);
|
||||
|
@ -1,10 +1,10 @@
|
||||
var fs;
|
||||
function readFileSync(filename, options) {
|
||||
function readFileSync(filename/*:string*/, options/*:any*/) {
|
||||
if(fs === undefined) fs = require('fs');
|
||||
return parse(fs.readFileSync(filename), options);
|
||||
}
|
||||
|
||||
function readSync(blob, 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);
|
||||
|
476
cfb.flow.js
Normal file
476
cfb.flow.js
Normal file
@ -0,0 +1,476 @@
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
var Base64 = (function(){
|
||||
var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
return {
|
||||
decode: function(input) {
|
||||
var o = "";
|
||||
var c1, c2, c3;
|
||||
var e1, e2, e3, e4;
|
||||
input = input.replace(/[^\w\+\/\=]/g, "");
|
||||
for(var i = 0; i < input.length;) {
|
||||
e1 = map.indexOf(input.charAt(i++));
|
||||
e2 = map.indexOf(input.charAt(i++));
|
||||
c1 = (e1 << 2) | (e2 >> 4);
|
||||
o += String.fromCharCode(c1);
|
||||
|
||||
e3 = map.indexOf(input.charAt(i++));
|
||||
c2 = ((e2 & 15) << 4) | (e3 >> 2);
|
||||
if (e3 !== 64) { o += String.fromCharCode(c2); }
|
||||
|
||||
e4 = map.indexOf(input.charAt(i++));
|
||||
c3 = ((e3 & 3) << 6) | e4;
|
||||
if (e4 !== 64) { o += String.fromCharCode(c3); }
|
||||
}
|
||||
return o;
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
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); };
|
||||
|
||||
|
||||
if(typeof Buffer !== "undefined") {
|
||||
__utf16le = function(b,s,e) {
|
||||
if(!Buffer.isBuffer(b)) 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); };
|
||||
}
|
||||
|
||||
|
||||
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]; };
|
||||
|
||||
function ReadShift(size/*:number*/, t/*:?any*/) {
|
||||
var oI, oS, type = 0;
|
||||
switch(size) {
|
||||
case 1: oI = __readUInt8(this, this.l); break;
|
||||
case 2: oI = (t !== 'i' ? __readUInt16LE : __readInt16LE)(this, this.l); break;
|
||||
case 4: oI = __readInt32LE(this, this.l); break;
|
||||
case 16: type = 2; oS = __hexlify(this, this.l, size);
|
||||
}
|
||||
this.l+=size; if(type === 0) return oI; return oS;
|
||||
}
|
||||
|
||||
function CheckField(hexstr/*:string*/, fld/*:string*/) {
|
||||
var m = __hexlify(this,this.l,hexstr.length>>1);
|
||||
if(m !== hexstr) throw fld + 'Expected ' + hexstr + ' saw ' + m;
|
||||
this.l += hexstr.length>>1;
|
||||
}
|
||||
|
||||
function prep_blob(blob/*:any*/, pos/*:number*/) {
|
||||
blob.l = pos;
|
||||
blob.read_shift = ReadShift;
|
||||
blob.chk = CheckField;
|
||||
}
|
||||
|
||||
/*::
|
||||
declare var DO_NOT_EXPORT_CFB:any;
|
||||
type SectorEntry = any;
|
||||
type SectorList = {
|
||||
(k:string|number):SectorEntry;
|
||||
name:?string;
|
||||
fat_addrs:any;
|
||||
ssz:number;
|
||||
}
|
||||
*/
|
||||
/* [MS-CFB] v20130118 */
|
||||
var CFB = (function _CFB(){
|
||||
var exports = {};
|
||||
exports.version = '0.10.3';
|
||||
function parse(file) {
|
||||
var mver = 3; // major version
|
||||
var ssz = 512; // sector size
|
||||
var nmfs = 0; // number of mini FAT sectors
|
||||
var ndfs = 0; // number of DIFAT sectors
|
||||
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
|
||||
|
||||
/* [MS-CFB] 2.2 Compound File Header */
|
||||
var blob/*:any*/ = file.slice(0,512);
|
||||
prep_blob(blob, 0);
|
||||
|
||||
/* major version */
|
||||
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;
|
||||
}
|
||||
|
||||
/* reprocess header */
|
||||
if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
|
||||
/* Save header for final object */
|
||||
var header = file.slice(0,ssz);
|
||||
|
||||
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;
|
||||
|
||||
// Number of FAT Sectors
|
||||
//var nfs = blob.read_shift(4, 'i');
|
||||
blob.l += 4;
|
||||
|
||||
// First Directory Sector Location
|
||||
dir_start = blob.read_shift(4, 'i');
|
||||
|
||||
// Transaction Signature
|
||||
blob.l += 4;
|
||||
|
||||
// Mini Stream Cutoff Size
|
||||
blob.chk('00100000', 'Mini Stream Cutoff Size: ');
|
||||
|
||||
// First Mini FAT Sector Location
|
||||
minifat_start = blob.read_shift(4, 'i');
|
||||
|
||||
// Number of Mini FAT Sectors
|
||||
nmfs = blob.read_shift(4, 'i');
|
||||
|
||||
// First DIFAT sector location
|
||||
difat_start = blob.read_shift(4, 'i');
|
||||
|
||||
// Number of DIFAT Sectors
|
||||
ndfs = blob.read_shift(4, 'i');
|
||||
|
||||
// Grab FAT Sector Locations
|
||||
for(var q, 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);
|
||||
|
||||
sleuth_fat(difat_start, ndfs, sectors, ssz, fat_addrs);
|
||||
|
||||
/** Chains */
|
||||
var sector_list/*:SectorList*/ = make_sector_list(sectors, dir_start, fat_addrs, ssz);
|
||||
|
||||
sector_list[dir_start].name = "!Directory";
|
||||
if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
|
||||
sector_list[fat_addrs[0]].name = "!FAT";
|
||||
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 = {};
|
||||
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);
|
||||
|
||||
return {
|
||||
raw: {header: header, sectors: sectors},
|
||||
FileIndex: FileIndex,
|
||||
FullPaths: FullPaths,
|
||||
FullPathDir: FullPathDir,
|
||||
find: find_path
|
||||
};
|
||||
} // parse
|
||||
|
||||
/* [MS-CFB] 2.2 Compound File Header -- read up to major version */
|
||||
function check_get_mver(blob) {
|
||||
// header signature 8
|
||||
blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
|
||||
|
||||
// clsid 16
|
||||
blob.chk(HEADER_CLSID, 'CLSID: ');
|
||||
|
||||
// minor version 2
|
||||
var mver = blob.read_shift(2, 'u');
|
||||
|
||||
return [blob.read_shift(2,'u'), mver];
|
||||
}
|
||||
function check_shifts(blob, mver) {
|
||||
var shift = 0x09;
|
||||
|
||||
// Byte Order
|
||||
blob.chk('feff', 'Byte Order: ');
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Mini Sector Shift
|
||||
blob.chk('0600', 'Mini Sector Shift: ');
|
||||
|
||||
// Reserved
|
||||
blob.chk('000000000000', 'Reserved: ');
|
||||
}
|
||||
|
||||
/** Break the file up into sectors */
|
||||
function sectorify(file, ssz) {
|
||||
var nsectors = Math.ceil(file.length/ssz)-1;
|
||||
var sectors = new Array(nsectors);
|
||||
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) {
|
||||
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
|
||||
var dad = new Array(pl), q = new Array(pl);
|
||||
|
||||
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
|
||||
|
||||
for(; j < q.length; ++j) {
|
||||
i = q[j];
|
||||
L = FI[i].L; R = FI[i].R; C = FI[i].C;
|
||||
if(dad[i] === i) {
|
||||
if(L !== -1 /*NOSTREAM*/ && dad[L] !== L) dad[i] = dad[L];
|
||||
if(R !== -1 && dad[R] !== R) dad[i] = dad[R];
|
||||
}
|
||||
if(C !== -1 /*NOSTREAM*/) dad[C] = i;
|
||||
if(L !== -1) { dad[L] = dad[i]; q.push(L); }
|
||||
if(R !== -1) { dad[R] = dad[i]; q.push(R); }
|
||||
}
|
||||
for(i=1; i !== pl; ++i) if(dad[i] === i) {
|
||||
if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R];
|
||||
else if(L !== -1 && dad[L] !== L) dad[i] = dad[L];
|
||||
}
|
||||
|
||||
for(i=1; i < pl; ++i) {
|
||||
if(FI[i].type === 0 /* unknown */) continue;
|
||||
j = dad[i];
|
||||
if(j === 0) FP[i] = FP[0] + "/" + FP[i];
|
||||
else while(j !== 0) {
|
||||
FP[i] = FP[j] + "/" + FP[i];
|
||||
j = dad[j];
|
||||
}
|
||||
dad[i] = 0;
|
||||
}
|
||||
|
||||
FP[0] += "/";
|
||||
for(i=1; i < pl; ++i) {
|
||||
if(FI[i].type !== 2 /* stream */) FP[i] += "/";
|
||||
FPD[FP[i]] = FI[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* [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;
|
||||
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;
|
||||
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);
|
||||
if(w === -1) return null;
|
||||
return k === true ? FileIndex[w] : files[Paths[w]];
|
||||
};
|
||||
}
|
||||
|
||||
/** 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;
|
||||
if(idx === ENDOFCHAIN) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
}
|
||||
sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, 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 = [];
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
}
|
||||
|
||||
/** 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;
|
||||
for(i=0; i < sl; ++i) {
|
||||
buf = [];
|
||||
k = (i + dir_start); if(k >= sl) k-=sl;
|
||||
if(chkd[k] === true) 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)];
|
||||
jj = ((j*4) & modulus);
|
||||
if(ssz < 4 + jj) throw "FAT boundary crossed: " + j + " 4 "+ssz;
|
||||
if(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
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;
|
||||
for(; i < sector.length; i+= 128) {
|
||||
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 = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
L: blob.read_shift(4, 'i'),
|
||||
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);
|
||||
}
|
||||
o.start = blob.read_shift(4, 'i');
|
||||
o.size = blob.read_shift(4, 'i');
|
||||
if(o.type === 5) { /* root */
|
||||
minifat_store = o.start;
|
||||
if(nmfs > 0 && minifat_store !== ENDOFCHAIN) sector_list[minifat_store].name = "!StreamData";
|
||||
/*minifat_size = o.size;*/
|
||||
} else if(o.size >= 4096 /* MSCSZ */) {
|
||||
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);
|
||||
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);
|
||||
prep_blob(o.content, 0);
|
||||
}
|
||||
}
|
||||
files[name] = o;
|
||||
FileIndex.push(o);
|
||||
}
|
||||
}
|
||||
|
||||
function read_date(blob, offset) {
|
||||
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');
|
||||
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);
|
||||
}
|
||||
return parse(blob);
|
||||
}
|
||||
|
||||
/** CFB Constants */
|
||||
var MSSZ = 64; /* Mini Sector Size = 1<<6 */
|
||||
//var MSCSZ = 4096; /* Mini Stream Cutoff Size */
|
||||
/* 2.1 Compound File Sector Numbers and Types */
|
||||
var ENDOFCHAIN = -2;
|
||||
/* 2.2 Compound File Header */
|
||||
var HEADER_SIGNATURE = 'd0cf11e0a1b11ae1';
|
||||
var HEADER_CLSID = '00000000000000000000000000000000';
|
||||
var consts = {
|
||||
/* 2.1 Compund File Sector Numbers and Types */
|
||||
MAXREGSECT: -6,
|
||||
DIFSECT: -4,
|
||||
FATSECT: -3,
|
||||
ENDOFCHAIN: ENDOFCHAIN,
|
||||
FREESECT: -1,
|
||||
/* 2.2 Compound File Header */
|
||||
HEADER_SIGNATURE: HEADER_SIGNATURE,
|
||||
HEADER_MINOR_VERSION: '3e00',
|
||||
MAXREGSID: -6,
|
||||
NOSTREAM: -1,
|
||||
HEADER_CLSID: HEADER_CLSID,
|
||||
/* 2.6.1 Compound File Directory Entry */
|
||||
EntryTypes: ['unknown','storage','stream','lockbytes','property','root']
|
||||
};
|
||||
|
||||
exports.read = readSync;
|
||||
exports.parse = parse;
|
||||
exports.utils = {
|
||||
ReadShift: ReadShift,
|
||||
CheckField: CheckField,
|
||||
prep_blob: prep_blob,
|
||||
bconcat: bconcat,
|
||||
consts: consts
|
||||
};
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }
|
13
cfb.js
13
cfb.js
@ -1,4 +1,4 @@
|
||||
/* cfb.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
@ -49,7 +49,7 @@ if(typeof Buffer !== "undefined") {
|
||||
};
|
||||
__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);};
|
||||
s2a = function(s) { return Buffer(s, "binary"); };
|
||||
s2a = function(s) { return new Buffer(s, "binary"); };
|
||||
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ function prep_blob(blob, pos) {
|
||||
/* [MS-CFB] v20130118 */
|
||||
var CFB = (function _CFB(){
|
||||
var exports = {};
|
||||
exports.version = '0.10.2';
|
||||
exports.version = '0.10.3';
|
||||
function parse(file) {
|
||||
var mver = 3; // major version
|
||||
var ssz = 512; // sector size
|
||||
@ -296,6 +296,7 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
@ -319,6 +320,7 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -341,6 +343,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -361,7 +364,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
if(namelen === 0) continue;
|
||||
name = __utf16le(blob,0,namelen-pl);
|
||||
Paths.push(name);
|
||||
o = {
|
||||
o = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
@ -370,7 +373,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
C: blob.read_shift(4, 'i'),
|
||||
clsid: blob.read_shift(16),
|
||||
state: blob.read_shift(4, 'i')
|
||||
};
|
||||
});
|
||||
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);
|
||||
|
2
dist/LICENSE
vendored
2
dist/LICENSE
vendored
@ -1,4 +1,4 @@
|
||||
Copyright (C) 2013-2014 SheetJS
|
||||
Copyright (C) 2013-present SheetJS
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
13
dist/cfb.js
vendored
13
dist/cfb.js
vendored
@ -1,4 +1,4 @@
|
||||
/* cfb.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
@ -49,7 +49,7 @@ if(typeof Buffer !== "undefined") {
|
||||
};
|
||||
__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);};
|
||||
s2a = function(s) { return Buffer(s, "binary"); };
|
||||
s2a = function(s) { return new Buffer(s, "binary"); };
|
||||
bconcat = function(bufs) { return Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : [].concat.apply([], bufs); };
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ function prep_blob(blob, pos) {
|
||||
/* [MS-CFB] v20130118 */
|
||||
var CFB = (function _CFB(){
|
||||
var exports = {};
|
||||
exports.version = '0.10.2';
|
||||
exports.version = '0.10.3';
|
||||
function parse(file) {
|
||||
var mver = 3; // major version
|
||||
var ssz = 512; // sector size
|
||||
@ -296,6 +296,7 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
@ -319,6 +320,7 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -341,6 +343,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -361,7 +364,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
if(namelen === 0) continue;
|
||||
name = __utf16le(blob,0,namelen-pl);
|
||||
Paths.push(name);
|
||||
o = {
|
||||
o = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
@ -370,7 +373,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
C: blob.read_shift(4, 'i'),
|
||||
clsid: blob.read_shift(16),
|
||||
state: blob.read_shift(4, 'i')
|
||||
};
|
||||
});
|
||||
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);
|
||||
|
4
dist/cfb.min.js
vendored
4
dist/cfb.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/cfb.min.map
vendored
2
dist/cfb.min.map
vendored
File diff suppressed because one or more lines are too long
11
dist/xlscfb.js
vendored
11
dist/xlscfb.js
vendored
@ -1,12 +1,12 @@
|
||||
var DO_NOT_EXPORT_CFB = true;
|
||||
/* cfb.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
/* [MS-CFB] v20130118 */
|
||||
var CFB = (function _CFB(){
|
||||
var exports = {};
|
||||
exports.version = '0.10.2';
|
||||
exports.version = '0.10.3';
|
||||
function parse(file) {
|
||||
var mver = 3; // major version
|
||||
var ssz = 512; // sector size
|
||||
@ -216,6 +216,7 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
@ -239,6 +240,7 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -261,6 +263,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -281,7 +284,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
if(namelen === 0) continue;
|
||||
name = __utf16le(blob,0,namelen-pl);
|
||||
Paths.push(name);
|
||||
o = {
|
||||
o = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
@ -290,7 +293,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
C: blob.read_shift(4, 'i'),
|
||||
clsid: blob.read_shift(16),
|
||||
state: blob.read_shift(4, 'i')
|
||||
};
|
||||
});
|
||||
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);
|
||||
|
@ -1,3 +1,6 @@
|
||||
apachepoi_testEXCEL_3.xls
|
||||
apachepoi_testEXCEL_4.xls
|
||||
xlrd_biff4_no_format_no_window2.xls
|
||||
roo_type_excelx.xls
|
||||
roo_type_openoffice.xls
|
||||
libreoffice_calc_csv-import_malformed-quotes.xls
|
||||
|
47
index.html
47
index.html
@ -1,4 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<!-- cfb.js (C) 2013-present SheetJS http://sheetjs.com -->
|
||||
<!-- vim: set ts=2: -->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>JS-CFB Live Demo</title>
|
||||
<style>
|
||||
#drop{
|
||||
border:2px dashed #bbb;
|
||||
@ -13,15 +19,33 @@
|
||||
width:100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<b>JS-CFB Live Demo</b><br />
|
||||
|
||||
<div id="drop">Drop an XLS file here to see the CFB structure.</div>
|
||||
Advanced Demo Options: <br />
|
||||
Use readAsBinaryString: (when available) <input type="checkbox" name="userabs" checked><br />
|
||||
<pre id="out"></pre>
|
||||
<br />
|
||||
<script src="cfb.js"></script>
|
||||
<script>
|
||||
/*jshint browser:true */
|
||||
/*global CFB, out */
|
||||
var rABS = typeof FileReader !== "undefined" && typeof FileReader.prototype !== "undefined" && typeof FileReader.prototype.readAsBinaryString !== "undefined";
|
||||
if(!rABS) {
|
||||
document.getElementsByName("userabs")[0].disabled = true;
|
||||
document.getElementsByName("userabs")[0].checked = false;
|
||||
}
|
||||
|
||||
function fixdata(data) {
|
||||
var o = "", l = 0, w = 10240;
|
||||
for(; l<data.byteLength/w; ++l) o+=String.fromCharCode.apply(null,new Uint8Array(data.slice(l*w,l*w+w)));
|
||||
o+=String.fromCharCode.apply(null, new Uint8Array(data.slice(l*w)));
|
||||
return o;
|
||||
}
|
||||
|
||||
function process_data(data) {
|
||||
var output = "";
|
||||
if(out.innerText === undefined) out.textContent = data.FullPaths;
|
||||
else out.innerText = data.FullPaths.join("\n");
|
||||
}
|
||||
@ -30,18 +54,25 @@ var drop = document.getElementById('drop');
|
||||
function handleDrop(e) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
rABS = document.getElementsByName("userabs")[0].checked;
|
||||
var files = e.dataTransfer.files;
|
||||
var i,f;
|
||||
for (i = 0, f = files[i]; i != files.length; ++i) {
|
||||
var f = files[0];
|
||||
{
|
||||
var reader = new FileReader();
|
||||
var name = f.name;
|
||||
//var name = f.name;
|
||||
reader.onload = function(e) {
|
||||
var data = e.target.result;
|
||||
var cfb = CFB.read(data, {type: 'binary'});
|
||||
var cfb;
|
||||
if(rABS) {
|
||||
cfb = CFB.read(data, {type: 'binary'});
|
||||
} else {
|
||||
var arr = fixdata(data);
|
||||
cfb = CFB.read(btoa(arr), {type: 'base64'});
|
||||
}
|
||||
process_data(cfb);
|
||||
};
|
||||
reader.readAsBinaryString(f);
|
||||
//reader.readAsArrayBuffer(f);
|
||||
if(rABS) reader.readAsBinaryString(f);
|
||||
else reader.readAsArrayBuffer(f);
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,3 +88,5 @@ if(drop.addEventListener) {
|
||||
drop.addEventListener('drop', handleDrop, false);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
5
misc/flow.js
Normal file
5
misc/flow.js
Normal file
@ -0,0 +1,5 @@
|
||||
/*::
|
||||
|
||||
type CFBModule = any;
|
||||
|
||||
*/
|
9
misc/flowdeps.js
Normal file
9
misc/flowdeps.js
Normal file
@ -0,0 +1,9 @@
|
||||
/*::
|
||||
|
||||
declare module 'cfb' { declare var exports:CFBModule; };
|
||||
declare module '../' { declare var exports:CFBModule; };
|
||||
declare module './' { declare var exports:CFBModule; };
|
||||
|
||||
declare module 'commander' { declare var exports:any; };
|
||||
|
||||
*/
|
42
misc/help.sh
Executable file
42
misc/help.sh
Executable file
@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
# make_help.sh -- process listing of targets and special items in Makefile
|
||||
# Copyright (C) 2016-present SheetJS
|
||||
#
|
||||
# usage in makefile: pipe the output of the following command:
|
||||
# @grep -hE '(^[a-zA-Z_-][ a-zA-Z_-]*:.*?|^#[#*])' $(MAKEFILE_LIST)
|
||||
#
|
||||
# lines starting with "## " are treated as subtitles
|
||||
# lines starting with "#* " are treated as plaintext comments
|
||||
# multiple targets with "## " after the ":" are rendered as separate targets
|
||||
# if the presumed default target is labeled, it will be assigned a unique color
|
||||
|
||||
awk '
|
||||
BEGIN{recipes=0;}
|
||||
!/#[#*] .*$/ {next;}
|
||||
{multi=0; isrecipe=0;}
|
||||
/^[^#]*:/ {isrecipe=1; ++recipes;}
|
||||
/^[^ :]* .*:/ {multi=1}
|
||||
multi==0 && isrecipe>0 { if(recipes > 1) print; else print $0, "[default]"; next}
|
||||
isrecipe == 0 {print; next}
|
||||
multi>0 {
|
||||
k=split($0, msg, "##"); m=split($0, a, ":"); n=split(a[1], b, " ");
|
||||
for(i=1; i<=n; ++i) print b[i] ":", "##" msg[2], (recipes==1 && i==1 ? "[default]" : "")
|
||||
}
|
||||
END {}
|
||||
' | if [[ -t 1 ]]; then
|
||||
awk '
|
||||
BEGIN {FS = ":.*?## "}
|
||||
{color=36}
|
||||
/\[default\]/ {color=35}
|
||||
NF==1 && /^##/ {color=34}
|
||||
NF==1 && /^#\*/ {color=20; $1 = substr($1, 4)}
|
||||
{printf "\033[" color "m%-20s\033[0m %s\n", $1, $2;}
|
||||
END{}' -
|
||||
else
|
||||
awk '
|
||||
BEGIN {FS = ":.*?## "}
|
||||
/^#\* / {$1 = substr($1, 4)}
|
||||
{printf "%-20s %s\n", $1, $2;}
|
||||
END{}' -
|
||||
fi
|
||||
|
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
# spin.sh -- show a spinner (for coverage test)
|
||||
# Copyright (C) 2014 SheetJS
|
||||
# Copyright (C) 2014-present SheetJS
|
||||
|
||||
wpid=$1
|
||||
delay=1
|
||||
|
@ -1 +1,15 @@
|
||||
var DO_NOT_EXPORT_CFB = true;
|
||||
/*::
|
||||
declare var Base64:any;
|
||||
declare var ReadShift:any;
|
||||
declare var CheckField:any;
|
||||
declare var prep_blob:any;
|
||||
declare var __readUInt32LE:any;
|
||||
declare var __readInt32LE:any;
|
||||
declare var __toBuffer:any;
|
||||
declare var __utf16le:any;
|
||||
declare var bconcat:any;
|
||||
declare var s2a:any;
|
||||
declare var chr0:any;
|
||||
declare var chr1:any;
|
||||
*/
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cfb",
|
||||
"version": "0.10.2-a",
|
||||
"version": "0.10.3",
|
||||
"author": "sheetjs",
|
||||
"description": "Compound File Binary File Format extractor",
|
||||
"keywords": [ "cfb", "compression", "office" ],
|
||||
@ -13,7 +13,8 @@
|
||||
"README.md",
|
||||
"bin/",
|
||||
"dist/",
|
||||
"cfb.js"
|
||||
"cfb.js",
|
||||
"xlscfb.flow.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"commander":""
|
||||
|
410
xlscfb.flow.js
Normal file
410
xlscfb.flow.js
Normal file
@ -0,0 +1,410 @@
|
||||
var DO_NOT_EXPORT_CFB = true;
|
||||
/*::
|
||||
declare var Base64:any;
|
||||
declare var ReadShift:any;
|
||||
declare var CheckField:any;
|
||||
declare var prep_blob:any;
|
||||
declare var __readUInt32LE:any;
|
||||
declare var __readInt32LE:any;
|
||||
declare var __toBuffer:any;
|
||||
declare var __utf16le:any;
|
||||
declare var bconcat:any;
|
||||
declare var s2a:any;
|
||||
declare var chr0:any;
|
||||
declare var chr1:any;
|
||||
*/
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
/*::
|
||||
declare var DO_NOT_EXPORT_CFB:any;
|
||||
type SectorEntry = any;
|
||||
type SectorList = {
|
||||
(k:string|number):SectorEntry;
|
||||
name:?string;
|
||||
fat_addrs:any;
|
||||
ssz:number;
|
||||
}
|
||||
*/
|
||||
/* [MS-CFB] v20130118 */
|
||||
var CFB = (function _CFB(){
|
||||
var exports = {};
|
||||
exports.version = '0.10.3';
|
||||
function parse(file) {
|
||||
var mver = 3; // major version
|
||||
var ssz = 512; // sector size
|
||||
var nmfs = 0; // number of mini FAT sectors
|
||||
var ndfs = 0; // number of DIFAT sectors
|
||||
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
|
||||
|
||||
/* [MS-CFB] 2.2 Compound File Header */
|
||||
var blob/*:any*/ = file.slice(0,512);
|
||||
prep_blob(blob, 0);
|
||||
|
||||
/* major version */
|
||||
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;
|
||||
}
|
||||
|
||||
/* reprocess header */
|
||||
if(ssz !== 512) { blob = file.slice(0,ssz); prep_blob(blob, 28 /* blob.l */); }
|
||||
/* Save header for final object */
|
||||
var header = file.slice(0,ssz);
|
||||
|
||||
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;
|
||||
|
||||
// Number of FAT Sectors
|
||||
//var nfs = blob.read_shift(4, 'i');
|
||||
blob.l += 4;
|
||||
|
||||
// First Directory Sector Location
|
||||
dir_start = blob.read_shift(4, 'i');
|
||||
|
||||
// Transaction Signature
|
||||
blob.l += 4;
|
||||
|
||||
// Mini Stream Cutoff Size
|
||||
blob.chk('00100000', 'Mini Stream Cutoff Size: ');
|
||||
|
||||
// First Mini FAT Sector Location
|
||||
minifat_start = blob.read_shift(4, 'i');
|
||||
|
||||
// Number of Mini FAT Sectors
|
||||
nmfs = blob.read_shift(4, 'i');
|
||||
|
||||
// First DIFAT sector location
|
||||
difat_start = blob.read_shift(4, 'i');
|
||||
|
||||
// Number of DIFAT Sectors
|
||||
ndfs = blob.read_shift(4, 'i');
|
||||
|
||||
// Grab FAT Sector Locations
|
||||
for(var q, 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);
|
||||
|
||||
sleuth_fat(difat_start, ndfs, sectors, ssz, fat_addrs);
|
||||
|
||||
/** Chains */
|
||||
var sector_list/*:SectorList*/ = make_sector_list(sectors, dir_start, fat_addrs, ssz);
|
||||
|
||||
sector_list[dir_start].name = "!Directory";
|
||||
if(nmfs > 0 && minifat_start !== ENDOFCHAIN) sector_list[minifat_start].name = "!MiniFAT";
|
||||
sector_list[fat_addrs[0]].name = "!FAT";
|
||||
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 = {};
|
||||
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);
|
||||
|
||||
return {
|
||||
raw: {header: header, sectors: sectors},
|
||||
FileIndex: FileIndex,
|
||||
FullPaths: FullPaths,
|
||||
FullPathDir: FullPathDir,
|
||||
find: find_path
|
||||
};
|
||||
} // parse
|
||||
|
||||
/* [MS-CFB] 2.2 Compound File Header -- read up to major version */
|
||||
function check_get_mver(blob) {
|
||||
// header signature 8
|
||||
blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
|
||||
|
||||
// clsid 16
|
||||
blob.chk(HEADER_CLSID, 'CLSID: ');
|
||||
|
||||
// minor version 2
|
||||
var mver = blob.read_shift(2, 'u');
|
||||
|
||||
return [blob.read_shift(2,'u'), mver];
|
||||
}
|
||||
function check_shifts(blob, mver) {
|
||||
var shift = 0x09;
|
||||
|
||||
// Byte Order
|
||||
blob.chk('feff', 'Byte Order: ');
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Mini Sector Shift
|
||||
blob.chk('0600', 'Mini Sector Shift: ');
|
||||
|
||||
// Reserved
|
||||
blob.chk('000000000000', 'Reserved: ');
|
||||
}
|
||||
|
||||
/** Break the file up into sectors */
|
||||
function sectorify(file, ssz) {
|
||||
var nsectors = Math.ceil(file.length/ssz)-1;
|
||||
var sectors = new Array(nsectors);
|
||||
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) {
|
||||
var i = 0, L = 0, R = 0, C = 0, j = 0, pl = Paths.length;
|
||||
var dad = new Array(pl), q = new Array(pl);
|
||||
|
||||
for(; i < pl; ++i) { dad[i]=q[i]=i; FP[i]=Paths[i]; }
|
||||
|
||||
for(; j < q.length; ++j) {
|
||||
i = q[j];
|
||||
L = FI[i].L; R = FI[i].R; C = FI[i].C;
|
||||
if(dad[i] === i) {
|
||||
if(L !== -1 /*NOSTREAM*/ && dad[L] !== L) dad[i] = dad[L];
|
||||
if(R !== -1 && dad[R] !== R) dad[i] = dad[R];
|
||||
}
|
||||
if(C !== -1 /*NOSTREAM*/) dad[C] = i;
|
||||
if(L !== -1) { dad[L] = dad[i]; q.push(L); }
|
||||
if(R !== -1) { dad[R] = dad[i]; q.push(R); }
|
||||
}
|
||||
for(i=1; i !== pl; ++i) if(dad[i] === i) {
|
||||
if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R];
|
||||
else if(L !== -1 && dad[L] !== L) dad[i] = dad[L];
|
||||
}
|
||||
|
||||
for(i=1; i < pl; ++i) {
|
||||
if(FI[i].type === 0 /* unknown */) continue;
|
||||
j = dad[i];
|
||||
if(j === 0) FP[i] = FP[0] + "/" + FP[i];
|
||||
else while(j !== 0) {
|
||||
FP[i] = FP[j] + "/" + FP[i];
|
||||
j = dad[j];
|
||||
}
|
||||
dad[i] = 0;
|
||||
}
|
||||
|
||||
FP[0] += "/";
|
||||
for(i=1; i < pl; ++i) {
|
||||
if(FI[i].type !== 2 /* stream */) FP[i] += "/";
|
||||
FPD[FP[i]] = FI[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* [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;
|
||||
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;
|
||||
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);
|
||||
if(w === -1) return null;
|
||||
return k === true ? FileIndex[w] : files[Paths[w]];
|
||||
};
|
||||
}
|
||||
|
||||
/** 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;
|
||||
if(idx === ENDOFCHAIN) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
}
|
||||
sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, 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 = [];
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
}
|
||||
|
||||
/** 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;
|
||||
for(i=0; i < sl; ++i) {
|
||||
buf = [];
|
||||
k = (i + dir_start); if(k >= sl) k-=sl;
|
||||
if(chkd[k] === true) 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)];
|
||||
jj = ((j*4) & modulus);
|
||||
if(ssz < 4 + jj) throw "FAT boundary crossed: " + j + " 4 "+ssz;
|
||||
if(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
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;
|
||||
for(; i < sector.length; i+= 128) {
|
||||
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 = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
L: blob.read_shift(4, 'i'),
|
||||
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);
|
||||
}
|
||||
o.start = blob.read_shift(4, 'i');
|
||||
o.size = blob.read_shift(4, 'i');
|
||||
if(o.type === 5) { /* root */
|
||||
minifat_store = o.start;
|
||||
if(nmfs > 0 && minifat_store !== ENDOFCHAIN) sector_list[minifat_store].name = "!StreamData";
|
||||
/*minifat_size = o.size;*/
|
||||
} else if(o.size >= 4096 /* MSCSZ */) {
|
||||
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);
|
||||
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);
|
||||
prep_blob(o.content, 0);
|
||||
}
|
||||
}
|
||||
files[name] = o;
|
||||
FileIndex.push(o);
|
||||
}
|
||||
}
|
||||
|
||||
function read_date(blob, offset) {
|
||||
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');
|
||||
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);
|
||||
}
|
||||
return parse(blob);
|
||||
}
|
||||
|
||||
/** CFB Constants */
|
||||
var MSSZ = 64; /* Mini Sector Size = 1<<6 */
|
||||
//var MSCSZ = 4096; /* Mini Stream Cutoff Size */
|
||||
/* 2.1 Compound File Sector Numbers and Types */
|
||||
var ENDOFCHAIN = -2;
|
||||
/* 2.2 Compound File Header */
|
||||
var HEADER_SIGNATURE = 'd0cf11e0a1b11ae1';
|
||||
var HEADER_CLSID = '00000000000000000000000000000000';
|
||||
var consts = {
|
||||
/* 2.1 Compund File Sector Numbers and Types */
|
||||
MAXREGSECT: -6,
|
||||
DIFSECT: -4,
|
||||
FATSECT: -3,
|
||||
ENDOFCHAIN: ENDOFCHAIN,
|
||||
FREESECT: -1,
|
||||
/* 2.2 Compound File Header */
|
||||
HEADER_SIGNATURE: HEADER_SIGNATURE,
|
||||
HEADER_MINOR_VERSION: '3e00',
|
||||
MAXREGSID: -6,
|
||||
NOSTREAM: -1,
|
||||
HEADER_CLSID: HEADER_CLSID,
|
||||
/* 2.6.1 Compound File Directory Entry */
|
||||
EntryTypes: ['unknown','storage','stream','lockbytes','property','root']
|
||||
};
|
||||
|
||||
exports.read = readSync;
|
||||
exports.parse = parse;
|
||||
exports.utils = {
|
||||
ReadShift: ReadShift,
|
||||
CheckField: CheckField,
|
||||
prep_blob: prep_blob,
|
||||
bconcat: bconcat,
|
||||
consts: consts
|
||||
};
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }
|
11
xlscfb.js
11
xlscfb.js
@ -1,12 +1,12 @@
|
||||
var DO_NOT_EXPORT_CFB = true;
|
||||
/* cfb.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
|
||||
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
||||
/* vim: set ts=2: */
|
||||
/*jshint eqnull:true */
|
||||
|
||||
/* [MS-CFB] v20130118 */
|
||||
var CFB = (function _CFB(){
|
||||
var exports = {};
|
||||
exports.version = '0.10.2';
|
||||
exports.version = '0.10.3';
|
||||
function parse(file) {
|
||||
var mver = 3; // major version
|
||||
var ssz = 512; // sector size
|
||||
@ -216,6 +216,7 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) {
|
||||
if(cnt !== 0) throw "DIFAT chain shorter than expected";
|
||||
} else if(idx !== -1 /*FREESECT*/) {
|
||||
var sector = sectors[idx], m = (ssz>>>2)-1;
|
||||
if(!sector) return;
|
||||
for(var i = 0; i < m; ++i) {
|
||||
if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break;
|
||||
fat_addrs.push(q);
|
||||
@ -239,6 +240,7 @@ function get_sector_list(sectors, start, fat_addrs, ssz, chkd) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
return {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -261,6 +263,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) {
|
||||
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(!sectors[addr]) break;
|
||||
j = __readInt32LE(sectors[addr], jj);
|
||||
}
|
||||
sector_list[k] = {nodes: buf, data:__toBuffer([buf_chain])};
|
||||
@ -281,7 +284,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
if(namelen === 0) continue;
|
||||
name = __utf16le(blob,0,namelen-pl);
|
||||
Paths.push(name);
|
||||
o = {
|
||||
o = ({
|
||||
name: name,
|
||||
type: blob.read_shift(1),
|
||||
color: blob.read_shift(1),
|
||||
@ -290,7 +293,7 @@ function read_directory(dir_start, sector_list, sectors, Paths, nmfs, files, Fil
|
||||
C: blob.read_shift(4, 'i'),
|
||||
clsid: blob.read_shift(16),
|
||||
state: blob.read_shift(4, 'i')
|
||||
};
|
||||
});
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user