Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/Crc16.java
8650 views
1
/*
2
* 11/19/04 : 1.0 moved to LGPL.
3
*
4
* 02/12/99 : Java Conversion by E.B , [email protected]
5
*
6
* @(#) crc.h 1.5, last edit: 6/15/94 16:55:32
7
* @(#) Copyright (C) 1993, 1994 Tobias Bading ([email protected])
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
*/
25
package javazoom.jl.decoder;
26
27
/**
28
* 16-Bit CRC checksum
29
*/
30
public 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
}
71
72