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