Hacker's guide
Endianness
Some files contain numbers that have a fixed endianness,
independent from the endianness of the CPU DeuTex happens to run
on. These call for some special treatment, as the C language has
no provision for reading and writing integers otherwise than in
the native endianness.
- To read an integer from a file with a particular endianness,
use use
fread_i16_le(),
fread_i16_be(), fread_i32_le() and
fread_i32_be(). The first argument is the file
descriptor, the second argument is a pointer on a variable that
will receive the value read from the file.
- To write an integer to a file with a particular endianness,
use
fwrite_i16_le(), fwrite_i16_be(),
fwrite_i32_le() and fwrite_i32_be().
The first argument is the file descriptor, the second argument
is the value to write.
- To read an integer with a particular endianness from a
memory area, use
read_i16_le(),
read_i16_be(), read_i32_le() and
read_i32_be(). The first argument is a pointer on
the memory area, the second argument is a pointer on a variable
that will receive the value read from the memory area.
Alternatively, you can use the peek_i*() functions
that take no second argument but instead return the read value.
- To write an integer with a particular endianness to a memory
area, use
write_i16_le(),
write_i16_be, write_i32_le() and
write_i32_be(). The first argument is a pointer on
the memory area, the second argument is the value to write.
Mnemonic to remember the arguments order : the object that
has a defined endianness is considered central and therefore
always comes first.
Here is some sample code and the result of running it.
fwrite_i32_be (stdout, 0x12345678);
fwrite_i32_le (stdout, 0x12345678);
fwrite_i16_be (stdout, 0xabcd);
fwrite_i16_le (stdout, 0xabcd);
12 34 56 78 78 56 34 12 AB CD CD AB