001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.kryo; 015 016import ch.qos.logback.core.kryo.Crc3; 017 018import static ch.qos.logback.classic.kryo.KryoClassicConstants.STREAM_HEADER_SIZE; 019import static ch.qos.logback.classic.kryo.KryoClassicConstants.STREAM_MAGIC; 020 021/** 022 * V5 record framing. EVENT/CLOSE prefixes are 1 type/crc byte plus an unsigned Kryo 023 * varint length (typically 2 bytes total). Epoch {@code STWD} headers stay 6 bytes. 024 * 025 * <pre> 026 * byte0: type (5) | crc3 (3) 027 * then: unsigned varint length 028 * then: payload[length] 029 * </pre> 030 * 031 * CRC-3 covers type (5 bits), the length varint bytes, and a small payload window: 032 * all bytes if {@code length <= 8}, otherwise first and last {@link #CRC_WINDOW} bytes. 033 * The middle is not covered; the check is for torn writes (process exit / power fail) 034 * where a later session is appended after a truncated EVENT. 035 */ 036public final class StreamFrameV5 { 037 038 public static final int TYPE_EVENT = 1; 039 public static final int TYPE_CLOSE = 2; 040 041 public static final int TYPE_BITS = 5; 042 public static final int CRC_BITS = 3; 043 public static final int TYPE_MASK = 0x1F; 044 public static final int CRC_MASK = 0x7; 045 046 /** 1 type/crc byte + at most 3 varint bytes (covers {@link KryoEncoder#MAX_BUFFER_SIZE}). */ 047 public static final int MAX_PREFIX = 4; 048 049 /** Bytes hashed at each end of a payload longer than {@code 2 * CRC_WINDOW}. */ 050 public static final int CRC_WINDOW = 4; 051 052 private StreamFrameV5() { 053 } 054 055 public static boolean isStreamHeader(byte[] rec) { 056 return rec != null 057 && rec.length >= STREAM_HEADER_SIZE 058 && rec[0] == STREAM_MAGIC[0] 059 && rec[1] == STREAM_MAGIC[1] 060 && rec[2] == STREAM_MAGIC[2] 061 && rec[3] == STREAM_MAGIC[3]; 062 } 063 064 public static int indexOfMagic(byte[] buf, int from, int to) { 065 int last = to - STREAM_MAGIC.length; 066 for (int i = from; i <= last; i++) { 067 if (buf[i] == STREAM_MAGIC[0] 068 && buf[i + 1] == STREAM_MAGIC[1] 069 && buf[i + 2] == STREAM_MAGIC[2] 070 && buf[i + 3] == STREAM_MAGIC[3]) { 071 return i; 072 } 073 } 074 return -1; 075 } 076 077 public static int countOccurrences(byte[] haystack, byte[] needle) { 078 if (needle.length == 0 || haystack.length < needle.length) { 079 return 0; 080 } 081 int count = 0; 082 outer: 083 for (int i = 0; i <= haystack.length - needle.length; i++) { 084 for (int j = 0; j < needle.length; j++) { 085 if (haystack[i + j] != needle[j]) { 086 continue outer; 087 } 088 } 089 count++; 090 } 091 return count; 092 } 093 094 public static int pack(int type, int crc3) { 095 return ((type & TYPE_MASK) << CRC_BITS) | (crc3 & CRC_MASK); 096 } 097 098 public static int typeOf(int packedByte) { 099 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}