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.

DesignationTypeRangeSize (bytes)
BInteger0 … 2551
HInteger0 … 655352
IInteger0 … 42949672954
iInteger-2147483648 … 21474836474
fFractional number-4
sString--

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

DataType
IdentifierH
CompressionH
Content size (bytes)I
ContentBinary 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.


Sources

Source