PageRenderTime 86ms CodeModel.GetById 39ms app.highlight 3ms RepoModel.GetById 11ms app.codeStats 1ms

/src/javazoom/jl/decoder/Crc16.java

http://jshout.googlecode.com/
Java | 70 lines | 29 code | 4 blank | 37 comment | 5 complexity | 3502e6445441308a0913c86e1ffb739c MD5 | raw file
Possible License(s): GPL-2.0
 1/*
 2 * 11/19/04 : 1.0 moved to LGPL.
 3 * 
 4 * 02/12/99 : Java Conversion by E.B , javalayer@javazoom.net
 5 *
 6 *  @(#) crc.h 1.5, last edit: 6/15/94 16:55:32
 7 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 8 *  @(#) Berlin University of Technology
 9 *-----------------------------------------------------------------------
10 *   This program is free software; you can redistribute it and/or modify
11 *   it under the terms of the GNU Library General Public License as published
12 *   by the Free Software Foundation; either version 2 of the License, or
13 *   (at your option) any later version.
14 *
15 *   This program is distributed in the hope that it will be useful,
16 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *   GNU Library General Public License for more details.
19 *
20 *   You should have received a copy of the GNU Library General Public
21 *   License along with this program; if not, write to the Free Software
22 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *----------------------------------------------------------------------
24 */
25package javazoom.jl.decoder;
26
27/**
28 * 16-Bit CRC checksum
29 */
30public final class Crc16
31{
32  private static	short polynomial=(short)0x8005;
33  private			short crc;
34
35  /**
36   * Dummy Constructor
37   */
38  public Crc16()
39  { 
40  	crc = (short) 0xFFFF;
41  }
42
43  /**
44   * Feed a bitstring to the crc calculation (0 < length <= 32).
45   */
46  public void add_bits (int bitstring, int length)
47  {
48  	int bitmask = 1 << (length - 1);
49  	do
50	 if (((crc & 0x8000) == 0) ^ ((bitstring & bitmask) == 0 ))
51	 {
52		crc <<= 1;
53		crc ^= polynomial;
54	 }
55	 else
56		crc <<= 1;
57  	while ((bitmask >>>= 1) != 0);
58  }
59
60  /**
61   * Return the calculated checksum.
62   * Erase it for next calls to add_bits().
63   */
64  public short	checksum()
65  {
66    short sum = crc;
67    crc = (short) 0xFFFF;
68    return sum;
69  }
70}