Java CRC Calculate crc32(int[] table, int crc, byte[] buffer, int off, int len)

Here you can find the source of crc32(int[] table, int crc, byte[] buffer, int off, int len)

Description

This function computes / refreshes a crc32 checksum.

License

Open Source License

Parameter

Parameter Description
table this is the precomputed crc32 table
crc set this value to zero if starting the computation or to the last retrieved value when processing further data.
buffer the buffer to be walked
off the offset to start
len the amount of bytes to process

Return

the new/refreshed crc checksum

Declaration

public static int crc32(int[] table, int crc, byte[] buffer, int off, int len) 

Method Source Code

//package com.java2s;
/*//  w  w w.  j  a  v  a  2s . c  o m
 * Created on Jun 25, 2007 at 11:12:23 AM.
 * 
 * Copyright (c) 2010 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

public class Main {
    /**
     *  This function computes / refreshes a crc32 checksum. 
     *  
     * @param table this is the precomputed crc32 table
     * @param crc set this value to zero if starting the computation
     *             or to the last retrieved value when processing 
     *             further data.
     * @param buffer the buffer to be walked
     * @param off the offset to start
     * @param len the amount of bytes to process
     * @return the new/refreshed crc checksum
     */
    public static int crc32(int[] table, int crc, byte[] buffer, int off, int len) {
        if (table[2] == 0) {
            initCrc32Table(table);
        }

        crc = crc ^ 0xffffffff;

        for (int n = 0; n < len; n++) {
            crc = table[(crc ^ buffer[n + off]) & 0xff] ^ (crc >>> 8);
        }

        return 0xffffffff ^ crc;

    }

    /**
     * In order to compute the CRC checksum fast it is crucial to 
     * have a precomputed table.
     * 
     * @param table store the table here
     */
    private static void initCrc32Table(int[] table) {
        int c;

        int n, k;
        for (n = 0; n < 256; n++) {
            c = n;
            for (k = 0; k < 8; k++) {
                if ((c & 1) == 1) {
                    c = 0xedb88320 ^ (c >>> 1);
                } else {
                    c >>>= 1;
                }
            }
            table[n] = c;
        }
    }
}

Related

  1. crc16(final byte[] bytes)
  2. crc16_get(byte aby[], int nOffset, int nLength)
  3. CRC16_TABLE(byte[] aData, int aSize)
  4. crc32(byte[] array, int offset, int size)
  5. CRC32(final byte[] buf, final int startPos, final int endPos)
  6. crc32(long crcin, byte[] buf, int off, int alen)
  7. crc32(String input)
  8. crc320(byte[] buf, int off, int len)
  9. crc321(byte[] buf, int off, int len)