Binary Files
About
This article is an introduction to binary files.
Before reading the other articles in the “File Formats” category, you should read this one first.
Structure
In binary files, bytes are represented in reverse order.
For example, the number 0x12345678 in a file would look like this: 78 56 34 12.
Data types
Let’s introduce the notation of data types.
These notations will be used in other articles.
Designation | Type | Range | Size (bytes) |
---|---|---|---|
B | Integer | 0 … 255 | 1 |
H | Integer | 0 … 65535 | 2 |
I | Integer | 0 … 4294967295 | 4 |
i | Integer | -2147483648 … 2147483647 | 4 |
f | Fractional number | - | 4 |
s | String | - | - |
In some files a value may not be stored in the whole byte, but only part of it (e.g. 4 bits).
As a result, one byte will store two values.
In articles about binary files, sizes will be specified either in bits or in bytes.
Strings necessarily have a null byte at the end, which indicates that the string has ended. For example: “test_string0x0”.
As a result, the length of the string is equal to the number of characters in it + 1 (null byte).
Blocks
In X-Ray, some binary files are in RIFF format.
Such files have blocks (also called chunks or sections).
A block is binary data with a header.
Block structure
Data | Type |
---|---|
Identifier | H |
Compression | H |
Content size (bytes) | I |
Content | Binary data |
You can see from the identifier what is stored in a particular block.
If the compression is set to 0x0000, the block is uncompressed, and if it is set to 0x8000, the block is compressed by the Huffman method.
You can use a program from xray_re_tools (trunk\garbage\lzhuf\lzhuf.c) to decompress compressed blocks.
In the files of the final version of the game most blocks are uncompressed.
The size of the block content indicates only the number of bytes of data (not including identifier, compression and size bytes).
Binary block data can be represented by nested blocks.