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.core.kryo;
015
016public class KryoCoreConstants {
017
018    static public boolean NEW_ENTRY = true;
019    static public boolean CACHED_ENTRY = false;
020
021    /**
022     * Sentinel id written on the wire (as 0 in the id field) when a value is not
023     * registered in the id map (e.g. frequency-gated first sightings).
024     */
025    static public int UNREGISTERED_ID = -1;
026
027    /**
028     * Maximum id that may appear in a {@link SerializerBase} entry header (12-bit field).
029     * Ids are assigned starting at 1; {@link #UNREGISTERED_ID} is encoded as 0.
030     */
031    static public final int MAX_SERIALIZER_ID = 0xFFF; // 4095
032
033    /**
034     * Live size of a {@link BidirectionalObjectToIdMap}: at least 1, at most
035     * {@link #MAX_SERIALIZER_ID} so LRU cannot mint an id outside the 12-bit header.
036     */
037    public static int cappedSerializerCapacity(int capacity) {
038        if (capacity < 1) {
039            return 1;
040        }
041        return Math.min(capacity, MAX_SERIALIZER_ID);
042    }
043
044    /**
045     * 2-byte entry header layout (big-endian short):
046     * <pre>
047     *   bits 15-3: 13-bit data
048     *     bit 12 of data (bit 15 of short): 1 = NEW_ENTRY, 0 = CACHED_ENTRY
049     *     bits 0-11 of data (bits 14-3 of short): id; 0 means UNREGISTERED_ID on NEW only
050     *   bits 2-0:  3-bit CRC over the 13 data bits (poly x^3+x+1)
051     * </pre>
052     */
053    static public final int ENTRY_HEADER_DATA_BITS = 13;
054    static public final int ENTRY_HEADER_CRC_BITS = 3;
055    static public final int ENTRY_HEADER_CRC_MASK = 0x7;
056    static public final int ENTRY_HEADER_DATA_MASK = 0x1FFF;
057
058    /** Within the 13-bit data word: NEW_ENTRY flag. */
059    static public final int ENTRY_HEADER_NEW_FLAG = 1 << 12;
060
061    /** Within the 13-bit data word: 12-bit id mask. */
062    static public final int ENTRY_HEADER_ID_MASK = 0xFFF;
063
064    /**
065     * CRC-3 generator polynomial {@code x^3 + x + 1} (0b1011), used over the 13 data bits.
066     */
067    static public final int ENTRY_HEADER_CRC3_POLY = 0b1011;
068
069    static public final String KRYO_UNSAFE_PROPERTY_KEY = "kryo.unsafe";
070}