Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:Kosinski

From ReverseEngineering.wiki
Format
On this wiki Facts · Code · Notes · Topics

LZSS-style compression used by Sega Mega Drive/Genesis games (Sonic the Hedgehog 2/3/Knuckles and others): 16-bit little-endian descriptor words read LSB first, three match forms and a terminator; no header.

Overview

Kosinski is the byte-oriented LZSS format used in many Mega Drive games (Platform:Sega Mega Drive), decompressed by the 68000 code of the game into RAM or VRAM. The data has no magic and no stored size; decoding stops at a terminator match. Flamewing's mdcomp library (LGPL-3.0) also handles Kosinski+ (a different, later format), Nemesis, Enigma, Comper, Saxman, LZKN1 and others. The notes below come from its Kosinski decoder.

Format

  • Descriptor field: 16 bits read little endian, bits consumed from the lowest bit up; a new descriptor is loaded as soon as the previous one is used up.
  • Bit 1: literal byte. Bit 0 then bit 1: separate match: two bytes Low, High; count = (High & 7) + 2, distance = 0x2000 - (((High & 0xF8) << 5) | Low); if (High & 7) is 0, a third byte gives the count (value + 1), where 0 ends the stream and 1 means no copy (skip). Bits 0,0: inline match: two more descriptor bits give count - 2 (so 2 to 5) and one byte gives distance = 0x100 - byte. See Fact:Kosinski/behavior/Descriptor and match coding.
  • Search window 8192 bytes, maximum match 256 (encoder constants).
  • Unverified in detail: a "moduled" wrapper (mdcomp has a moduled_adaptor) splits large data into several Kosinski blocks behind a 16-bit size header with padding.

Identification

No magic. Checked: flamewing/mdcomp @ 72c6df405a src/lib/kosinski.cc (decoder, lines 175-205 read) computes the distance of a dictionary match as 0x2000 minus a 13-bit value (full match) or 0x100 minus one byte (inline match), and the disassembly sonicretro/s2disasm (s2.asm, label KosDec, which shifts the descriptor with lsr.w #1) names the routine KosDec and calls it from many places. To find it in 68000 code of other games look for that descriptor loop (a 16-bit word shifted right bit by bit).

Code & repositories

  • https://github.com/flamewing/mdcomp @ 72c6df405a75d322c5b3722da46c3abb864d3793 · license: LGPL-3.0-or-later (declared by the contributor; GitHub's detector did not recognize COPYING)
    Sega Mega Drive compression library and tools (Kosinski, Kosinski+, Nemesis, Enigma, Comper, Saxman, LZKN1 ...), LGPL-3.0-or-later.
    Tested on: source read, not run: source read · checked 2026-10-18; last activity 2026-10-11; Note: the latest release ships binaries; the wiki links only the source, never the binaries

See also

Format:Nemesis (Sega Mega Drive), Format:Enigma (Sega Mega Drive), Technique:Identifying compression libraries in binaries