Data Type Encoding
The four primitive types of the SANE standard are encoded as follows:
- SANE_Byte:
- A byte is encoded as an 8 bit value.
Since the transport protocol is assumed to be byte-orientd, the bit
order is irrelevant.
- SANE_Word:
- A word is encoded as 4 bytes (32
bits). The bytes are ordered from most-significant to
least-significant byte (big-endian byte-order).
- SANE_Char:
- A character is currently encoded as an 8-bit
ISO LATIN-1 value. An extension to support wider character sets (16 or 32
bits) is planned for the future, but not supported at this point.
- SANE_String:
- A string pointer is encoded as a
SANE_Char array. The trailing NUL byte is considered part
of the array and a NULL pointer is encoded as a zero-length
array.
- SANE_Handle:
- A handle is encoded like a word.
The network backend needs to take care of converting these integer
values to the opaque pointer values that are presented to the user
of the network backend. Similarly, the SANE daemon needs to take
care of converting the opaque pointer values it receives from its
backends into 32-bit integers suitable for use for network encoding.
- enumeration types:
- Enumeration types are encoded
like words.
Closely following the type constructors of the C language, the SANE network
protocol supports the following four constructors:
- pointer:
- A pointer is encoded by a word that indicates
whether the pointer is a NULL-pointer which is then followed by the
value that the pointer points to (in the case of a non-NULL pointer;
in the case of a NULL pointer, no bytes are encoded for the pointer
value).
- array:
- An array is encoded by a word that indicates
the length of the array followed by the values of the elements in
the array. The length may be zero in which case no bytes are
encoded for the element values.
- structure:
- A structure is encoded by simply encoding the
structure members in the order in which they appear in the
corresponding C type declaration.
- union:
- A union must always be accompanied by a tag
value that indicates which of the union members is the currently the
active one. For this reason, the union itself is encoded simply by
encoding the value of the currently active member.
Note that for type constructors, the pointer, element, or member
values themselves may have a constructed type. Thus, the above rules
should be applied recursively until a sequence of primitive types has
been found.
Also SANE had no need for encoding of circular structures. This
greatly simplifies the network protocol.