-
Notifications
You must be signed in to change notification settings - Fork 2
/
crc.c
102 lines (74 loc) · 2.05 KB
/
crc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
crc -- MPEG crc functions
Copyright (C) 1998 by Johannes Overmann
Copyright (C) 2000 Dieter Baron
The crc implementation was taken from mp3check, adapted for use in
malint by Dieter Baron.
The original author can be contacted at <[email protected]>
This file is part of malint, an MPEG Audio stream validator.
The author can be contacted at <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "malint.h"
#include "mpeg.h"
#define MPEG_CRCPOLY 0x18005
static int crc_tab[256];
static void crc_update(int *crc, unsigned char *b, int n);
void
crc_init(void)
{
int i, x, j;
for(i=0; i<256; i++) {
x = i << 9;
for(j=0; j<8; j++, x<<=1)
if (x & 0x10000)
x ^= MPEG_CRCPOLY;
crc_tab[i] = x >> 1;
}
}
static void
crc_update(int *crc, unsigned char *b, int n)
{
int i;
for (i=0; i<n; i++)
*crc = crc_tab[(*crc>>8)^b[i]] ^ ((*crc<<8)&0xffff);
}
int
crc_frame(unsigned long h, unsigned char *data, int len)
{
int crc, n;
switch (MPEG_LAYER(h)) {
case 1:
switch (MPEG_MODE(h)) {
case MPEG_MODE_SINGLE:
n = 16;
break;
case MPEG_MODE_JSTEREO:
n = (32+MPEG_JSBOUND(h))/2;
break;
case MPEG_MODE_STEREO:
case MPEG_MODE_DUAL:
n = 32;
break;
}
break;
case 2:
return -1;
case 3:
n = MPEG_SILEN(h);
break;
}
crc = 0xffff;
crc_update(&crc, data+2, 2);
crc_update(&crc, data+6, n);
return crc;
}