-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathOggCrc.java
More file actions
121 lines (111 loc) · 5.84 KB
/
Copy pathOggCrc.java
File metadata and controls
121 lines (111 loc) · 5.84 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/******************************************************************************
* *
* Copyright (c) 1999-2003 Wimba S.A., All Rights Reserved. *
* *
* COPYRIGHT: *
* This software is the property of Wimba S.A. *
* This software is redistributed under the Xiph.org variant of *
* the BSD license. *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* - Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* - Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* - Neither the name of Wimba, the Xiph.org Foundation nor the names of *
* its contributors may be used to endorse or promote products derived *
* from this software without specific prior written permission. *
* *
* WARRANTIES: *
* This software is made available by the authors in the hope *
* that it will be useful, but without any warranty. *
* Wimba S.A. is not liable for any consequence related to the *
* use of the provided software. *
* *
* Class: OggCrc.java *
* *
* Author: Marc GIMPEL *
* Based on code by: Ross WILLIAMS *
* *
* Date: 20th April 2003 *
* *
******************************************************************************/
/* $Id$ */
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
* by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: code raw [Vorbis] packets into framed OggSquish stream and
decode Ogg streams back into raw packets
last mod: $Id$
note: The CRC code is directly derived from public domain code by
Ross Williams ([email protected]). See docs/framing.html
for details.
********************************************************************/
package javaforce.codec.speex;
/**
* Calculates the CRC checksum for Ogg packets.
* <p/>
* <p>Ogg uses the same generator polynomial as ethernet, although with an
* unreflected alg and an init/final of 0, not 0xffffffff.
*
* @author Jim Lawrence, helloNetwork.com
* @author Marc Gimpel, Wimba S.A. ([email protected])
* @version $Revision$
*/
public class OggCrc {
// TODO - implement java.util.zip.Checksum
/**
* CRC checksum lookup table
*/
private static final int[] crc_lookup;
static {
crc_lookup = new int[256];
for (int i = 0; i < crc_lookup.length; i++) {
int r = i << 24;
for (int j = 0; j < 8; j++) {
if ((r & 0x80000000) != 0) {
/* The same as the ethernet generator polynomial, although we use an
unreflected alg and an init/final of 0, not 0xffffffff */
r = (r << 1) ^ 0x04c11db7;
} else {
r <<= 1;
}
}
crc_lookup[i] = r;
}
}
/**
* Calculates the checksum on the given data, from the give offset and
* for the given length, using the given initial value.
* This allows on to calculate the checksum iteratively, by reinjecting the
* last returned value as the initial value when the function is called for
* the next data chunk.
* The initial value should be 0 for the first iteration.
*
* @param crc - the initial value
* @param data - the data
* @param offset - the offset at which to start calculating the checksum.
* @param length - the length of data over which to calculate the checksum.
* @return the checksum.
*/
public static int checksum(int crc,
final byte[] data,
int offset,
final int length) {
int end = offset + length;
for (; offset < end; offset++) {
crc = (crc << 8) ^ crc_lookup[((crc >>> 24) & 0xff) ^ (data[offset] & 0xff)];
}
return crc;
}
}