View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * V5 record framing. EVENT/CLOSE prefixes are 1 type/crc byte plus an unsigned Kryo
23   * varint length (typically 2 bytes total). Epoch {@code STWD} headers stay 6 bytes.
24   *
25   * <pre>
26   *   byte0:  type (5) | crc3 (3)
27   *   then:   unsigned varint length
28   *   then:   payload[length]
29   * </pre>
30   *
31   * CRC-3 covers type (5 bits), the length varint bytes, and a small payload window:
32   * all bytes if {@code length <= 8}, otherwise first and last {@link #CRC_WINDOW} bytes.
33   * The middle is not covered; the check is for torn writes (process exit / power fail)
34   * where a later session is appended after a truncated EVENT.
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      /** 1 type/crc byte + at most 3 varint bytes (covers {@link KryoEncoder#MAX_BUFFER_SIZE}). */
47      public static final int MAX_PREFIX = 4;
48  
49      /** Bytes hashed at each end of a payload longer than {@code 2 * CRC_WINDOW}. */
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     /** Kryo unsigned varint into {@code dest[off..]}. Returns bytes written. */
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      * Writes the EVENT prefix immediately before {@code dest[payloadOff]}. Payload bytes
146      * must already occupy {@code dest[payloadOff .. payloadOff + payloadLen)}.
147      *
148      * @return prefix length in bytes (2–4)
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 }