Skip to content

Commit

Permalink
Check volume header signature when creating HFSVolume derivates.
Browse files Browse the repository at this point in the history
This sanity check prevents us from accidentally loading the wrong HFS
type. Helps us to catch errors early.
  • Loading branch information
unsound committed Jan 9, 2015
1 parent 8e69da9 commit 2d2b3b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/java/org/catacombae/hfs/original/HFSOriginalVolume.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ public HFSOriginalVolume(ReadableRandomAccessStream hfsFile,

super(hfsFile, cachingEnabled);

final MasterDirectoryBlock mdb = getHFSMasterDirectoryBlock();
if(mdb.getDrSigWord() != MasterDirectoryBlock.SIGNATURE_HFS) {
throw new RuntimeException("Invalid volume header signature " +
"(expected: 0x" +
Util.toHexStringBE(MasterDirectoryBlock.SIGNATURE_HFS) +
" actual: 0x" + Util.toHexStringBE(mdb.getDrSigWord()) +
").");
}

this.stringCodec = new MutableStringCodec<CharsetStringCodec>(
new CharsetStringCodec(encodingName));

this.allocationFile = createAllocationFile();
}

public MasterDirectoryBlock getHFSMasterDirectoryBlock() {
public final MasterDirectoryBlock getHFSMasterDirectoryBlock() {
byte[] currentBlock = new byte[512];
hfsFile.readFrom(1024, currentBlock);
return new MasterDirectoryBlock(currentBlock, 0);
Expand Down
17 changes: 15 additions & 2 deletions src/java/org/catacombae/hfs/plus/HFSPlusVolume.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,26 @@ public class HFSPlusVolume extends HFSVolume {

public HFSPlusVolume(ReadableRandomAccessStream hfsFile,
boolean cachingEnabled) {
this(hfsFile, cachingEnabled, HFSPlusVolumeHeader.SIGNATURE_HFS_PLUS);
}

protected HFSPlusVolume(ReadableRandomAccessStream hfsFile,
boolean cachingEnabled, short volumeHeaderSignature)
{
super(hfsFile, cachingEnabled);

final HFSPlusVolumeHeader volumeHeader = getHFSPlusVolumeHeader();
if(volumeHeader.getSignature() != volumeHeaderSignature) {
throw new RuntimeException("Invalid volume header signature " +
"(expected: 0x" +
Util.toHexStringBE(volumeHeaderSignature) + " actual: 0x" +
Util.toHexStringBE(volumeHeader.getSignature()) + ").");
}

this.allocationFile = createAllocationFile();
this.journal = new HFSPlusJournal(this);

if(getHFSPlusVolumeHeader().getAttributesFile().getExtents().
if(volumeHeader.getAttributesFile().getExtents().
getExtentDescriptors()[0].getBlockCount() == 0)
{
/* TODO: Is this even valid? */
Expand All @@ -86,7 +99,7 @@ SynchronizedReadableRandomAccess getBackingStream() {
return hfsFile;
}

public HFSPlusVolumeHeader getHFSPlusVolumeHeader() {
public final HFSPlusVolumeHeader getHFSPlusVolumeHeader() {
//System.err.println("getHFSPlusVolumeHeader()");
byte[] currentBlock = new byte[512];
//System.err.println(" hfsFile.seek(" + (fsOffset + 1024) + ")");
Expand Down
3 changes: 2 additions & 1 deletion src/java/org/catacombae/hfs/x/HFSXVolume.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.catacombae.hfs.types.hfsx.HFSXCatalogKey;
import org.catacombae.io.ReadableRandomAccessStream;
import org.catacombae.hfs.plus.HFSPlusVolume;
import org.catacombae.hfs.types.hfsplus.HFSPlusVolumeHeader;

/**
* @author <a href="http://www.catacombae.org/" target="_top">Erik Larsson</a>
Expand All @@ -40,7 +41,7 @@ public class HFSXVolume extends HFSPlusVolume {
public HFSXVolume(ReadableRandomAccessStream hfsFile,
boolean cachingEnabled) {

super(hfsFile, cachingEnabled);
super(hfsFile, cachingEnabled, HFSPlusVolumeHeader.SIGNATURE_HFSX);

CommonBTHeaderRecord.CompareType keyCompareTypeEnum =
getCatalogFile().getCatalogHeaderNode().getHeaderRecord().
Expand Down

0 comments on commit 2d2b3b4

Please sign in to comment.