01: /*
02: ******************************************************************************
03: * Copyright (C) 2003-2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: ******************************************************************************
06: */
07:
08: package com.ibm.icu.dev.tool.localeconverter;
09:
10: import java.math.*;
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 crc;
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,
69: int lastcrc) {
70: buildCRCTable();
71: int temp1, temp2;
72: int i = start;
73: crc = lastcrc;
74:
75: while (count-- != 0) {
76: temp1 = crc >>> 8;
77: byte s = buffer[i++];
78: temp2 = CRCTable[(crc ^ s) & 0xFF];
79: crc = temp1 ^ temp2;
80: }
81: return crc;
82: }
83:
84: public byte[] toBytes() {
85: return new BigInteger(new Integer(crc).toString())
86: .toByteArray();
87: }
88: }
|