summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker/CrcFile.java
blob: 4a05471952918661c4ca638fa4d04433c62a66a0 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package org.openslx.imagemaster.crcchecker;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Represents a crc file
 */
public class CrcFile
{
	private final int masterCrc;
	private final int[] crcSums;
	private Boolean valid = null;
	private final File file;

	private static Logger log = LogManager.getLogger( CrcFile.class );

	/**
	 * Loads a crcFile from file
	 * 
	 * @param filename
	 * @throws IOException
	 */
	public CrcFile( String filename ) throws IOException
	{
		file = new File( filename );
		DataInputStream dis = null;
		try {
			dis = new DataInputStream( new FileInputStream( file ) );
			int numSums = (int) ( file.length() / 4 ) - 1;
			if ( numSums < 0 )
				throw new IOException( "Invalid crc file: " + filename );
			masterCrc = dis.readInt();
			int[] sums = new int[ numSums ];
			for ( int i = 0; i < numSums; i++ ) {
				sums[i] = dis.readInt();
			}
			crcSums = sums;
		} finally {
			if ( dis != null ) {
				try {
					dis.close();
				} catch ( IOException e ) {
				}
			}
		}
	}

	/**
	 * Creates a crc file which is not on the drive.
	 * 
	 * @param crcSums
	 */
	public CrcFile( int[] crcSumsWithLeadingMasterCrc )
	{
		this.file = null;
		this.masterCrc = crcSumsWithLeadingMasterCrc[0];
		this.crcSums = Arrays.copyOfRange( crcSumsWithLeadingMasterCrc, 1, crcSumsWithLeadingMasterCrc.length );
	}

	public CrcFile( List<Integer> crcSumsWithLeadingMasterCrc )
	{
		this.file = null;
		this.masterCrc = crcSumsWithLeadingMasterCrc.get( 0 );
		this.crcSums = new int[ crcSumsWithLeadingMasterCrc.size() - 1 ];
		for ( int i = 0; i < crcSums.length; i++ )
			crcSums[i] = crcSumsWithLeadingMasterCrc.get( i + 1 );
	}

	/**
	 * 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.
	 * Deletes existing files with the same name.
	 * 
	 * @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 void writeCrcFile( String filename )
	{
		File file = new File( filename );

		if ( file.exists() )
			file.delete();

		FileOutputStream fos = null;
		DataOutputStream dos = null;
		try {
			fos = new FileOutputStream( file );
		} catch ( FileNotFoundException e ) {
			log.error( "File " + filename + " not found.", e );
			return;
		}
		try {
			dos = new DataOutputStream( fos );
			dos.writeInt( Integer.reverseBytes( masterCrc ) );
			for ( int sum : crcSums ) {
				dos.writeInt( Integer.reverseBytes( sum ) );
			}
		} catch ( IOException e ) {
			log.error( "IOException", e );
			return;
		} finally {
			if ( dos != null )
				try {
					dos.close();
				} catch ( IOException e ) {
				}
		}
	}

	/**
	 * 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
	 */
	public boolean isValid()
	{
		if ( valid == null ) {
			if ( crcSums == null || crcSums.length < 1 )
				return false;

			int masterSum = crcSums[0];

			CRC32 crcCalc = new CRC32();
			byte[] buffer = new byte[ 4 ];

			for ( int i = 1; i < crcSums.length; i++ ) {
				crcCalc.update( intToByteArrayLittleEndian( crcSums[i], buffer ) ); // update the crc calculator with the next 4 bytes of the integer
			}

			valid = ( masterSum == Integer.reverseBytes( (int)crcCalc.getValue() ) );
		}
		return valid;
	}

	/**
	 * Delete the file that is backing this list of crc sums, if any.
	 */
	public void delete()
	{
		if ( file != null )
			file.delete();
	}

	/**
	 * 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 )
	{
		if ( crcSums == null || blockNumber < 0 || blockNumber >= crcSums.length )
			return 0;
		return crcSums[blockNumber];
	}

	public List<Integer> getCrcSums()
	{
		List<Integer> ret = new ArrayList<Integer>( crcSums.length );
		for ( int i = 0; i < crcSums.length; i++ )
			ret.add( crcSums[i] );
		return ret;
	}

	public int getMasterSum()
	{
		return masterCrc;
	}

	private static final byte[] intToByteArrayLittleEndian( int value, byte[] buffer )
	{
		buffer[3] = (byte) ( value >>> 24 );
		buffer[2] = (byte) ( value >>> 16 );
		buffer[1] = (byte) ( value >>> 8 );
		buffer[0] = (byte)value;
		return buffer;
	}

}