1*9558e6acSTreehugger Robot /*-
2*9558e6acSTreehugger Robot * SPDX-License-Identifier: BSD-3-Clause
3*9558e6acSTreehugger Robot *
4*9558e6acSTreehugger Robot * Copyright (c) 1982, 1986, 1989, 1993
5*9558e6acSTreehugger Robot * The Regents of the University of California. All rights reserved.
6*9558e6acSTreehugger Robot * (c) UNIX System Laboratories, Inc.
7*9558e6acSTreehugger Robot * All or some portions of this file are derived from material licensed
8*9558e6acSTreehugger Robot * to the University of California by American Telephone and Telegraph
9*9558e6acSTreehugger Robot * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10*9558e6acSTreehugger Robot * the permission of UNIX System Laboratories, Inc.
11*9558e6acSTreehugger Robot *
12*9558e6acSTreehugger Robot * Redistribution and use in source and binary forms, with or without
13*9558e6acSTreehugger Robot * modification, are permitted provided that the following conditions
14*9558e6acSTreehugger Robot * are met:
15*9558e6acSTreehugger Robot * 1. Redistributions of source code must retain the above copyright
16*9558e6acSTreehugger Robot * notice, this list of conditions and the following disclaimer.
17*9558e6acSTreehugger Robot * 2. Redistributions in binary form must reproduce the above copyright
18*9558e6acSTreehugger Robot * notice, this list of conditions and the following disclaimer in the
19*9558e6acSTreehugger Robot * documentation and/or other materials provided with the distribution.
20*9558e6acSTreehugger Robot * 3. Neither the name of the University nor the names of its contributors
21*9558e6acSTreehugger Robot * may be used to endorse or promote products derived from this software
22*9558e6acSTreehugger Robot * without specific prior written permission.
23*9558e6acSTreehugger Robot *
24*9558e6acSTreehugger Robot * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*9558e6acSTreehugger Robot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*9558e6acSTreehugger Robot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*9558e6acSTreehugger Robot * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*9558e6acSTreehugger Robot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*9558e6acSTreehugger Robot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*9558e6acSTreehugger Robot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*9558e6acSTreehugger Robot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*9558e6acSTreehugger Robot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*9558e6acSTreehugger Robot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*9558e6acSTreehugger Robot * SUCH DAMAGE.
35*9558e6acSTreehugger Robot */
36*9558e6acSTreehugger Robot
37*9558e6acSTreehugger Robot /* From FreeBSD sys/sys/param.h */
38*9558e6acSTreehugger Robot
39*9558e6acSTreehugger Robot #ifndef roundup2
40*9558e6acSTreehugger Robot #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
41*9558e6acSTreehugger Robot #endif
42*9558e6acSTreehugger Robot
43*9558e6acSTreehugger Robot /*-
44*9558e6acSTreehugger Robot * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
45*9558e6acSTreehugger Robot *
46*9558e6acSTreehugger Robot * Copyright (c) 2002 Thomas Moestl <[email protected]>
47*9558e6acSTreehugger Robot * All rights reserved.
48*9558e6acSTreehugger Robot *
49*9558e6acSTreehugger Robot * Redistribution and use in source and binary forms, with or without
50*9558e6acSTreehugger Robot * modification, are permitted provided that the following conditions
51*9558e6acSTreehugger Robot * are met:
52*9558e6acSTreehugger Robot * 1. Redistributions of source code must retain the above copyright
53*9558e6acSTreehugger Robot * notice, this list of conditions and the following disclaimer.
54*9558e6acSTreehugger Robot * 2. Redistributions in binary form must reproduce the above copyright
55*9558e6acSTreehugger Robot * notice, this list of conditions and the following disclaimer in the
56*9558e6acSTreehugger Robot * documentation and/or other materials provided with the distribution.
57*9558e6acSTreehugger Robot *
58*9558e6acSTreehugger Robot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59*9558e6acSTreehugger Robot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60*9558e6acSTreehugger Robot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61*9558e6acSTreehugger Robot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62*9558e6acSTreehugger Robot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63*9558e6acSTreehugger Robot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64*9558e6acSTreehugger Robot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65*9558e6acSTreehugger Robot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66*9558e6acSTreehugger Robot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67*9558e6acSTreehugger Robot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68*9558e6acSTreehugger Robot * SUCH DAMAGE.
69*9558e6acSTreehugger Robot */
70*9558e6acSTreehugger Robot
71*9558e6acSTreehugger Robot #include <stdint.h>
72*9558e6acSTreehugger Robot
73*9558e6acSTreehugger Robot /* From FreeBSD sys/sys/endian.h */
74*9558e6acSTreehugger Robot
75*9558e6acSTreehugger Robot static __inline void
le16enc(void * pp,uint16_t u)76*9558e6acSTreehugger Robot le16enc(void *pp, uint16_t u)
77*9558e6acSTreehugger Robot {
78*9558e6acSTreehugger Robot uint8_t *p = (uint8_t *)pp;
79*9558e6acSTreehugger Robot
80*9558e6acSTreehugger Robot p[0] = u & 0xff;
81*9558e6acSTreehugger Robot p[1] = (u >> 8) & 0xff;
82*9558e6acSTreehugger Robot }
83*9558e6acSTreehugger Robot
84*9558e6acSTreehugger Robot static __inline void
le32enc(void * pp,uint32_t u)85*9558e6acSTreehugger Robot le32enc(void *pp, uint32_t u)
86*9558e6acSTreehugger Robot {
87*9558e6acSTreehugger Robot uint8_t *p = (uint8_t *)pp;
88*9558e6acSTreehugger Robot
89*9558e6acSTreehugger Robot p[0] = u & 0xff;
90*9558e6acSTreehugger Robot p[1] = (u >> 8) & 0xff;
91*9558e6acSTreehugger Robot p[2] = (u >> 16) & 0xff;
92*9558e6acSTreehugger Robot p[3] = (u >> 24) & 0xff;
93*9558e6acSTreehugger Robot }
94*9558e6acSTreehugger Robot
95*9558e6acSTreehugger Robot static __inline uint16_t
le16dec(const void * pp)96*9558e6acSTreehugger Robot le16dec(const void *pp)
97*9558e6acSTreehugger Robot {
98*9558e6acSTreehugger Robot uint8_t const *p = (uint8_t const *)pp;
99*9558e6acSTreehugger Robot
100*9558e6acSTreehugger Robot return ((p[1] << 8) | p[0]);
101*9558e6acSTreehugger Robot }
102*9558e6acSTreehugger Robot
103*9558e6acSTreehugger Robot static __inline uint32_t
le32dec(const void * pp)104*9558e6acSTreehugger Robot le32dec(const void *pp)
105*9558e6acSTreehugger Robot {
106*9558e6acSTreehugger Robot uint8_t const *p = (uint8_t const *)pp;
107*9558e6acSTreehugger Robot
108*9558e6acSTreehugger Robot return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
109*9558e6acSTreehugger Robot }
110