From 1611d164a8cc643be1c767285bbd255db68187d4 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 4 Jan 2006 00:32:03 +0000 Subject: [PATCH] Incorporated code for computing the CRC32 hash function. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1750 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/util/CRC32.java | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/util/CRC32.java diff --git a/projects/samskivert/src/java/com/samskivert/util/CRC32.java b/projects/samskivert/src/java/com/samskivert/util/CRC32.java new file mode 100644 index 00000000..2f6d31db --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/CRC32.java @@ -0,0 +1,141 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import java.math.*; + +/** + *

Calculates the CRC32 - 32 bit Cyclical Redundancy Check. This check is + * used in numerous systems to verify the integrity of information. It's also + * used as a hashing function. Unlike a regular checksum, it's sensitive to + * the order of the characters. It produces a 32 bit Java int. + * + *

This Java programme was translated from a C version I had written. This + * software is in the public domain. + * + *

When calculating the CRC32 over a number of strings or byte arrays the + * previously calculated CRC is passed to the next call. In this way the CRC + * is built up over a number of items, including a mix of strings and byte + * arrays. + * + *

+ * int crcCalc = CRC32.crc32("Hello World");
+ * crcCalc = CRC32.crc32("How are you?", crcCalc);
+ * crcCalc = CRC32.crc32("I'm feeling really good, how about you?", crcCalc);
+ * 
+ * + *

The line int crcCalc = CRC32.crc32("Hello World"); is + * equivalent to int crcCalc = CRC32.crc32("Hello World", -1);. + * When starting a new CRC calculation the "previous crc" is set to 0xFFFFFFFF + * (or -1). + * + * @author Michael Lecuyer (mjl@theorem.com) + * @author Michael Bayne (slight modifications) + */ +public class CRC32 +{ + /** + * Convenience mithod for generating a CRC from a single + * String. + * + * @param buffer string to generate the CRC32. + * @return 32 bit CRC. + */ + public static int crc32 (String buffer) + { + return crc32(buffer, 0xFFFFFFFF); + } + + /** + * Convenience method for generating a CRC from a byte array. + * + * @param buffer byte array to generate the CRC32. + * @return 32 bit CRC. + */ + public static int crc32 (byte buffer[]) + { + return crc32(buffer, 0xFFFFFFFF); + } + + /** + * Convenience method for generating a CRC from a series of + * String's. + * + * @param buffer string to generate the CRC32. + * @param crc previously generated CRC32. + * @return 32 bit CRC. + */ + public static int crc32 (String buffer, int crc) + { + return crc32(buffer.getBytes(), crc); + } + + /** + * Convenience method for generating a CRC from a series of + * byte arrays. + * + * @param buffer byte array to generate the CRC32 + * @param crc previously generated CRC32. + * @return 32 bit CRC. + */ + public static int crc32 (byte buffer[], int crc) + { + return crc32(buffer, 0, buffer.length, crc); + } + + /** + * General CRC generation function. + * + * @param buffer byte array to generate the CRC32. + * @param start byte start position. + * @param count number of byte's to include in CRC calculation. + * @param lastcrc previously generated CRC32. + * @return 32 bit CRC + */ + public static int crc32 (byte buffer[], int start, int count, int lastcrc) + { + int crc = lastcrc; + for (int ii = start; count-- != 0; ii++) { + int temp1 = crc >>> 8; + int temp2 = _crcTable[(crc ^ buffer[ii]) & 0xFF]; + crc = temp1 ^ temp2; + } + return crc; + } + + protected static int[] _crcTable = new int[256]; + protected static final int CRC32_POLYNOMIAL = 0xEDB88320; + + static { + int crc; + for (int ii = 0; ii <= 255; ii++) { + crc = ii; + for (int jj = 8; jj > 0; jj--) { + if ((crc & 1) == 1) { + crc = (crc >>> 1) ^ CRC32_POLYNOMIAL; + } else { + crc >>>= 1; + } + } + _crcTable[ii] = crc; + } + } +}