summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker/CRCFile.java
blob: 7d059fc44fbb26df8a906cac0eca3b459dd61b82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.openslx.imagemaster.crcchecker;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;

/**
 * Represents a crc file
 */
public class CRCFile
{
	private File file;
	private List<Integer> crcSums = null;

	/**
	 * Loads a crcFile from file
	 * 
	 * @param filename
	 */
	public CRCFile( String filename )
	{
		this.file = new File( filename );
	}

	/**
	 * Creates a new crc file with the given sums.
	 * The first crc sum in the list needs to be the sum over the other sums.
	 * 
	 * @param listOfCrcSums The list of the crc sums that are going into the crc file
	 * @param filename Where to save the created crc file
	 * @throws IOException If it's not possible to write the file
	 */
	public static CRCFile writeCrcFile( List<Integer> listOfCrcSums, String filename ) throws IOException
	{
		File file = new File( filename );
		FileOutputStream fos = new FileOutputStream( file );
		DataOutputStream dos = new DataOutputStream( fos );

		for ( Integer sum : listOfCrcSums ) {
			dos.writeInt( sum.intValue() );		// save byte-reversed integers to match right order in crc file	TODO: is that right?
		}

		dos.close();
		return new CRCFile( filename );
	}

	/**
	 * Checks if given sums are valid.
	 * @param listOfCrcSums
	 * @return
	 */
	public static boolean sumsAreValid( List<Integer> listOfCrcSums )
	{
		byte[] bytes = new byte[ ( listOfCrcSums.size() - 1 ) * Integer.SIZE / 8 ];
		int masterSum = listOfCrcSums.remove( 0 );
		for ( int i = 0; i < bytes.length; i++ ) {
			bytes[i] = listOfCrcSums.remove( 0 ).byteValue();
		}
		CRC32 crcCalc = new CRC32();
		crcCalc.update( bytes );
		return ( masterSum == Integer.reverseBytes( (int)crcCalc.getValue() ) );
	}

	/**
	 * 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 If the crcSums could not be loaded from file
	 */
	public int getCRCSum( int blockNumber ) throws IOException
	{
		if ( crcSums == null )
			loadSums();

		if ( blockNumber < 0 )
			return 0;
		if ( blockNumber > crcSums.size() - 1 )
			return 0;

		return crcSums.get( blockNumber );
	}

	/**
	 * Returns the loaded crcSums.
	 * 
	 * @return The loaded crcSums
	 * @throws IOException If the crcSums could not be loaded from file
	 */
	public List<Integer> getCrcSums() throws IOException
	{
		if ( crcSums == null )
			loadSums();
		return this.crcSums;
	}

	private void loadSums() throws IOException
	{
		// the crcSums 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();
	}
}