xref: /titanic_52/usr/src/man/man5/byteorder.5 (revision 3b4b8bda7949c2c1bf610c9f0413accc9d26119a)
1*3b4b8bdaSRobert Mustacchi.\"
2*3b4b8bdaSRobert Mustacchi.\" This file and its contents are supplied under the terms of the
3*3b4b8bdaSRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0.
4*3b4b8bdaSRobert Mustacchi.\" You may only use this file in accordance with the terms of version
5*3b4b8bdaSRobert Mustacchi.\" 1.0 of the CDDL.
6*3b4b8bdaSRobert Mustacchi.\"
7*3b4b8bdaSRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this
8*3b4b8bdaSRobert Mustacchi.\" source.  A copy of the CDDL is also available via the Internet at
9*3b4b8bdaSRobert Mustacchi.\" http://www.illumos.org/license/CDDL.
10*3b4b8bdaSRobert Mustacchi.\"
11*3b4b8bdaSRobert Mustacchi.\"
12*3b4b8bdaSRobert Mustacchi.\" Copyright 2016 Joyent, Inc.
13*3b4b8bdaSRobert Mustacchi.\"
14*3b4b8bdaSRobert Mustacchi.Dd January 31, 2016
15*3b4b8bdaSRobert Mustacchi.Dt BYTEORDER 5
16*3b4b8bdaSRobert Mustacchi.Os
17*3b4b8bdaSRobert Mustacchi.Sh NAME
18*3b4b8bdaSRobert Mustacchi.Nm byteorder ,
19*3b4b8bdaSRobert Mustacchi.Nm endian
20*3b4b8bdaSRobert Mustacchi.Nd byte order and endianness
21*3b4b8bdaSRobert Mustacchi.Sh DESCRIPTION
22*3b4b8bdaSRobert MustacchiInteger values which occupy more than 1 byte in memory can be laid out
23*3b4b8bdaSRobert Mustacchiin different ways on different platforms. In particular, there is a
24*3b4b8bdaSRobert Mustacchimajor split between those which place the least significant byte of an
25*3b4b8bdaSRobert Mustacchiinteger at the lowest address, and those which place the most
26*3b4b8bdaSRobert Mustacchisignificant byte there instead. As this difference relates to which
27*3b4b8bdaSRobert Mustacchiend of the integer is found in memory first, the term
28*3b4b8bdaSRobert Mustacchi.Em endian
29*3b4b8bdaSRobert Mustacchiis used to refer to a particular byte order.
30*3b4b8bdaSRobert Mustacchi.Pp
31*3b4b8bdaSRobert MustacchiA platform is referred to as using a
32*3b4b8bdaSRobert Mustacchi.Em big-endian
33*3b4b8bdaSRobert Mustacchibyte order when it places the most significant byte at the lowest
34*3b4b8bdaSRobert Mustacchiaddress, and
35*3b4b8bdaSRobert Mustacchi.Em little-endian
36*3b4b8bdaSRobert Mustacchiwhen it places the least significant byte first. Some platforms may also
37*3b4b8bdaSRobert Mustacchiswitch between big- and little-endian mode and run code compiled for
38*3b4b8bdaSRobert Mustacchieither.
39*3b4b8bdaSRobert Mustacchi.Pp
40*3b4b8bdaSRobert MustacchiHistorically, there have also been some systems that utilized
41*3b4b8bdaSRobert Mustacchi.Em middle-endian
42*3b4b8bdaSRobert Mustacchibyte orders for integers larger than 2 bytes. Such orderings are not in
43*3b4b8bdaSRobert Mustacchicommon use today.
44*3b4b8bdaSRobert Mustacchi.Pp
45*3b4b8bdaSRobert MustacchiEndianness is also of particular importance when dealing with values
46*3b4b8bdaSRobert Mustacchithat are being read into memory from an external source. For example,
47*3b4b8bdaSRobert Mustacchinetwork protocols such as IP conventionally define the fields in a
48*3b4b8bdaSRobert Mustacchipacket as being always stored in big-endian byte order. This means that
49*3b4b8bdaSRobert Mustacchia little-endian machine will have to perform transformations on these
50*3b4b8bdaSRobert Mustacchifields in order to process them.
51*3b4b8bdaSRobert Mustacchi.Ss Examples
52*3b4b8bdaSRobert MustacchiTo illustrate endianness in memory, let us consider the decimal integer
53*3b4b8bdaSRobert Mustacchi2864434397. This number fits in 32 bits of storage (4 bytes).
54*3b4b8bdaSRobert Mustacchi.Pp
55*3b4b8bdaSRobert MustacchiOn a big-endian system, this integer would be written into memory as
56*3b4b8bdaSRobert Mustacchithe bytes 0xAA, 0xBB, 0xCC, 0xDD, in order from lowest memory address to
57*3b4b8bdaSRobert Mustacchihighest.
58*3b4b8bdaSRobert Mustacchi.Pp
59*3b4b8bdaSRobert MustacchiOn a little-endian system, it would be written instead as the bytes
60*3b4b8bdaSRobert Mustacchi0xDD, 0xCC, 0xBB, 0xAA, in that order.
61*3b4b8bdaSRobert Mustacchi.Pp
62*3b4b8bdaSRobert MustacchiIf both the big- and little-endian systems were asked to store this
63*3b4b8bdaSRobert Mustacchiinteger at address 0x100, we would see the following in each of their
64*3b4b8bdaSRobert Mustacchimemory:
65*3b4b8bdaSRobert Mustacchi.Bd -literal
66*3b4b8bdaSRobert Mustacchi
67*3b4b8bdaSRobert Mustacchi                    Big-Endian
68*3b4b8bdaSRobert Mustacchi
69*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
70*3b4b8bdaSRobert Mustacchi        || 0xAA || 0xBB || 0xCC || 0xDD ||
71*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
72*3b4b8bdaSRobert Mustacchi            ^^      ^^      ^^      ^^
73*3b4b8bdaSRobert Mustacchi          0x100   0x101   0x102   0x103
74*3b4b8bdaSRobert Mustacchi            vv      vv      vv      vv
75*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
76*3b4b8bdaSRobert Mustacchi        || 0xDD || 0xCC || 0xBB || 0xAA ||
77*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
78*3b4b8bdaSRobert Mustacchi
79*3b4b8bdaSRobert Mustacchi                  Little-Endian
80*3b4b8bdaSRobert Mustacchi.Ed
81*3b4b8bdaSRobert Mustacchi.Pp
82*3b4b8bdaSRobert MustacchiIt is particularly important to note that even though the byte order is
83*3b4b8bdaSRobert Mustacchidifferent between these two machines, the bit ordering within each byte,
84*3b4b8bdaSRobert Mustacchiby convention, is still the same.
85*3b4b8bdaSRobert Mustacchi.Pp
86*3b4b8bdaSRobert MustacchiFor example, take the decimal integer 4660, which occupies in 16 bits (2
87*3b4b8bdaSRobert Mustacchibytes).
88*3b4b8bdaSRobert Mustacchi.Pp
89*3b4b8bdaSRobert MustacchiOn a big-endian system, this would be written into memory as 0x12, then
90*3b4b8bdaSRobert Mustacchi0x34.
91*3b4b8bdaSRobert Mustacchi.Pp
92*3b4b8bdaSRobert MustacchiOn a little-endian system, it would be written as 0x34, then 0x12.  Note
93*3b4b8bdaSRobert Mustacchithat this is not at all the same as seeing 0x43 then 0x21 in memory --
94*3b4b8bdaSRobert Mustacchionly the bytes are re-ordered, not any bits (or nybbles) within them.
95*3b4b8bdaSRobert Mustacchi.Pp
96*3b4b8bdaSRobert MustacchiAs before, storing this at address 0x100:
97*3b4b8bdaSRobert Mustacchi.Bd -literal
98*3b4b8bdaSRobert Mustacchi                    Big-Endian
99*3b4b8bdaSRobert Mustacchi
100*3b4b8bdaSRobert Mustacchi                ++------++------++
101*3b4b8bdaSRobert Mustacchi                || 0x12 || 0x34 ||
102*3b4b8bdaSRobert Mustacchi                ++------++------++
103*3b4b8bdaSRobert Mustacchi                    ^^      ^^
104*3b4b8bdaSRobert Mustacchi                  0x100   0x101
105*3b4b8bdaSRobert Mustacchi                    vv      vv
106*3b4b8bdaSRobert Mustacchi                ++------++------++
107*3b4b8bdaSRobert Mustacchi                || 0x34 || 0x12 ||
108*3b4b8bdaSRobert Mustacchi                ++------++------++
109*3b4b8bdaSRobert Mustacchi
110*3b4b8bdaSRobert Mustacchi                   Little-Endian
111*3b4b8bdaSRobert Mustacchi.Ed
112*3b4b8bdaSRobert Mustacchi.Pp
113*3b4b8bdaSRobert MustacchiThis example shows how an eight byte number, 0xBADCAFEDEADBEEF is stored
114*3b4b8bdaSRobert Mustacchiin both big and little-endian:
115*3b4b8bdaSRobert Mustacchi.Bd -literal
116*3b4b8bdaSRobert Mustacchi                        Big-Endian
117*3b4b8bdaSRobert Mustacchi
118*3b4b8bdaSRobert Mustacchi    +------+------+------+------+------+------+------+------+
119*3b4b8bdaSRobert Mustacchi    | 0xBA | 0xDC | 0xAF | 0xFE | 0xDE | 0xAD | 0xBE | 0xEF |
120*3b4b8bdaSRobert Mustacchi    +------+------+------+------+------+------+------+------+
121*3b4b8bdaSRobert Mustacchi       ^^     ^^     ^^     ^^     ^^     ^^     ^^     ^^
122*3b4b8bdaSRobert Mustacchi     0x100  0x101  0x102  0x103  0x104  0x105  0x106  0x107
123*3b4b8bdaSRobert Mustacchi       vv     vv     vv     vv     vv     vv     vv     vv
124*3b4b8bdaSRobert Mustacchi    +------+------+------+------+------+------+------+------+
125*3b4b8bdaSRobert Mustacchi    | 0xEF | 0xBE | 0xAD | 0xDE | 0xFE | 0xAF | 0xDC | 0xBA |
126*3b4b8bdaSRobert Mustacchi    +------+------+------+------+------+------+------+------+
127*3b4b8bdaSRobert Mustacchi
128*3b4b8bdaSRobert Mustacchi                       Little-Endian
129*3b4b8bdaSRobert Mustacchi
130*3b4b8bdaSRobert Mustacchi.Ed
131*3b4b8bdaSRobert Mustacchi.Pp
132*3b4b8bdaSRobert MustacchiThe treatment of different endian values would not be complete without
133*3b4b8bdaSRobert Mustacchidiscussing
134*3b4b8bdaSRobert Mustacchi.Em PDP-endian ,
135*3b4b8bdaSRobert Mustacchiwhich is also known as
136*3b4b8bdaSRobert Mustacchi.Em middle-endian .
137*3b4b8bdaSRobert MustacchiWhile the PDP-11 was a 16-bit little-endian system, it laid out 32-bit
138*3b4b8bdaSRobert Mustacchivalues in a different way from current little-endian systems. First, it
139*3b4b8bdaSRobert Mustacchiwould divide a 32-bit number into two 16-bit numbers. Each 16-bit number
140*3b4b8bdaSRobert Mustacchiwould be stored in little-endian; however, the two 16-bit words would be
141*3b4b8bdaSRobert Mustacchistored with the larger 16-bit word appearing first in memory, followed
142*3b4b8bdaSRobert Mustacchiby the latter.
143*3b4b8bdaSRobert Mustacchi.Pp
144*3b4b8bdaSRobert MustacchiThe following image illustrates PDP-endian and compares it against
145*3b4b8bdaSRobert Mustacchilittle-endian values. Here, we'll start with the value 0xAABBCCDD and
146*3b4b8bdaSRobert Mustacchishow how the four bytes for it will be laid out, starting at 0x100.
147*3b4b8bdaSRobert Mustacchi.Bd -literal
148*3b4b8bdaSRobert Mustacchi                    PDP-Endian
149*3b4b8bdaSRobert Mustacchi
150*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
151*3b4b8bdaSRobert Mustacchi        || 0xBB || 0xAA || 0xDD || 0xCC ||
152*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
153*3b4b8bdaSRobert Mustacchi            ^^      ^^      ^^      ^^
154*3b4b8bdaSRobert Mustacchi          0x100   0x101   0x102   0x103
155*3b4b8bdaSRobert Mustacchi            vv      vv      vv      vv
156*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
157*3b4b8bdaSRobert Mustacchi        || 0xDD || 0xCC || 0xBB || 0xAA ||
158*3b4b8bdaSRobert Mustacchi        ++------++------++------++------++
159*3b4b8bdaSRobert Mustacchi
160*3b4b8bdaSRobert Mustacchi                  Little-Endian
161*3b4b8bdaSRobert Mustacchi
162*3b4b8bdaSRobert Mustacchi.Ed
163*3b4b8bdaSRobert Mustacchi.Ss Network Byte Order
164*3b4b8bdaSRobert MustacchiThe term 'network byte order' refers to big-endian ordering, and
165*3b4b8bdaSRobert Mustacchioriginates from the IEEE. Early disagreements over which byte ordering
166*3b4b8bdaSRobert Mustacchito use for network traffic prompted RFC1700 to define that all
167*3b4b8bdaSRobert MustacchiIETF-specified network protocols use big-endian ordering unless noted
168*3b4b8bdaSRobert Mustacchiexplicitly otherwise. The Internet protocol family (IP, and thus TCP and
169*3b4b8bdaSRobert MustacchiUDP etc) particularly adhere to this convention.
170*3b4b8bdaSRobert Mustacchi.Ss Determining the System's Byte Order
171*3b4b8bdaSRobert MustacchiThe operating system supports both big-endian and little-endian CPUs. To
172*3b4b8bdaSRobert Mustacchimake it easier for programs to determine the endianness of the
173*3b4b8bdaSRobert Mustacchiplatform they are being compiled for, functions and macro constants are
174*3b4b8bdaSRobert Mustacchiprovided in the system header files.
175*3b4b8bdaSRobert Mustacchi.Pp
176*3b4b8bdaSRobert MustacchiThe endianness of the system can be obtained by including the header
177*3b4b8bdaSRobert Mustacchi.In sys/types.h
178*3b4b8bdaSRobert Mustacchiand using the pre-processor macros
179*3b4b8bdaSRobert Mustacchi.Sy _LITTLE_ENDIAN
180*3b4b8bdaSRobert Mustacchiand
181*3b4b8bdaSRobert Mustacchi.Sy _BIG_ENDIAN .
182*3b4b8bdaSRobert MustacchiSee
183*3b4b8bdaSRobert Mustacchi.Xr types.h 3HEAD
184*3b4b8bdaSRobert Mustacchifor more information.
185*3b4b8bdaSRobert Mustacchi.Pp
186*3b4b8bdaSRobert MustacchiAdditionally, the header
187*3b4b8bdaSRobert Mustacchi.In endian.h
188*3b4b8bdaSRobert Mustacchidefines an alternative means for determining the endianness of the
189*3b4b8bdaSRobert Mustacchicurrent system. See
190*3b4b8bdaSRobert Mustacchi.Xr endian.h 3HEAD
191*3b4b8bdaSRobert Mustacchifor more information.
192*3b4b8bdaSRobert Mustacchi.Pp
193*3b4b8bdaSRobert Mustacchiillumos runs on both big- and little-endian systems. When writing
194*3b4b8bdaSRobert Mustacchisoftware for which the endianness is important, one must always check
195*3b4b8bdaSRobert Mustacchithe byte order and convert it appropriately.
196*3b4b8bdaSRobert Mustacchi.Ss Converting Between Byte Orders
197*3b4b8bdaSRobert MustacchiThe system provides two different sets of functions to convert values
198*3b4b8bdaSRobert Mustacchibetween big-endian and little-endian. They are defined in
199*3b4b8bdaSRobert Mustacchi.Xr byteorder 3C
200*3b4b8bdaSRobert Mustacchiand
201*3b4b8bdaSRobert Mustacchi.Xr endian 3C .
202*3b4b8bdaSRobert Mustacchi.Pp
203*3b4b8bdaSRobert MustacchiThe
204*3b4b8bdaSRobert Mustacchi.Xr byteorder 3SOCKET
205*3b4b8bdaSRobert Mustacchifamily of functions convert data between the host's native byte order
206*3b4b8bdaSRobert Mustacchiand big- or little-endian.
207*3b4b8bdaSRobert MustacchiThe functions operate on either 16-bit, 32-bit, or 64-bit values.
208*3b4b8bdaSRobert MustacchiFunctions that convert from network byte order to the host's byte order
209*3b4b8bdaSRobert Mustacchistart with the string
210*3b4b8bdaSRobert Mustacchi.Sy ntoh ,
211*3b4b8bdaSRobert Mustacchiwhile functions which convert from the host's byte order to network byte
212*3b4b8bdaSRobert Mustacchiorder, begin with
213*3b4b8bdaSRobert Mustacchi.Sy hton .
214*3b4b8bdaSRobert MustacchiFor example, to convert a 32-bit value, a long, from network byte order
215*3b4b8bdaSRobert Mustacchito the host's, one would use the function
216*3b4b8bdaSRobert Mustacchi.Xr ntohl 3SOCKET .
217*3b4b8bdaSRobert Mustacchi.Pp
218*3b4b8bdaSRobert MustacchiThese functions have been standardized by POSIX. However, the 64-bit variants,
219*3b4b8bdaSRobert Mustacchi.Xr ntohll 3SOCKET
220*3b4b8bdaSRobert Mustacchiand
221*3b4b8bdaSRobert Mustacchi.Xr htonll 3SOCKET
222*3b4b8bdaSRobert Mustacchiare not standardized and may not be found on other systems. For more
223*3b4b8bdaSRobert Mustacchiinformation on these functions, see
224*3b4b8bdaSRobert Mustacchi.Xr byteorder 3SOCKET .
225*3b4b8bdaSRobert Mustacchi.Pp
226*3b4b8bdaSRobert MustacchiThe second family of functions,
227*3b4b8bdaSRobert Mustacchi.Xr endian 3C ,
228*3b4b8bdaSRobert Mustacchiprovide a means to convert between the host's byte order
229*3b4b8bdaSRobert Mustacchiand big-endian and little-endian specifically. While these functions are
230*3b4b8bdaSRobert Mustacchisimilar to those in
231*3b4b8bdaSRobert Mustacchi.Xr byteorder 3C ,
232*3b4b8bdaSRobert Mustacchithey more explicitly cover different data conversions. Like them, these
233*3b4b8bdaSRobert Mustacchifunctions operate on either 16-bit, 32-bit, or 64-bit values. When
234*3b4b8bdaSRobert Mustacchiconverting from big-endian, to the host's endianness, the functions
235*3b4b8bdaSRobert Mustacchibegin with
236*3b4b8bdaSRobert Mustacchi.Sy betoh .
237*3b4b8bdaSRobert MustacchiIf instead, one is converting data from the host's native endianness to
238*3b4b8bdaSRobert Mustacchianother, then it starts with
239*3b4b8bdaSRobert Mustacchi.Sy htobe .
240*3b4b8bdaSRobert MustacchiWhen working with little-endian data, the prefixes
241*3b4b8bdaSRobert Mustacchi.Sy letoh
242*3b4b8bdaSRobert Mustacchiand
243*3b4b8bdaSRobert Mustacchi.Sy htole
244*3b4b8bdaSRobert Mustacchiconvert little-endian data to the host's endianness and from the host's
245*3b4b8bdaSRobert Mustacchito little-endian respectively.
246*3b4b8bdaSRobert Mustacchi.Pp
247*3b4b8bdaSRobert MustacchiThese functions
248*3b4b8bdaSRobert Mustacchiare not standardized and the header they appear in varies between the
249*3b4b8bdaSRobert MustacchiBSDs and GNU/Linux. Applications that wish to be portable, should
250*3b4b8bdaSRobert Mustacchiinstead use the
251*3b4b8bdaSRobert Mustacchi.Xr byteorder 3C
252*3b4b8bdaSRobert Mustacchifunctions.
253*3b4b8bdaSRobert Mustacchi.Pp
254*3b4b8bdaSRobert MustacchiAll of these functions in both families simply return their input when
255*3b4b8bdaSRobert Mustacchithe host's native byte order is the same as the desired order. For
256*3b4b8bdaSRobert Mustacchiexample, when calling
257*3b4b8bdaSRobert Mustacchi.Xr htonl 3SOCKET
258*3b4b8bdaSRobert Mustacchion a big-endian system the original data is returned with no conversion
259*3b4b8bdaSRobert Mustacchior modification.
260*3b4b8bdaSRobert Mustacchi.Sh SEE ALSO
261*3b4b8bdaSRobert Mustacchi.Xr endian 3C ,
262*3b4b8bdaSRobert Mustacchi.Xr endian.h 3HEAD ,
263*3b4b8bdaSRobert Mustacchi.Xr inet 3HEAD ,
264*3b4b8bdaSRobert Mustacchi.Xr byteorder 3SOCKET
265