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