1 /*
2 * This file is part of mpck, a program to check MP3 files for errors
3 *
4 * Copyright (C) 2006 Sjoerd Langkemper
5 *
6 * mpck is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 *****************************************************************************
21 *
22 * crc.c - functions that compute the crc16 of some data
23 *
24 */
25
26 #include "crc.h"
27
28 /* from lame 3.93.1 */
29 static int
crc_update(int value,int crc)30 crc_update(int value, int crc)
31 {
32 int i;
33 value <<= 8;
34 for (i = 0; i < 8; i++) {
35 value <<= 1;
36 crc <<= 1;
37
38 if (((crc ^ value) & 0x10000))
39 crc ^= CRC16_POLYNOMIAL;
40 }
41 return crc;
42 }
43
44 int
crc16(data,length)45 crc16(data, length)
46 unsigned char * data;
47 int length;
48 {
49 int i;
50 int crc=0xffff;
51
52 for (i=0; i<length; i++)
53 crc=crc_update(data[i], crc);
54
55 return crc & 0xffff;
56 }
57
58 static int
crc_update_bits(int value,int crc,int nbits)59 crc_update_bits(int value, int crc, int nbits)
60 {
61 int i;
62 value <<= 8;
63 for (i = 0; i < nbits; i++) {
64 value <<= 1;
65 crc <<= 1;
66
67 if (((crc ^ value) & 0x10000))
68 crc ^= CRC16_POLYNOMIAL;
69 }
70 return crc;
71 }
72
73
crc16bits(data,nbits)74 int crc16bits(data, nbits)
75 unsigned char * data;
76 int nbits;
77 {
78 int i;
79 int crc=0xffff;
80 int nbytes = nbits >> 3;
81 nbits = nbits & 7;
82
83 for (i=0; i<nbytes; i++)
84 crc=crc_update_bits(data[i], crc, 8);
85
86 // i++;
87 crc=crc_update_bits(data[i], crc, nbits);
88 return crc & 0xffff;
89 }
90