2 ******************************************************************************
\r
3 * Copyright (C) 2003-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
8 package com.ibm.icu.dev.tool.localeconverter;
\r
10 import java.math.BigInteger;
\r
13 * The code is from http://www.theorem.com/java/CRC32.java
\r
14 * Calculates the CRC32 - 32 bit Cyclical Redundancy Check
\r
15 * <P> This check is used in numerous systems to verify the integrity
\r
16 * of information. It's also used as a hashing function. Unlike a regular
\r
17 * checksum, it's sensitive to the order of the characters.
\r
18 * It produces a 32 bit
\r
20 * @author Michael Lecuyer (mjl@theorem.com)
\r
21 * @version 1.1 August 11, 1998
\r
24 /* ICU is not endian portable, because ICU data generated on big endian machines can be
\r
25 * ported to big endian machines but not to little endian machines and vice versa. The
\r
26 * conversion is not portable across platforms with different endianess.
\r
29 public class CalculateCRC32 {
\r
30 static int CRCTable[];
\r
31 static int cachedCRC;
\r
33 static void buildCRCTable() {
\r
34 final int CRC32_POLYNOMIAL = 0xEDB88320;
\r
37 CRCTable = new int[256];
\r
39 for (i = 0; i <= 255; i++) {
\r
41 for (j = 8; j > 0; j--) {
\r
42 if ((crc & 1) == 1) {
\r
43 crc = (crc >>> 1) ^ CRC32_POLYNOMIAL;
\r
52 public static int computeCRC32(String buffer) {
\r
53 return computeCRC32(buffer, 0xFFFFFFFF);
\r
56 public static int computeCRC32(byte buffer[]) {
\r
57 return computeCRC32(buffer, 0xFFFFFFFF);
\r
60 public static int computeCRC32(String buffer, int crc){
\r
61 return computeCRC32(buffer.getBytes(), crc);
\r
64 public static int computeCRC32(byte buffer[], int crc) {
\r
65 return computeCRC32(buffer, 0, buffer.length, crc);
\r
68 public static int computeCRC32(byte buffer[], int start, int count, int lastcrc){
\r
72 cachedCRC = lastcrc;
\r
74 while (count-- != 0){
\r
75 temp1 = cachedCRC >>> 8;
\r
76 byte s = buffer[i++];
\r
77 temp2 = CRCTable[(cachedCRC ^s) & 0xFF];
\r
78 cachedCRC = temp1 ^ temp2;
\r
83 public byte [] toBytes() {
\r
84 return new BigInteger(new Integer(cachedCRC).toString()).toByteArray();
\r