Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/CRC32.java
8650 views
1
/* -*-mode:java; c-basic-offset:2; -*- */
2
/*
3
Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
4
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
7
8
1. Redistributions of source code must retain the above copyright notice,
9
this list of conditions and the following disclaimer.
10
11
2. Redistributions in binary form must reproduce the above copyright
12
notice, this list of conditions and the following disclaimer in
13
the documentation and/or other materials provided with the distribution.
14
15
3. The names of the authors may not be used to endorse or promote products
16
derived from this software without specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
/*
30
* This program is based on zlib-1.1.3, so all credit should go authors
31
* Jean-loup Gailly([email protected]) and Mark Adler([email protected])
32
* and contributors of zlib.
33
*/
34
35
package com.jcraft.jzlib;
36
37
final public class CRC32 implements Checksum {
38
39
/*
40
* The following logic has come from RFC1952.
41
*/
42
private int v = 0;
43
private static int[] crc_table = null;
44
static {
45
crc_table = new int[256];
46
for (int n = 0; n < 256; n++) {
47
int c = n;
48
for (int k = 8; --k >= 0; ) {
49
if ((c & 1) != 0)
50
c = 0xedb88320 ^ (c >>> 1);
51
else
52
c = c >>> 1;
53
}
54
crc_table[n] = c;
55
}
56
}
57
58
public void update (byte[] buf, int index, int len) {
59
int c = ~v;
60
while (--len >= 0)
61
c = crc_table[(c^buf[index++])&0xff]^(c >>> 8);
62
v = ~c;
63
}
64
65
public void reset(){
66
v = 0;
67
}
68
69
public void reset(long vv){
70
v = (int)(vv&0xffffffffL);
71
}
72
73
public long getValue(){
74
return (long)(v&0xffffffffL);
75
}
76
77
// The following logic has come from zlib.1.2.
78
private static final int GF2_DIM = 32;
79
static long combine(long crc1, long crc2, long len2){
80
long row;
81
long[] even = new long[GF2_DIM];
82
long[] odd = new long[GF2_DIM];
83
84
// degenerate case (also disallow negative lengths)
85
if (len2 <= 0)
86
return crc1;
87
88
// put operator for one zero bit in odd
89
odd[0] = 0xedb88320L; // CRC-32 polynomial
90
row = 1;
91
for (int n = 1; n < GF2_DIM; n++) {
92
odd[n] = row;
93
row <<= 1;
94
}
95
96
// put operator for two zero bits in even
97
gf2_matrix_square(even, odd);
98
99
// put operator for four zero bits in odd
100
gf2_matrix_square(odd, even);
101
102
// apply len2 zeros to crc1 (first square will put the operator for one
103
// zero byte, eight zero bits, in even)
104
do {
105
// apply zeros operator for this bit of len2
106
gf2_matrix_square(even, odd);
107
if ((len2 & 1)!=0)
108
crc1 = gf2_matrix_times(even, crc1);
109
len2 >>= 1;
110
111
// if no more bits set, then done
112
if (len2 == 0)
113
break;
114
115
// another iteration of the loop with odd and even swapped
116
gf2_matrix_square(odd, even);
117
if ((len2 & 1)!=0)
118
crc1 = gf2_matrix_times(odd, crc1);
119
len2 >>= 1;
120
121
// if no more bits set, then done
122
} while (len2 != 0);
123
124
/* return combined crc */
125
crc1 ^= crc2;
126
return crc1;
127
}
128
129
private static long gf2_matrix_times(long[] mat, long vec){
130
long sum = 0;
131
int index = 0;
132
while (vec!=0) {
133
if ((vec & 1)!=0)
134
sum ^= mat[index];
135
vec >>= 1;
136
index++;
137
}
138
return sum;
139
}
140
141
static final void gf2_matrix_square(long[] square, long[] mat) {
142
for (int n = 0; n < GF2_DIM; n++)
143
square[n] = gf2_matrix_times(mat, mat[n]);
144
}
145
146
public CRC32 copy(){
147
CRC32 foo = new CRC32();
148
foo.v = this.v;
149
return foo;
150
}
151
152
public static int[] getCRC32Table(){
153
int[] tmp = new int[crc_table.length];
154
System.arraycopy(crc_table, 0, tmp, 0, tmp.length);
155
return tmp;
156
}
157
}
158
159