summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNils Schwabe2014-06-23 15:13:59 +0200
committerNils Schwabe2014-06-23 15:13:59 +0200
commitd6e0ea55cf0d9dc4fb61ff5950dac910d14387e0 (patch)
treea3acfb68cbdcc0c4f780f576313000ecc6052e59 /src
parentFix some issues with the ftp connection and the ftp user management (diff)
downloadmasterserver-d6e0ea55cf0d9dc4fb61ff5950dac910d14387e0.tar.gz
masterserver-d6e0ea55cf0d9dc4fb61ff5950dac910d14387e0.tar.xz
masterserver-d6e0ea55cf0d9dc4fb61ff5950dac910d14387e0.zip
Add CRCChecker
This class checks given blocks of image files against a given crc file
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java77
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java72
-rw-r--r--src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java48
3 files changed, 197 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
new file mode 100644
index 0000000..04f6a22
--- /dev/null
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCChecker.java
@@ -0,0 +1,77 @@
+package org.openslx.imagemaster.crcchecker;
+
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.zip.CRC32;
+
+import org.apache.log4j.Logger;
+
+
+public class CRCChecker
+{
+
+ private static Logger log = Logger.getLogger( CRCChecker.class );
+
+ /**
+ * Checks the CRC sum of given blocks of a given imageFile against a given crcFile.
+ * @param imageFile The imageFile to check
+ * @param crcFile The crcFile to check against
+ * @param blocks The blocks to check
+ * @return The blocks where the crc was right
+ */
+ public static List<Integer> checkCRC(String imageFile, String crcFile, List<Integer> blocks) throws IOException
+ {
+ List<Integer> result = new LinkedList<>();
+
+ final int blockSize = 16 * 1024 * 1024;
+
+ ImageFile image = new ImageFile( imageFile, blockSize );
+ CRCFile crc = new CRCFile( crcFile );
+
+ log.debug( "Checking image file: '" + imageFile + "' with crc file: '" + crcFile + "'");
+ try {
+ if (!crc.isValid()) return result; // TODO: this needs to be handled in another way
+ } catch (IOException e) {
+ throw new IOException( "Could not read CRC file", e );
+ }
+
+ // file is smaller than one block - no need to check crc yet
+ if (image.length() < blockSize) {
+ return result;
+ }
+ // check all blocks
+ for (Integer blockN : blocks) {
+ byte[] block;
+ try {
+ block = image.getBlock( blockN );
+ } catch ( IOException e ) {
+ throw new IOException( "Could not read image file", e );
+ }
+
+ if (block == null) continue; // some error occured (for example: someone tried to check a block that is not in the file)
+
+ // check this block with CRC32
+ // add this block to result, if check was ok with CRC file
+
+ CRC32 crcCalc = new CRC32();
+ crcCalc.update( block );
+ int crcSum = Integer.reverseBytes( (int) crcCalc.getValue() );
+ log.debug( "Got crc for block '" + blockN + "': '" + crcSum + "'" );
+ int crcSumFromFile;
+ try {
+ crcSumFromFile = crc.getCRCSum( blockN );
+ } catch ( IOException e ) {
+ throw new IOException( "Could not read CRC file", e );
+ }
+ log.debug( "And read crc from file: '" + crcSumFromFile + "'" );
+
+ if (crcSum == crcSumFromFile) result.add( blockN );
+ }
+
+ return result;
+ }
+}
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java
new file mode 100644
index 0000000..3a75b37
--- /dev/null
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java
@@ -0,0 +1,72 @@
+package org.openslx.imagemaster.crcchecker;
+
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.zip.CRC32;
+
+/**
+ * Represents a crcfile
+ * @author nils
+ *
+ */
+public class CRCFile
+{
+ private File file;
+ private List<Integer> crcSums = null;
+
+ CRCFile(String filename) {
+ this.file = new File( filename );
+ }
+
+ /**
+ * Checks if this crc file is valid.
+ * (If the crc over the file is equal to the first crc sum.)
+ * @return Whether the crc file is valid
+ * @throws IOException If the file could not be read or could not be found
+ */
+ public boolean isValid() throws IOException {
+ FileInputStream fis = new FileInputStream( file );
+ DataInputStream dis = new DataInputStream( fis );
+
+ int crcSum = dis.readInt();
+
+ CRC32 crcCalc = new CRC32();
+
+ byte[] bytes = new byte[(int) file.length() - Integer.SIZE/8]; // byte array with length of the file minus the first crc sum (=4byte)
+ fis.read( bytes );
+ crcCalc.update( bytes );
+
+ dis.close();
+
+ if (crcSum == Integer.reverseBytes( (int) crcCalc.getValue() ) ) return true;
+ else return false;
+ }
+
+ /**
+ * Get a specified crcSum for a block number
+ * @param blockNumber
+ * @return The crcSum or 0 if the block number is invalid
+ * @throws IOException
+ */
+ public int getCRCSum(int blockNumber) throws IOException {
+ if (crcSums == null) {
+ // the crcSum were not read yet
+ DataInputStream dis = new DataInputStream( new FileInputStream( file ) );
+ crcSums = new ArrayList<>();
+ for (int i = 0; i < file.length()/4; i++) {
+ int s = dis.readInt();
+ if (i > 0) crcSums.add( s ); // skip the first crcSum because it's the sum over the crcFile
+ }
+ dis.close();
+ }
+
+ if (blockNumber < 0) return 0;
+ if (blockNumber > crcSums.size() - 1) return 0;
+
+ return crcSums.get( blockNumber );
+ }
+}
diff --git a/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
new file mode 100644
index 0000000..a6ff8f0
--- /dev/null
+++ b/src/main/java/org/openslx/imagemaster/crcchecker/ImageFile.java
@@ -0,0 +1,48 @@
+package org.openslx.imagemaster.crcchecker;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+/**
+ * Representing an image file.
+ * Is able to return certain blocks of this file.
+ * @author nils
+ *
+ */
+public class ImageFile
+{
+ private File file;
+ private int blockSize;
+
+ public ImageFile(String filename, int blockSize) {
+ this.file = new File( filename );
+ this.blockSize = blockSize;
+ }
+
+ /**
+ * Get a certain block
+ * @param block The number of the block you want to get
+ * @return The specified block
+ * @throws IOException When file was not found or could not be read
+ */
+ public byte[] getBlock(int block) throws IOException {
+ if (block < 0) return null;
+ if (block > file.length()/blockSize) return null;
+
+ FileInputStream fis = new FileInputStream( file );
+
+ fis.skip( block * (long) blockSize );
+
+ byte[] result = new byte[ ( blockSize > fis.available() )? fis.available() : blockSize ]; // only read availible bytes
+
+ fis.read(result);
+ fis.close();
+
+ return result;
+ }
+
+ public long length() {
+ return file.length();
+ }
+}