1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hacker's guide</title>
</head>
<body>
<h1>Hacker's guide</h1>
<h2>Endianness</h2>
<p>
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.
</p>
<ul>
<li>
<p>
To read an integer from a file with a particular endianness, use
<code>fread_i16_le()</code>, <code>fread_i16_be()</code>,
<code>fread_i32_le()</code> and <code>fread_i32_be()</code>.
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.
</p>
<li>
<p>
To write an integer to a file with a particular endianness, use
<code>fwrite_i16_le()</code>, <code>fwrite_i16_be()</code>,
<code>fwrite_i32_le()</code> and <code>fwrite_i32_be()</code>.
The first argument is the file descriptor, the second argument is the
value to write.
</p>
<li>
<p>
To read an integer with a particular endianness from a memory area,
use <code>read_i16_le()</code>, <code>read_i16_be()</code>,
<code>read_i32_le()</code> and <code>read_i32_be()</code>.
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 <code>peek_i*()</code> functions that
take no second argument but instead return the read value.
</p>
<li>
<p>
To write an integer with a particular endianness to a memory area,
use <code>write_i16_le()</code>, <code>write_i16_be</code>,
<code>write_i32_le()</code> and <code>write_i32_be()</code>.
The first argument is a pointer on the memory area, the second
argument is the value to write.
</p>
</ul>
<p>
Mnemonic to remember the arguments order : the object that has a
defined endianness is considered central and therefore always comes
first.
</p>
<p>
Here is some sample code and the result of running it.
</p>
<pre>fwrite_i32_be (stdout, 0x12345678);
fwrite_i32_le (stdout, 0x12345678);
fwrite_i16_be (stdout, 0xabcd);
fwrite_i16_le (stdout, 0xabcd);</pre>
<pre>12 34 56 78 78 56 34 12 AB CD CD AB</pre>
</body>
</html>
|