/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

  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. */
  25. package javazoom.jl.decoder;
  26. /**
  27. * 16-Bit CRC checksum
  28. */
  29. public final class Crc16
  30. {
  31. private static short polynomial=(short)0x8005;
  32. private short crc;
  33. /**
  34. * Dummy Constructor
  35. */
  36. public Crc16()
  37. {
  38. crc = (short) 0xFFFF;
  39. }
  40. /**
  41. * Feed a bitstring to the crc calculation (0 < length <= 32).
  42. */
  43. public void add_bits (int bitstring, int length)
  44. {
  45. int bitmask = 1 << (length - 1);
  46. do
  47. if (((crc & 0x8000) == 0) ^ ((bitstring & bitmask) == 0 ))
  48. {
  49. crc <<= 1;
  50. crc ^= polynomial;
  51. }
  52. else
  53. crc <<= 1;
  54. while ((bitmask >>>= 1) != 0);
  55. }
  56. /**
  57. * Return the calculated checksum.
  58. * Erase it for next calls to add_bits().
  59. */
  60. public short checksum()
  61. {
  62. short sum = crc;
  63. crc = (short) 0xFFFF;
  64. return sum;
  65. }
  66. }