]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/tools/misc/src/com/ibm/icu/dev/tool/localeconverter/CalculateCRC32.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / tools / misc / src / com / ibm / icu / dev / tool / localeconverter / CalculateCRC32.java
1 /*\r
2 ******************************************************************************\r
3 * Copyright (C) 2003-2010, International Business Machines Corporation and   *\r
4 * others. All Rights Reserved.                                               *\r
5 ******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.dev.tool.localeconverter;\r
9 \r
10 import java.math.BigInteger;\r
11 \r
12 /*\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
19  * \r
20  * @author Michael Lecuyer (mjl@theorem.com)\r
21  * @version 1.1 August 11, 1998\r
22  */\r
23  \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
27  */\r
28  \r
29 public class CalculateCRC32 {\r
30     static int CRCTable[];\r
31     static int cachedCRC;\r
32 \r
33     static void buildCRCTable() {\r
34         final int CRC32_POLYNOMIAL = 0xEDB88320;\r
35         int i, j;\r
36         int crc;\r
37         CRCTable = new int[256];\r
38 \r
39         for (i = 0; i <= 255; i++) {\r
40             crc = i;\r
41             for (j = 8; j > 0; j--) {\r
42                 if ((crc & 1) == 1) {\r
43                     crc = (crc >>> 1) ^ CRC32_POLYNOMIAL;\r
44                 } else {\r
45                     crc >>>= 1;\r
46                 }\r
47             }\r
48             CRCTable[i] = crc;\r
49         }\r
50     }\r
51 \r
52     public static int computeCRC32(String buffer) {\r
53         return computeCRC32(buffer, 0xFFFFFFFF);\r
54     }\r
55 \r
56     public static int computeCRC32(byte buffer[]) {\r
57         return computeCRC32(buffer, 0xFFFFFFFF);\r
58     }\r
59    \r
60     public static int computeCRC32(String buffer, int crc){\r
61         return computeCRC32(buffer.getBytes(), crc);\r
62     }\r
63 \r
64     public static int computeCRC32(byte buffer[], int crc) {\r
65         return computeCRC32(buffer, 0, buffer.length, crc);\r
66     }\r
67 \r
68     public static int computeCRC32(byte buffer[], int start, int count, int lastcrc){\r
69         buildCRCTable();  \r
70         int temp1, temp2;\r
71         int i = start;\r
72         cachedCRC = lastcrc;\r
73       \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
79         }\r
80         return cachedCRC;\r
81     }\r
82 \r
83     public byte [] toBytes() {\r
84         return new BigInteger(new Integer(cachedCRC).toString()).toByteArray();\r
85     }\r
86 }\r