1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003,2010 Poul-Henning Kamp <phk@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * From:
29 * $FreeBSD: head/sys/sys/endian.h 121122 2003-10-15 20:05:57Z obrien $
30 *
31 * Endian conversion functions
32 */
33
34 #ifndef VEND_H_INCLUDED
35 #define VEND_H_INCLUDED
36
37 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
38
39 static __inline uint16_t
vbe16dec(const void * pp)40 vbe16dec(const void *pp)
41 {
42 uint8_t const *p = (uint8_t const *)pp;
43
44 return ((p[0] << 8) | p[1]);
45 }
46
47 static __inline uint32_t
vbe32dec(const void * pp)48 vbe32dec(const void *pp)
49 {
50 uint8_t const *p = (uint8_t const *)pp;
51
52 return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
53 }
54
55 static __inline uint64_t
vbe64dec(const void * pp)56 vbe64dec(const void *pp)
57 {
58 uint8_t const *p = (uint8_t const *)pp;
59
60 return (((uint64_t)vbe32dec(p) << 32) | vbe32dec(p + 4));
61 }
62
63 #if 0
64 static __inline uint16_t
65 vle16dec(const void *pp)
66 {
67 uint8_t const *p = (uint8_t const *)pp;
68
69 return ((p[1] << 8) | p[0]);
70 }
71 #endif
72
73 static __inline uint32_t
vle32dec(const void * pp)74 vle32dec(const void *pp)
75 {
76 uint8_t const *p = (uint8_t const *)pp;
77
78 return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
79 }
80
81 #if 0
82 static __inline uint64_t
83 vle64dec(const void *pp)
84 {
85 uint8_t const *p = (uint8_t const *)pp;
86
87 return (((uint64_t)vle32dec(p + 4) << 32) | vle32dec(p));
88 }
89 #endif
90
91 static __inline void
vbe16enc(void * pp,uint16_t u)92 vbe16enc(void *pp, uint16_t u)
93 {
94 uint8_t *p = (uint8_t *)pp;
95
96 p[0] = (u >> 8) & 0xff;
97 p[1] = u & 0xff;
98 }
99
100 static __inline void
vbe32enc(void * pp,uint32_t u)101 vbe32enc(void *pp, uint32_t u)
102 {
103 uint8_t *p = (uint8_t *)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
vbe64enc(void * pp,uint64_t u)112 vbe64enc(void *pp, uint64_t u)
113 {
114 uint8_t *p = (uint8_t *)pp;
115
116 vbe32enc(p, (uint32_t)(u >> 32));
117 vbe32enc(p + 4, (uint32_t)(u & 0xffffffffU));
118 }
119
120 static __inline void
vle16enc(void * pp,uint16_t u)121 vle16enc(void *pp, uint16_t u)
122 {
123 uint8_t *p = (uint8_t *)pp;
124
125 p[0] = u & 0xff;
126 p[1] = (u >> 8) & 0xff;
127 }
128
129 static __inline void
vle32enc(void * pp,uint32_t u)130 vle32enc(void *pp, uint32_t u)
131 {
132 uint8_t *p = (uint8_t *)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 #if 0
141 static __inline void
142 vle64enc(void *pp, uint64_t u)
143 {
144 uint8_t *p = (uint8_t *)pp;
145
146 vle32enc(p, (uint32_t)(u & 0xffffffffU));
147 vle32enc(p + 4, (uint32_t)(u >> 32));
148 }
149 #endif
150
151 #endif
152