FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
zlib-tiny/src-java/CRC32C.java at master · source-c/zlib-tiny · GitHub
source-c
/
zlib-tiny
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
2
Star
7
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
zlib-tiny
/
src-java
/
CRC32C.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
228 lines (206 loc) · 8.86 KB
Breadcrumbs
zlib-tiny
/
src-java
/
CRC32C.java
Copy path
File metadata and controls
228 lines (206 loc) · 8.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package
zlib_tiny
;
import
java
.
util
.
zip
.
Checksum
;
/**
* CRC32C checksum implementation with automatic hardware acceleration.
* <p>
* This class generates a CRC32C checksum as defined by RFC 3720 Section B.4.
* It automatically detects and uses the JDK's hardware-accelerated implementation
* (available in Java 9+) when present, falling back to a portable software
* implementation otherwise.
* <p>
* The hardware-accelerated path uses CPU intrinsics (SSE4.2 on x86, CRC extensions
* on ARM) and can be 30-40x faster than the software implementation for large data.
* <p>
* The software fallback is ported from Google's cloud package (2011) and works
* correctly on all platforms regardless of Java version.
*/
public
final
class
CRC32C
implements
Checksum
{
/**
* Indicates whether the JDK's hardware-accelerated CRC32C is available.
*/
private
static
final
boolean
JDK_CRC32C_AVAILABLE
;
/**
* The JDK CRC32C class, if available (null otherwise).
*/
private
static
final
Class
<?>
JDK_CRC32C_CLASS
;
static
{
Class
<?>
clazz
=
null
;
boolean
available
=
false
;
try
{
clazz
=
Class
.
forName
(
"java.util.zip.CRC32C"
);
// Verify we can instantiate it
clazz
.
getDeclaredConstructor
().
newInstance
();
available
=
true
;
}
catch
(
Exception
ignored
) {
// JDK CRC32C not available, will use software fallback
}
JDK_CRC32C_CLASS
=
clazz
;
JDK_CRC32C_AVAILABLE
=
available
;
}
/**
* Returns true if the JDK's hardware-accelerated CRC32C is being used.
* Useful for diagnostics and testing.
*
* @return true if hardware acceleration is available and in use
*/
public
static
boolean
isHardwareAccelerated
() {
return
JDK_CRC32C_AVAILABLE
;
}
// Delegate to JDK implementation when available
private
final
Checksum
delegate
;
// Software implementation state (used only when JDK not available)
private
long
crc
;
public
CRC32C
() {
if
(
JDK_CRC32C_AVAILABLE
) {
try
{
delegate
= (
Checksum
)
JDK_CRC32C_CLASS
.
getDeclaredConstructor
().
newInstance
();
}
catch
(
Exception
e
) {
// Should never happen since we verified in static init
throw
new
RuntimeException
(
"Failed to instantiate JDK CRC32C"
,
e
);
}
crc
=
0
;
// unused but must initialize
}
else
{
delegate
=
null
;
crc
=
0
;
}
}
@
Override
public
void
update
(
int
b
) {
if
(
delegate
!=
null
) {
delegate
.
update
(
b
);
}
else
{
long
newCrc
=
crc
^
LONG_MASK
;
newCrc
=
updateByte
((
byte
)
b
,
newCrc
);
crc
=
newCrc
^
LONG_MASK
;
}
}
@
Override
public
void
update
(
byte
[]
bArray
,
int
off
,
int
len
) {
if
(
delegate
!=
null
) {
delegate
.
update
(
bArray
,
off
,
len
);
}
else
{
updateSoftware
(
bArray
,
off
,
len
);
}
}
/**
* Updates the checksum with the entire byte array.
* Convenience method that delegates to update(byte[], int, int).
*
* @param bArray the byte array to update with
*/
public
void
update
(
byte
[]
bArray
) {
update
(
bArray
,
0
,
bArray
.
length
);
}
@
Override
public
long
getValue
() {
if
(
delegate
!=
null
) {
return
delegate
.
getValue
();
}
return
crc
;
}
@
Override
public
void
reset
() {
if
(
delegate
!=
null
) {
delegate
.
reset
();
}
else
{
crc
=
0
;
}
}
// ========== Software Implementation ==========
private
static
final
long
[]
CRC_TABLE
= {
0x00000000L
,
0xf26b8303L
,
0xe13b70f7L
,
0x1350f3f4L
,
0xc79a971fL
,
0x35f1141cL
,
0x26a1e7e8L
,
0xd4ca64ebL
,
0x8ad958cfL
,
0x78b2dbccL
,
0x6be22838L
,
0x9989ab3bL
,
0x4d43cfd0L
,
0xbf284cd3L
,
0xac78bf27L
,
0x5e133c24L
,
0x105ec76fL
,
0xe235446cL
,
0xf165b798L
,
0x030e349bL
,
0xd7c45070L
,
0x25afd373L
,
0x36ff2087L
,
0xc494a384L
,
0x9a879fa0L
,
0x68ec1ca3L
,
0x7bbcef57L
,
0x89d76c54L
,
0x5d1d08bfL
,
0xaf768bbcL
,
0xbc267848L
,
0x4e4dfb4bL
,
0x20bd8edeL
,
0xd2d60dddL
,
0xc186fe29L
,
0x33ed7d2aL
,
0xe72719c1L
,
0x154c9ac2L
,
0x061c6936L
,
0xf477ea35L
,
0xaa64d611L
,
0x580f5512L
,
0x4b5fa6e6L
,
0xb93425e5L
,
0x6dfe410eL
,
0x9f95c20dL
,
0x8cc531f9L
,
0x7eaeb2faL
,
0x30e349b1L
,
0xc288cab2L
,
0xd1d83946L
,
0x23b3ba45L
,
0xf779deaeL
,
0x05125dadL
,
0x1642ae59L
,
0xe4292d5aL
,
0xba3a117eL
,
0x4851927dL
,
0x5b016189L
,
0xa96ae28aL
,
0x7da08661L
,
0x8fcb0562L
,
0x9c9bf696L
,
0x6ef07595L
,
0x417b1dbcL
,
0xb3109ebfL
,
0xa0406d4bL
,
0x522bee48L
,
0x86e18aa3L
,
0x748a09a0L
,
0x67dafa54L
,
0x95b17957L
,
0xcba24573L
,
0x39c9c670L
,
0x2a993584L
,
0xd8f2b687L
,
0x0c38d26cL
,
0xfe53516fL
,
0xed03a29bL
,
0x1f682198L
,
0x5125dad3L
,
0xa34e59d0L
,
0xb01eaa24L
,
0x42752927L
,
0x96bf4dccL
,
0x64d4cecfL
,
0x77843d3bL
,
0x85efbe38L
,
0xdbfc821cL
,
0x2997011fL
,
0x3ac7f2ebL
,
0xc8ac71e8L
,
0x1c661503L
,
0xee0d9600L
,
0xfd5d65f4L
,
0x0f36e6f7L
,
0x61c69362L
,
0x93ad1061L
,
0x80fde395L
,
0x72966096L
,
0xa65c047dL
,
0x5437877eL
,
0x4767748aL
,
0xb50cf789L
,
0xeb1fcbadL
,
0x197448aeL
,
0x0a24bb5aL
,
0xf84f3859L
,
0x2c855cb2L
,
0xdeeedfb1L
,
0xcdbe2c45L
,
0x3fd5af46L
,
0x7198540dL
,
0x83f3d70eL
,
0x90a324faL
,
0x62c8a7f9L
,
0xb602c312L
,
0x44694011L
,
0x5739b3e5L
,
0xa55230e6L
,
0xfb410cc2L
,
0x092a8fc1L
,
0x1a7a7c35L
,
0xe811ff36L
,
0x3cdb9bddL
,
0xceb018deL
,
0xdde0eb2aL
,
0x2f8b6829L
,
0x82f63b78L
,
0x709db87bL
,
0x63cd4b8fL
,
0x91a6c88cL
,
0x456cac67L
,
0xb7072f64L
,
0xa457dc90L
,
0x563c5f93L
,
0x082f63b7L
,
0xfa44e0b4L
,
0xe9141340L
,
0x1b7f9043L
,
0xcfb5f4a8L
,
0x3dde77abL
,
0x2e8e845fL
,
0xdce5075cL
,
0x92a8fc17L
,
0x60c37f14L
,
0x73938ce0L
,
0x81f80fe3L
,
0x55326b08L
,
0xa759e80bL
,
0xb4091bffL
,
0x466298fcL
,
0x1871a4d8L
,
0xea1a27dbL
,
0xf94ad42fL
,
0x0b21572cL
,
0xdfeb33c7L
,
0x2d80b0c4L
,
0x3ed04330L
,
0xccbbc033L
,
0xa24bb5a6L
,
0x502036a5L
,
0x4370c551L
,
0xb11b4652L
,
0x65d122b9L
,
0x97baa1baL
,
0x84ea524eL
,
0x7681d14dL
,
0x2892ed69L
,
0xdaf96e6aL
,
0xc9a99d9eL
,
0x3bc21e9dL
,
0xef087a76L
,
0x1d63f975L
,
0x0e330a81L
,
0xfc588982L
,
0xb21572c9L
,
0x407ef1caL
,
0x532e023eL
,
0xa145813dL
,
0x758fe5d6L
,
0x87e466d5L
,
0x94b49521L
,
0x66df1622L
,
0x38cc2a06L
,
0xcaa7a905L
,
0xd9f75af1L
,
0x2b9cd9f2L
,
0xff56bd19L
,
0x0d3d3e1aL
,
0x1e6dcdeeL
,
0xec064eedL
,
0xc38d26c4L
,
0x31e6a5c7L
,
0x22b65633L
,
0xd0ddd530L
,
0x0417b1dbL
,
0xf67c32d8L
,
0xe52cc12cL
,
0x1747422fL
,
0x49547e0bL
,
0xbb3ffd08L
,
0xa86f0efcL
,
0x5a048dffL
,
0x8ecee914L
,
0x7ca56a17L
,
0x6ff599e3L
,
0x9d9e1ae0L
,
0xd3d3e1abL
,
0x21b862a8L
,
0x32e8915cL
,
0xc083125fL
,
0x144976b4L
,
0xe622f5b7L
,
0xf5720643L
,
0x07198540L
,
0x590ab964L
,
0xab613a67L
,
0xb831c993L
,
0x4a5a4a90L
,
0x9e902e7bL
,
0x6cfbad78L
,
0x7fab5e8cL
,
0x8dc0dd8fL
,
0xe330a81aL
,
0x115b2b19L
,
0x020bd8edL
,
0xf0605beeL
,
0x24aa3f05L
,
0xd6c1bc06L
,
0xc5914ff2L
,
0x37faccf1L
,
0x69e9f0d5L
,
0x9b8273d6L
,
0x88d28022L
,
0x7ab90321L
,
0xae7367caL
,
0x5c18e4c9L
,
0x4f48173dL
,
0xbd23943eL
,
0xf36e6f75L
,
0x0105ec76L
,
0x12551f82L
,
0xe03e9c81L
,
0x34f4f86aL
,
0xc69f7b69L
,
0xd5cf889dL
,
0x27a40b9eL
,
0x79b737baL
,
0x8bdcb4b9L
,
0x988c474dL
,
0x6ae7c44eL
,
0xbe2da0a5L
,
0x4c4623a6L
,
0x5f16d052L
,
0xad7d5351L
};
private
static
final
long
LONG_MASK
=
0xffffffffL
;
private
static
final
long
BYTE_MASK
=
0xffL
;
private
void
updateSoftware
(
byte
[]
bArray
,
int
off
,
int
len
) {
long
newCrc
=
crc
^
LONG_MASK
;
int
end
=
off
+
len
;
// Process 8 bytes at a time for better performance
int
fastEnd
=
end
-
7
;
int
i
=
off
;
while
(
i
<
fastEnd
) {
newCrc
=
updateByte
(
bArray
[
i
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
1
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
2
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
3
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
4
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
5
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
6
],
newCrc
);
newCrc
=
updateByte
(
bArray
[
i
+
7
],
newCrc
);
i
+=
8
;
}
// Process remaining bytes
while
(
i
<
end
) {
newCrc
=
updateByte
(
bArray
[
i
],
newCrc
);
i
++;
}
crc
=
newCrc
^
LONG_MASK
;
}
private
static
long
updateByte
(
byte
newByte
,
long
crc
) {
int
index
= (
int
) ((
crc
^
newByte
) &
BYTE_MASK
);
return
(
CRC_TABLE
[
index
] ^ (
crc
>>>
8
)) &
LONG_MASK
;
}
}
Back
|
FazBrowse Home
|
New Git URL