xref: /freebsd/contrib/openbsm/compat/endian_enc.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3  * Copyright (c) 2005 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Derived from FreeBSD src/sys/sys/endian.h:1.6.
28  */
29 
30 #ifndef _COMPAT_ENDIAN_ENC_H_
31 #define _COMPAT_ENDIAN_ENC_H_
32 
33 /*
34  * Some systems will have the uint/int types defined here already, others
35  * will need stdint.h.
36  */
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h>
39 #endif
40 
41 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
42 
43 static __inline uint16_t
44 be16dec(const void *pp)
45 {
46 	unsigned char const *p = (unsigned char const *)pp;
47 
48 	return ((p[0] << 8) | p[1]);
49 }
50 
51 static __inline uint32_t
52 be32dec(const void *pp)
53 {
54 	unsigned char const *p = (unsigned char const *)pp;
55 
56 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
57 }
58 
59 static __inline uint64_t
60 be64dec(const void *pp)
61 {
62 	unsigned char const *p = (unsigned char const *)pp;
63 
64 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
65 }
66 
67 static __inline uint16_t
68 le16dec(const void *pp)
69 {
70 	unsigned char const *p = (unsigned char const *)pp;
71 
72 	return ((p[1] << 8) | p[0]);
73 }
74 
75 static __inline uint32_t
76 le32dec(const void *pp)
77 {
78 	unsigned char const *p = (unsigned char const *)pp;
79 
80 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
81 }
82 
83 static __inline uint64_t
84 le64dec(const void *pp)
85 {
86 	unsigned char const *p = (unsigned char const *)pp;
87 
88 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
89 }
90 
91 static __inline void
92 be16enc(void *pp, uint16_t u)
93 {
94 	unsigned char *p = (unsigned char *)pp;
95 
96 	p[0] = (u >> 8) & 0xff;
97 	p[1] = u & 0xff;
98 }
99 
100 static __inline void
101 be32enc(void *pp, uint32_t u)
102 {
103 	unsigned char *p = (unsigned char *)pp;
104 
105 	p[0] = (u >> 24) & 0xff;
106 	p[1] = (u >> 16) & 0xff;
107 	p[2] = (u >> 8) & 0xff;
108 	p[3] = u & 0xff;
109 }
110 
111 static __inline void
112 be64enc(void *pp, uint64_t u)
113 {
114 	unsigned char *p = (unsigned char *)pp;
115 
116 	be32enc(p, u >> 32);
117 	be32enc(p + 4, u & 0xffffffff);
118 }
119 
120 static __inline void
121 le16enc(void *pp, uint16_t u)
122 {
123 	unsigned char *p = (unsigned char *)pp;
124 
125 	p[0] = u & 0xff;
126 	p[1] = (u >> 8) & 0xff;
127 }
128 
129 static __inline void
130 le32enc(void *pp, uint32_t u)
131 {
132 	unsigned char *p = (unsigned char *)pp;
133 
134 	p[0] = u & 0xff;
135 	p[1] = (u >> 8) & 0xff;
136 	p[2] = (u >> 16) & 0xff;
137 	p[3] = (u >> 24) & 0xff;
138 }
139 
140 static __inline void
141 le64enc(void *pp, uint64_t u)
142 {
143 	unsigned char *p = (unsigned char *)pp;
144 
145 	le32enc(p, u & 0xffffffff);
146 	le32enc(p + 4, u >> 32);
147 }
148 
149 #endif	/* _COMPAT_ENDIAN_ENC_H_ */
150