package ole;
import pptViewer.PptException;
/**
* File Allocation Table der Datei
* @author Michael Roth <michi.ro@gmx.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
public class FAT {
private OleFile file;
int sectorsPerFat;
int fatPerDif;
public FAT(OleFile oleFile) {
this.file = oleFile;
sectorsPerFat=this.file.getHeader().getSecSize()/4;
fatPerDif=this.file.getHeader().getSecSize()/4-1;
}
/**
* Eintrag in der FAT zu dem gesuchten Sektor
* @param block Der aktuelle Sektor
* @return Position des nachfolgenden Sektors
* @throws PptException Falls die Sektorkette mit dem aktuellen Block endet
*/
public int getNextBlock(int block) throws PptException {
byte[] data;
int offset;
int FatBlock = block/sectorsPerFat;
//System.out.println("Looking for next block for "+block);
data = this.getFatBlock(FatBlock);
offset=(block-(FatBlock*sectorsPerFat))*4;
int sector= (data[offset]& 0x0FF)|((data[offset+1]& 0x0FF)<<8)|((data[offset+2]& 0x0FF)<<16)|((data[offset+3]& 0x0FF)<<24);
if (sector == OleFile.ENDOFCHAIN) throw new PptException("EndOfChain");
if (sector == OleFile.DIFSECT) throw new PptException("DifSector");
if (sector == OleFile.FATSECT) throw new PptException("FatSector");
if (sector == OleFile.FREESECT) throw new PptException("FreeSector");
return sector;
}
/**
* Liefert den FAT-Sektor
* @param block Nummer des FAT-Sektors
* @return Der Sektor
* @throws PptException
*/
private byte[] getFatBlock(int block) throws PptException {
byte[] data;
int FatSector;
int offset;
// Bestimmt, ob die Position des FAT-Sektors aus dem Header oder der DIF gelesen werden muss
if (block < 109) {
// 1. Fall: Header
offset= block*4;
data = this.file.getHeader().getFat();
} else {
// 2. Fall: DIF
block-=109;
int DifBlock = block/fatPerDif;
data = this.file.getDif().getDifBlock(DifBlock);
offset=(block-(DifBlock*fatPerDif))*4;
}
/*
System.out.println(" "+(offset)+": "+data[offset]);
System.out.println(" "+(offset+1)+": "+data[offset+1]);
System.out.println(" "+(offset+2)+": "+data[offset+2]);
System.out.println(" "+(offset+3)+": "+data[offset+3]);
*/
FatSector=(data[offset]& 0x0FF)|((data[offset+1]& 0x0FF)<<8)|((data[offset+2]& 0x0FF)<<16)|((data[offset+3]& 0x0FF)<<24);
//System.out.println("Loading FAT-Sector "+FatSector);
// Liest den entsprechenden Sektor aus der Datei
return this.file.getBlock(FatSector);
}
}