summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/crcchecker/CrcFile.java
blob: 370c714ac52fc07dc65c8c939209960d56983a1f (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
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.Arrays;
import java.util.List;
import java.util.zip.CRC32;

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

	/**
	 * Loads a crcFile from file
	 * 
	 * @param filename
	 * @throws IOException
	 */
	public CrcFile( String filename ) throws IOException
	{
		File 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 ( Throwable t ) {
				}
		}
	}

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

	public CrcFile( List<Integer> crcSumsWithLeadingMasterCrc )
	{
		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 ) throws IOException
	{
		File file = new File( filename );

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

		FileOutputStream fos = new FileOutputStream( file );
		DataOutputStream dos = null;
		try {
			dos = new DataOutputStream( fos );
			dos.writeInt( Integer.reverseBytes( masterCrc ) );
			for ( int sum : crcSums ) {
				dos.writeInt( Integer.reverseBytes( sum ) );
			}
		} finally {
			if ( dos != null )
				dos.close();
		}
	}

	/**
	 * 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;
	}

	/**
	 * 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 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;
	}

}