js-cfb/bits/61_layout.js

25 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-09-14 21:14:22 +00:00
var L = (function(cfb/*:CFBContainer*/)/*:Array<number>*/{
var mini_size = 0, fat_size = 0;
for(var i = 0; i < cfb.FileIndex.length; ++i) {
var file = cfb.FileIndex[i];
if(!file.content) continue;
/*:: if(file.content == null) throw new Error("unreachable"); */
var flen = file.content.length;
2018-01-19 01:09:51 +00:00
if(flen > 0){
if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6;
else fat_size += (flen + 0x01FF) >> 9;
}
2017-09-14 21:14:22 +00:00
}
var dir_cnt = (cfb.FullPaths.length +3) >> 2;
var mini_cnt = (mini_size + 7) >> 3;
var mfat_cnt = (mini_size + 0x7F) >> 7;
var fat_base = mini_cnt + fat_size + dir_cnt + mfat_cnt;
var fat_cnt = (fat_base + 0x7F) >> 7;
var difat_cnt = fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
while(((fat_base + fat_cnt + difat_cnt + 0x7F) >> 7) > fat_cnt) difat_cnt = ++fat_cnt <= 109 ? 0 : Math.ceil((fat_cnt-109)/0x7F);
var L = [1, difat_cnt, fat_cnt, mfat_cnt, dir_cnt, fat_size, mini_size, 0];
cfb.FileIndex[0].size = mini_size << 6;
L[7] = (cfb.FileIndex[0].start=L[0]+L[1]+L[2]+L[3]+L[4]+L[5])+((L[6]+7) >> 3);
return L;
})(cfb);