diff options
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java | 37 | ||||
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java | 40 |
2 files changed, 59 insertions, 18 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java index a096bf6..1fa7453 100644 --- a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java +++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java @@ -18,6 +18,7 @@ public class CRCFile { private File file = null; private List<Integer> crcSums = null; + private Boolean valid = null; /** * Loads a crcFile from file @@ -71,19 +72,16 @@ public class CRCFile { if ( listOfCrcSums == null || listOfCrcSums.isEmpty() ) return false; - int masterSum = listOfCrcSums.remove( 0 ); - byte[] bytesOfInt; + + int masterSum = listOfCrcSums.get( 0 ); // don't use the first sum for the calculation because it is the sum over the other sums int size = listOfCrcSums.size(); - byte[] bytes = new byte[ size * Integer.SIZE / 8 ]; - for ( int i = 0; i < size; i++ ) { - bytesOfInt = ByteBuffer.allocate( 4 ).putInt( listOfCrcSums.remove( 0 ) ).array(); // get the bytes of this integer - bytes[4 * i] = bytesOfInt[0]; - bytes[4 * i + 1] = bytesOfInt[1]; - bytes[4 * i + 2] = bytesOfInt[2]; - bytes[4 * i + 3] = bytesOfInt[3]; - } + CRC32 crcCalc = new CRC32(); - crcCalc.update( bytes ); + + for ( int i = 1; i < size; i++ ) { + crcCalc.update( ByteBuffer.allocate( 4 ).putInt( listOfCrcSums.get( i ) ).array() ); // update the crc calculator with the next 4 bytes of the integer + } + return ( masterSum == Integer.reverseBytes( (int)crcCalc.getValue() ) ); } @@ -96,13 +94,16 @@ public class CRCFile */ public boolean isValid() throws IOException { - if ( file == null ) { - return sumsAreValid( this.crcSums ); - } else { - if ( crcSums == null ) - loadSums(); - return sumsAreValid( this.crcSums ); + if ( valid == null ) { + if ( file == null ) { + valid = sumsAreValid( this.crcSums ); + } else { + if ( crcSums == null ) + loadSums(); + valid = sumsAreValid( this.crcSums ); + } } + return valid; } /** @@ -150,7 +151,7 @@ public class CRCFile } dis.close(); } - + public int getMasterSum() throws IOException { if ( crcSums == null ) diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java b/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java new file mode 100644 index 0000000..7d905a3 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/crcchecker/ClassTest.java @@ -0,0 +1,40 @@ +package org.openslx.imagemaster.crcchecker; + +import java.io.IOException; + +public class ClassTest +{ + public static void main( String[] args ) throws IOException + { + String filename = "/home/nils/win98-dp-demo-de.vmdk.r1"; + final int bs = 16 * 1024 * 1024; + + CRCFile f = new CRCFile( filename.concat( ".crc" ) ); + System.out.println( f.getMasterSum() ); + System.out.println( f.getCrcSums() ); + System.out.println( f.isValid() ); + + System.out.println( CRCFile.sumsAreValid( f.getCrcSums() ) ); + + ImageFile i = new ImageFile( filename, bs ); + + CRCChecker c = new CRCChecker( i, f ); + System.out.println( c.checkBlock( 0 ) ); + System.out.println( c.checkBlock( 1 ) ); + System.out.println( c.checkBlock( 2 ) ); + System.out.println( c.checkBlock( 3 ) ); + System.out.println( c.checkBlock( 4 ) ); + System.out.println( c.checkBlock( 5 ) ); + System.out.println( c.checkBlock( 6 ) ); + System.out.println( c.checkBlock( 7 ) ); + System.out.println( c.checkBlock( 8 ) ); + System.out.println( c.checkBlock( 9 ) ); + System.out.println( c.checkBlock( 10 ) ); + System.out.println( c.checkBlock( 11 ) ); + System.out.println( c.checkBlock( 12 ) ); + System.out.println( c.checkBlock( 13 ) ); + System.out.println( c.checkBlock( 14 ) ); + System.out.println( c.checkBlock( 15 ) ); + c.done(); + } +} |
