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:Squirrel bytecode

From ReverseEngineering.wiki
Format
Magic FA FA 52 49 51 53
Extensions .cnut
Endianness native (little-endian on PC)
On this wiki Facts · Code · Notes · Topics

Compiled-closure stream of the Squirrel VM (sq_writeclosure, .cnut files): a 2-byte tag FA FA, the 'SQIR' header with the sizes of char, integer and float, one function prototype with tagged parts, and a 'TAIL' marker.

Layout (Squirrel 3.2)

All integers are written raw in native byte order with the sizes recorded in the header.

Offset Size Field
0 u16 SQ_BYTECODE_STREAM_TAG 0xFAFA
2 u32 SQ_CLOSURESTREAM_HEAD 0x53514952 (bytes 52 49 51 53)
6 u32 sizeof(SQChar): 1 or 2
10 u32 sizeof(SQInteger): 4 or 8
14 u32 sizeof(SQFloat): 4 or 8
18 function prototype
end u32 SQ_CLOSURESTREAM_TAIL 0x4C494154 (bytes 54 41 49 4C)

The three size fields identify the build (4/4 on 32-bit style builds, 8/8 on 64-bit ones).

Function prototype

Every section is introduced by a PART tag (u32 0x54524150, bytes 50 41 52 54):

  1. PART, source name object, function name object
  2. PART, eight SQInteger counts: literals, parameters, outer values, local var infos, line infos, default params, instructions, functions
  3. PART, literals (objects)
  4. PART, parameter names (objects)
  5. PART, outer values: u32 type, value object, name object each
  6. PART, local var infos: name object, then three u32 (pos, start_op, end_op)
  7. PART, line infos: raw array of {SQInteger line, SQInteger op}
  8. PART, default parameter indices: raw SQInteger array
  9. PART, instructions: raw array of 8-byte SQInstruction (i32 arg1, u8 op, u8 arg0, u8 arg2, u8 arg3)
  10. PART, nested function prototypes (recursively)
  11. stack size (SQInteger), generator flag (bool), varparams flag

Objects

An object is a u32 type followed by its payload. The type is the complete OT_ value including the flag bits: null 0x01000001 (no payload), integer 0x05000002 (SQInteger), float 0x05000004 (SQFloat), bool 0x01000008 (SQInteger), string 0x08000010 (SQInteger length, then length*sizeof(SQChar) bytes, not terminated). Nothing else can be serialized.

Notes

  • Closures with bound free variables cannot be written.
  • The header carries no version number: a stream from another Squirrel version fails later or decodes wrong. Check the opcode table (sqopcodes.h, _OP_*) of the version the game embeds.
  • Source read, no file was parsed: verify against a real .cnut.

Code & repositories

  • https://github.com/albertodemichelis/squirrel @ f9267f2f2a9afa1face89d8ea078c686331e30d1 · license: MIT
    Reference Squirrel VM. Useful without running: squirrel/sqobject.cpp (closure save/load), sqopcodes.h (opcodes), include/squirrel.h (API, type constants).
    Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-02-28

See also

Library:Squirrel, Format:Lua bytecode.