2014-06-24 04:00:39 +00:00
|
|
|
/** 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 */
|
2017-07-28 17:53:08 +00:00
|
|
|
function sleuth_fat(idx/*:number*/, cnt/*:number*/, sectors/*:Array<RawBytes>*/, ssz/*:number*/, fat_addrs)/*:void*/ {
|
|
|
|
var q/*:number*/ = ENDOFCHAIN;
|
2014-06-24 04:00:39 +00:00
|
|
|
if(idx === ENDOFCHAIN) {
|
2017-03-30 21:34:37 +00:00
|
|
|
if(cnt !== 0) throw new Error("DIFAT chain shorter than expected");
|
2014-06-24 04:00:39 +00:00
|
|
|
} else if(idx !== -1 /*FREESECT*/) {
|
|
|
|
var sector = sectors[idx], m = (ssz>>>2)-1;
|
2017-02-24 05:11:45 +00:00
|
|
|
if(!sector) return;
|
2014-06-24 04:00:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 04:02:42 +00:00
|
|
|
/** Follow the linked list of sectors for a given starting point */
|
2017-07-28 17:53:08 +00:00
|
|
|
function get_sector_list(sectors/*:Array<RawBytes>*/, start/*:number*/, fat_addrs/*:Array<number>*/, ssz/*:number*/, chkd/*:?Array<boolean>*/)/*:SectorEntry*/ {
|
2014-11-03 04:02:42 +00:00
|
|
|
var sl = sectors.length;
|
2017-07-28 17:53:08 +00:00
|
|
|
var buf/*:Array<number>*/ = [], buf_chain/*:Array<any>*/ = [];
|
|
|
|
if(!chkd) chkd = [];
|
|
|
|
var modulus = ssz - 1, j = 0, jj = 0;
|
2014-11-03 04:02:42 +00:00
|
|
|
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);
|
2017-03-30 21:34:37 +00:00
|
|
|
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
|
2017-02-24 05:11:45 +00:00
|
|
|
if(!sectors[addr]) break;
|
2014-11-03 04:02:42 +00:00
|
|
|
j = __readInt32LE(sectors[addr], jj);
|
|
|
|
}
|
|
|
|
return {nodes: buf, data:__toBuffer([buf_chain])};
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:00:39 +00:00
|
|
|
/** Chase down the sector linked lists */
|
2017-07-28 17:53:08 +00:00
|
|
|
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;
|
2014-06-24 04:00:39 +00:00
|
|
|
for(i=0; i < sl; ++i) {
|
2017-07-28 17:53:08 +00:00
|
|
|
buf = ([]/*:Array<number>*/);
|
2014-06-24 04:00:39 +00:00
|
|
|
k = (i + dir_start); if(k >= sl) k-=sl;
|
2017-07-28 17:53:08 +00:00
|
|
|
if(chkd[k]) continue;
|
2014-06-24 04:00:39 +00:00
|
|
|
buf_chain = [];
|
|
|
|
for(j=k; j>=0;) {
|
|
|
|
chkd[j] = true;
|
|
|
|
buf[buf.length] = j;
|
|
|
|
buf_chain.push(sectors[j]);
|
2017-07-28 17:53:08 +00:00
|
|
|
var addr/*:number*/ = fat_addrs[Math.floor(j*4/ssz)];
|
2014-06-24 04:00:39 +00:00
|
|
|
jj = ((j*4) & modulus);
|
2017-03-30 21:34:37 +00:00
|
|
|
if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz);
|
2017-02-24 05:11:45 +00:00
|
|
|
if(!sectors[addr]) break;
|
2014-06-24 04:00:39 +00:00
|
|
|
j = __readInt32LE(sectors[addr], jj);
|
|
|
|
}
|
2017-07-28 17:53:08 +00:00
|
|
|
sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/);
|
2014-06-24 04:00:39 +00:00
|
|
|
}
|
|
|
|
return sector_list;
|
|
|
|
}
|
|
|
|
|