xref: /freebsd/share/man/man9/byteorder.9 (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
1.\" Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.Dd April 29, 2002
26.Dt BYTEORDER 9
27.Os
28.Sh NAME
29.Nm bswap16 , bswap32 , bswap64 ,
30.Nm be16toh , be32toh , be64toh , htobe16 , htobe32 , htobe64 ,
31.Nm htole16 , htole32 , htole64 , le16toh , le32toh , le64toh ,
32.Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec ,
33.Nm le16enc , le16dec , le32enc , le32dec , le64enc , le64dec
34.Nd byte order operations
35.Sh SYNOPSIS
36.In sys/endian.h
37.Ft uint16_t
38.Fn bswap16 "uint16_t int16"
39.Ft uint32_t
40.Fn bswap32 "uint32_t int32"
41.Ft uint64_t
42.Fn bswap64 "uint64_t int64"
43.Ft uint16_t
44.Fn be16toh "uint16_t big16"
45.Ft uint32_t
46.Fn be32toh "uint32_t big32"
47.Ft uint64_t
48.Fn be64toh "uint64_t big64"
49.Ft uint16_t
50.Fn htobe16 "uint16_t host16"
51.Ft uint32_t
52.Fn htobe32 "uint32_t host32"
53.Ft uint64_t
54.Fn htobe64 "uint64_t host64"
55.Ft uint16_t
56.Fn htole16 "uint16_t host16"
57.Ft uint32_t
58.Fn htole32 "uint32_t host32"
59.Ft uint64_t
60.Fn htole64 "uint64_t host64"
61.Ft uint16_t
62.Fn le16toh "uint16_t little16"
63.Ft uint32_t
64.Fn le32toh "uint32_t little32"
65.Ft uint64_t
66.Fn le64toh "uint64_t little64"
67.Ft uint16_t
68.Fn be16dec "const void *"
69.Ft uint32_t
70.Fn be32dec "const void *"
71.Ft uint64_t
72.Fn be64dec "const void *"
73.Ft uint16_t
74.Fn le16dec "const void *"
75.Ft uint32_t
76.Fn le32dec "const void *"
77.Ft uint64_t
78.Fn le64dec "const void *"
79.Ft void
80.Fn be16enc "void *" uint16_t
81.Ft void
82.Fn be32enc "void *" uint32_t
83.Ft void
84.Fn be64enc "void *" uint64_t
85.Ft void
86.Fn le16enc "void *" uint16_t
87.Ft void
88.Fn le32enc "void *" uint32_t
89.Ft void
90.Fn le64enc "void *" uint64_t
91.Sh DESCRIPTION
92The
93.Fn bswap16 ,
94.Fn bswap32 ,
95and
96.Fn bswap64
97functions return a byte order swapped integer.
98On big endian systems, the number is converted to little endian byte order.
99On little endian systems, the number is converted to big endian byte order.
100.Pp
101The
102.Fn be16toh ,
103.Fn be32toh ,
104and
105.Fn be64toh
106functions return a big endian byte ordered integer
107converted to the system's native byte order.
108The return value will be the same as the argument on big endian systems.
109.Pp
110The
111.Fn le16toh ,
112.Fn le32toh ,
113and
114.Fn le64toh
115functions return a little endian byte ordered integer
116converted to the system's native byte order.
117The return value will be the same as the argument on little endian systems.
118.Pp
119The
120.Fn htobe16 ,
121.Fn htobe32 ,
122and
123.Fn htobe64
124functions return an integer in the system's native
125byte order converted to big endian byte order.
126The return value will be the same as the argument on big endian systems.
127.Pp
128The
129.Fn htole16 ,
130.Fn htole32 ,
131and
132.Fn htole64
133functions return a integer in the system's native
134byte order converted to little endian byte order.
135The return value will be the same as the argument on little endian systems.
136.Pp
137The
138.Fn be16enc ,
139.Fn be16dec ,
140.Fn be32enc ,
141.Fn be32dec ,
142.Fn be64enc ,
143.Fn be64dec ,
144.Fn le16enc ,
145.Fn le16dec ,
146.Fn le32enc ,
147.Fn le32dec ,
148.Fn le64enc ,
149and
150.Fn le64dec
151functions encode and decode integers to/from byte strings on any alignment
152in big/little endian format.
153.Sh SEE ALSO
154.Xr byteorder 3
155.Sh HISTORY
156The
157.Fn hto*
158and
159.Fn *toh
160functions first appeared in
161.Fx 5.0 ,
162and were originally developed by the
163.Nx
164project.
165.Pp
166The encode/decode functions first appeared in
167.Fx 5.1 .
168