1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 /*
4 ******************************************************************************
5 * Copyright (C) 2003-2010, International Business Machines Corporation and   *
6 * others. All Rights Reserved.                                               *
7 ******************************************************************************
8 */
9 
10 package com.ibm.icu.dev.tool.localeconverter;
11 
12 import java.math.BigInteger;
13 
14 /*
15  *  The code is from  http://www.theorem.com/java/CRC32.java
16  * Calculates the CRC32 - 32 bit Cyclical Redundancy Check
17  * <P> This check is used in numerous systems to verify the integrity
18  * of information.  It's also used as a hashing function.  Unlike a regular
19  * checksum, it's sensitive to the order of the characters.
20  * It produces a 32 bit
21  *
22  * @author Michael Lecuyer (mjl@theorem.com)
23  * @version 1.1 August 11, 1998
24  */
25 
26 /* ICU is not endian portable, because ICU data generated on big endian machines can be
27  * ported to big endian machines but not to little endian machines and vice versa. The
28  * conversion is not portable across platforms with different endianess.
29  */
30 
31 public class CalculateCRC32 {
32     static int CRCTable[];
33     static int cachedCRC;
34 
buildCRCTable()35     static void buildCRCTable() {
36         final int CRC32_POLYNOMIAL = 0xEDB88320;
37         int i, j;
38         int crc;
39         CRCTable = new int[256];
40 
41         for (i = 0; i <= 255; i++) {
42             crc = i;
43             for (j = 8; j > 0; j--) {
44                 if ((crc & 1) == 1) {
45                     crc = (crc >>> 1) ^ CRC32_POLYNOMIAL;
46                 } else {
47                     crc >>>= 1;
48                 }
49             }
50             CRCTable[i] = crc;
51         }
52     }
53 
computeCRC32(String buffer)54     public static int computeCRC32(String buffer) {
55         return computeCRC32(buffer, 0xFFFFFFFF);
56     }
57 
computeCRC32(byte buffer[])58     public static int computeCRC32(byte buffer[]) {
59         return computeCRC32(buffer, 0xFFFFFFFF);
60     }
61 
computeCRC32(String buffer, int crc)62     public static int computeCRC32(String buffer, int crc){
63         return computeCRC32(buffer.getBytes(), crc);
64     }
65 
computeCRC32(byte buffer[], int crc)66     public static int computeCRC32(byte buffer[], int crc) {
67         return computeCRC32(buffer, 0, buffer.length, crc);
68     }
69 
computeCRC32(byte buffer[], int start, int count, int lastcrc)70     public static int computeCRC32(byte buffer[], int start, int count, int lastcrc){
71         buildCRCTable();
72         int temp1, temp2;
73         int i = start;
74         cachedCRC = lastcrc;
75 
76         while (count-- != 0){
77             temp1 = cachedCRC >>> 8;
78             byte s = buffer[i++];
79             temp2 = CRCTable[(cachedCRC ^s) & 0xFF];
80             cachedCRC = temp1 ^ temp2;
81         }
82         return cachedCRC;
83     }
84 
toBytes()85     public byte [] toBytes() {
86         return new BigInteger(new Integer(cachedCRC).toString()).toByteArray();
87     }
88 }
89