1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.kryo;
15
16
17
18
19
20
21
22
23 public final class Crc16 {
24
25 public static final int POLY = 0x1021;
26 public static final int INIT = 0xFFFF;
27
28 private static final int[] TABLE = new int[256];
29
30 static {
31 for (int i = 0; i < 256; i++) {
32 int c = i << 8;
33 for (int bit = 0; bit < 8; bit++) {
34 if ((c & 0x8000) != 0) {
35 c = (c << 1) ^ POLY;
36 } else {
37 c <<= 1;
38 }
39 c &= 0xFFFF;
40 }
41 TABLE[i] = c;
42 }
43 }
44
45 private int crc = INIT;
46
47 public void update(int b) {
48 crc = ((crc << 8) ^ TABLE[((crc >>> 8) ^ (b & 0xFF)) & 0xFF]) & 0xFFFF;
49 }
50
51 public void update(byte[] data) {
52 update(data, 0, data.length);
53 }
54
55 public void update(byte[] data, int off, int len) {
56 int c = crc;
57 int end = off + len;
58 for (int i = off; i < end; i++) {
59 c = ((c << 8) ^ TABLE[((c >>> 8) ^ (data[i] & 0xFF)) & 0xFF]) & 0xFFFF;
60 }
61 crc = c;
62 }
63
64 public int getValue() {
65 return crc & 0xFFFF;
66 }
67
68 public static int of(byte[] data) {
69 Crc16 crc = new Crc16();
70 crc.update(data);
71 return crc.getValue();
72 }
73 }