MCPcopy Create free account
hub / github.com/ReadyTalk/avian / CRC32

Class CRC32

classpath/java/util/zip/CRC32.java:13–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11package java.util.zip;
12
13public class CRC32 {
14 private static final int Polynomial = 0x04C11DB7;
15 private static final int Width = 32;
16 private static final int Top = 1 << (Width - 1);
17 private static final int InitialRemainder = 0xFFFFFFFF;
18 private static final long ResultXor = 0xFFFFFFFFL;
19
20 private static final int[] table = new int[256];
21
22 static {
23 for (int dividend = 0; dividend < 256; ++ dividend) {
24 int remainder = dividend << (Width - 8);
25 for (int bit = 8; bit > 0; --bit) {
26 remainder = ((remainder & Top) != 0)
27 ? (remainder << 1) ^ Polynomial
28 : (remainder << 1);
29 }
30 table[dividend] = remainder;
31 }
32 }
33
34 private int remainder = InitialRemainder;
35
36 public void reset() {
37 remainder = InitialRemainder;
38 }
39
40 public void update(int b) {
41 remainder = table[reflect(b, 8) ^ (remainder >>> (Width - 8))]
42 ^ (remainder << 8);
43 }
44
45 public void update(byte[] array, int offset, int length) {
46 for (int i = 0; i < length; ++i) {
47 update(array[offset + i] & 0xFF);
48 }
49 }
50
51 public void update(byte[] array) {
52 update(array, 0, array.length);
53 }
54
55 public long getValue() {
56 return (reflect(remainder, Width) ^ ResultXor) & 0xFFFFFFFFL;
57 }
58
59 private static int reflect(int x, int n) {
60 int reflection = 0;
61 for (int i = 0; i < n; ++i) {
62 if ((x & 1) != 0) {
63 reflection |= (1 << ((n - 1) - i));
64 }
65 x = (x >>> 1);
66 }
67 return reflection;
68 }
69}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected