1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.kryo;
15
16 import ch.qos.logback.core.kryo.Crc3;
17
18 import static ch.qos.logback.classic.kryo.KryoClassicConstants.STREAM_HEADER_SIZE;
19 import static ch.qos.logback.classic.kryo.KryoClassicConstants.STREAM_MAGIC;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public final class StreamFrameV5 {
37
38 public static final int TYPE_EVENT = 1;
39 public static final int TYPE_CLOSE = 2;
40
41 public static final int TYPE_BITS = 5;
42 public static final int CRC_BITS = 3;
43 public static final int TYPE_MASK = 0x1F;
44 public static final int CRC_MASK = 0x7;
45
46
47 public static final int MAX_PREFIX = 4;
48
49
50 public static final int CRC_WINDOW = 4;
51
52 private StreamFrameV5() {
53 }
54
55 public static boolean isStreamHeader(byte[] rec) {
56 return rec != null
57 && rec.length >= STREAM_HEADER_SIZE
58 && rec[0] == STREAM_MAGIC[0]
59 && rec[1] == STREAM_MAGIC[1]
60 && rec[2] == STREAM_MAGIC[2]
61 && rec[3] == STREAM_MAGIC[3];
62 }
63
64 public static int indexOfMagic(byte[] buf, int from, int to) {
65 int last = to - STREAM_MAGIC.length;
66 for (int i = from; i <= last; i++) {
67 if (buf[i] == STREAM_MAGIC[0]
68 && buf[i + 1] == STREAM_MAGIC[1]
69 && buf[i + 2] == STREAM_MAGIC[2]
70 && buf[i + 3] == STREAM_MAGIC[3]) {
71 return i;
72 }
73 }
74 return -1;
75 }
76
77 public static int countOccurrences(byte[] haystack, byte[] needle) {
78 if (needle.length == 0 || haystack.length < needle.length) {
79 return 0;
80 }
81 int count = 0;
82 outer:
83 for (int i = 0; i <= haystack.length - needle.length; i++) {
84 for (int j = 0; j < needle.length; j++) {
85 if (haystack[i + j] != needle[j]) {
86 continue outer;
87 }
88 }
89 count++;
90 }
91 return count;
92 }
93
94 public static int pack(int type, int crc3) {
95 return ((type & TYPE_MASK) << CRC_BITS) | (crc3 & CRC_MASK);
96 }
97
98 public static int typeOf(int packedByte) {
99 return (packedByte >>> CRC_BITS) & TYPE_MASK;
100 }
101
102 public static int crcOf(int packedByte) {
103 return packedByte & CRC_MASK;
104 }
105
106 public static int unsignedVarIntSize(int value) {
107 if (value < 0) {
108 throw new IllegalArgumentException("varint length must be >= 0: " + value);
109 }
110 if (value >>> 7 == 0) {
111 return 1;
112 }
113 if (value >>> 14 == 0) {
114 return 2;
115 }
116 if (value >>> 21 == 0) {
117 return 3;
118 }
119 if (value >>> 28 == 0) {
120 return 4;
121 }
122 return 5;
123 }
124
125
126 public static int writeUnsignedVarInt(byte[] dest, int off, int value) {
127 int i = off;
128 while ((value & ~0x7F) != 0) {
129 dest[i++] = (byte) ((value & 0x7F) | 0x80);
130 value >>>= 7;
131 }
132 dest[i++] = (byte) value;
133 return i - off;
134 }
135
136 public static byte[] encodeUnsignedVarInt(int value) {
137 byte[] tmp = new byte[5];
138 int n = writeUnsignedVarInt(tmp, 0, value);
139 byte[] out = new byte[n];
140 System.arraycopy(tmp, 0, out, 0, n);
141 return out;
142 }
143
144
145
146
147
148
149
150 public static int writeEventPrefix(byte[] dest, int payloadOff, int payloadLen) {
151 if (payloadLen < 0 || payloadLen > KryoEncoder.MAX_BUFFER_SIZE) {
152 throw new IllegalArgumentException("payload too large: " + payloadLen);
153 }
154 int varintLen = unsignedVarIntSize(payloadLen);
155 int prefixLen = 1 + varintLen;
156 if (payloadOff < prefixLen) {
157 throw new IllegalArgumentException("payloadOff " + payloadOff + " < prefix " + prefixLen);
158 }
159 int headerOff = payloadOff - prefixLen;
160 writeUnsignedVarInt(dest, headerOff + 1, payloadLen);
161 int crc = computeCrc(TYPE_EVENT, dest, headerOff + 1, varintLen, dest, payloadOff, payloadLen);
162 dest[headerOff] = (byte) pack(TYPE_EVENT, crc);
163 return prefixLen;
164 }
165
166 public static byte[] wrapEvent(byte[] payload) {
167 if (payload == null) {
168 throw new IllegalArgumentException("payload cannot be null");
169 }
170 int varintLen = unsignedVarIntSize(payload.length);
171 byte[] out = new byte[1 + varintLen + payload.length];
172 writeUnsignedVarInt(out, 1, payload.length);
173 int crc = computeCrc(TYPE_EVENT, out, 1, varintLen, payload, 0, payload.length);
174 out[0] = (byte) pack(TYPE_EVENT, crc);
175 System.arraycopy(payload, 0, out, 1 + varintLen, payload.length);
176 return out;
177 }
178
179 public static byte[] closeCode() {
180 byte[] out = new byte[2];
181 writeUnsignedVarInt(out, 1, 0);
182 out[0] = (byte) pack(TYPE_CLOSE, computeCrc(TYPE_CLOSE, out, 1, 1, out, 0, 0));
183 return out;
184 }
185
186 public static int computeCrc(int type, byte[] lenBytes, byte[] payload, int off, int len) {
187 return computeCrc(type, lenBytes, 0, lenBytes.length, payload, off, len);
188 }
189
190 public static int computeCrc(int type, byte[] lenBuf, int lenOff, int lenLen,
191 byte[] payload, int off, int len) {
192 Crc3 crc = new Crc3();
193 crc.updateBits(type, TYPE_BITS);
194 crc.update(lenBuf, lenOff, lenLen);
195 if (len <= 0) {
196 return crc.getValue();
197 }
198 int window = CRC_WINDOW;
199 if (len <= window * 2) {
200 crc.update(payload, off, len);
201 } else {
202 crc.update(payload, off, window);
203 crc.update(payload, off + len - window, window);
204 }
205 return crc.getValue();
206 }
207
208 public static boolean crcMatches(int packedByte, byte[] lenBytes, byte[] payload, int off, int len) {
209 int type = typeOf(packedByte);
210 int expected = computeCrc(type, lenBytes, payload, off, len);
211 return crcOf(packedByte) == expected;
212 }
213 }