xref: /freebsd/usr.bin/mkimg/endian.h (revision 5aad7d9a874abf493b80737c67f6cffe9394b27e)
1*5aad7d9aSMarcel Moolenaar /*-
2*5aad7d9aSMarcel Moolenaar  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3*5aad7d9aSMarcel Moolenaar  * All rights reserved.
4*5aad7d9aSMarcel Moolenaar  *
5*5aad7d9aSMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
6*5aad7d9aSMarcel Moolenaar  * modification, are permitted provided that the following conditions
7*5aad7d9aSMarcel Moolenaar  * are met:
8*5aad7d9aSMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
9*5aad7d9aSMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
10*5aad7d9aSMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
11*5aad7d9aSMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
12*5aad7d9aSMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
13*5aad7d9aSMarcel Moolenaar  *
14*5aad7d9aSMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*5aad7d9aSMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*5aad7d9aSMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*5aad7d9aSMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*5aad7d9aSMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*5aad7d9aSMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*5aad7d9aSMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*5aad7d9aSMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*5aad7d9aSMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*5aad7d9aSMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*5aad7d9aSMarcel Moolenaar  * SUCH DAMAGE.
25*5aad7d9aSMarcel Moolenaar  *
26*5aad7d9aSMarcel Moolenaar  * $FreeBSD$
27*5aad7d9aSMarcel Moolenaar  */
28*5aad7d9aSMarcel Moolenaar 
29*5aad7d9aSMarcel Moolenaar #ifndef _MKIMG_ENDIAN_H_
30*5aad7d9aSMarcel Moolenaar #define _MKIMG_ENDIAN_H_
31*5aad7d9aSMarcel Moolenaar 
32*5aad7d9aSMarcel Moolenaar static __inline uint16_t
33*5aad7d9aSMarcel Moolenaar be16dec(const void *pp)
34*5aad7d9aSMarcel Moolenaar {
35*5aad7d9aSMarcel Moolenaar 	uint8_t const *p = (uint8_t const *)pp;
36*5aad7d9aSMarcel Moolenaar 
37*5aad7d9aSMarcel Moolenaar 	return ((p[0] << 8) | p[1]);
38*5aad7d9aSMarcel Moolenaar }
39*5aad7d9aSMarcel Moolenaar 
40*5aad7d9aSMarcel Moolenaar static __inline void
41*5aad7d9aSMarcel Moolenaar be16enc(void *pp, uint16_t u)
42*5aad7d9aSMarcel Moolenaar {
43*5aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
44*5aad7d9aSMarcel Moolenaar 
45*5aad7d9aSMarcel Moolenaar 	p[0] = (u >> 8) & 0xff;
46*5aad7d9aSMarcel Moolenaar 	p[1] = u & 0xff;
47*5aad7d9aSMarcel Moolenaar }
48*5aad7d9aSMarcel Moolenaar 
49*5aad7d9aSMarcel Moolenaar static __inline void
50*5aad7d9aSMarcel Moolenaar be32enc(void *pp, uint32_t u)
51*5aad7d9aSMarcel Moolenaar {
52*5aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
53*5aad7d9aSMarcel Moolenaar 
54*5aad7d9aSMarcel Moolenaar 	p[0] = (u >> 24) & 0xff;
55*5aad7d9aSMarcel Moolenaar 	p[1] = (u >> 16) & 0xff;
56*5aad7d9aSMarcel Moolenaar 	p[2] = (u >> 8) & 0xff;
57*5aad7d9aSMarcel Moolenaar 	p[3] = u & 0xff;
58*5aad7d9aSMarcel Moolenaar }
59*5aad7d9aSMarcel Moolenaar 
60*5aad7d9aSMarcel Moolenaar static __inline void
61*5aad7d9aSMarcel Moolenaar be64enc(void *pp, uint64_t u)
62*5aad7d9aSMarcel Moolenaar {
63*5aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
64*5aad7d9aSMarcel Moolenaar 
65*5aad7d9aSMarcel Moolenaar 	be32enc(p, (uint32_t)(u >> 32));
66*5aad7d9aSMarcel Moolenaar 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
67*5aad7d9aSMarcel Moolenaar }
68*5aad7d9aSMarcel Moolenaar 
69*5aad7d9aSMarcel Moolenaar static __inline uint16_t
70*5aad7d9aSMarcel Moolenaar le16dec(const void *pp)
71*5aad7d9aSMarcel Moolenaar {
72*5aad7d9aSMarcel Moolenaar 	uint8_t const *p = (uint8_t const *)pp;
73*5aad7d9aSMarcel Moolenaar 
74*5aad7d9aSMarcel Moolenaar 	return ((p[1] << 8) | p[0]);
75*5aad7d9aSMarcel Moolenaar }
76*5aad7d9aSMarcel Moolenaar 
77*5aad7d9aSMarcel Moolenaar static __inline void
78*5aad7d9aSMarcel Moolenaar le16enc(void *pp, uint16_t u)
79*5aad7d9aSMarcel Moolenaar {
80*5aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
81*5aad7d9aSMarcel Moolenaar 
82*5aad7d9aSMarcel Moolenaar 	p[0] = u & 0xff;
83*5aad7d9aSMarcel Moolenaar 	p[1] = (u >> 8) & 0xff;
84*5aad7d9aSMarcel Moolenaar }
85*5aad7d9aSMarcel Moolenaar 
86*5aad7d9aSMarcel Moolenaar static __inline void
87*5aad7d9aSMarcel Moolenaar le32enc(void *pp, uint32_t u)
88*5aad7d9aSMarcel Moolenaar {
89*5aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
90*5aad7d9aSMarcel Moolenaar 
91*5aad7d9aSMarcel Moolenaar 	p[0] = u & 0xff;
92*5aad7d9aSMarcel Moolenaar 	p[1] = (u >> 8) & 0xff;
93*5aad7d9aSMarcel Moolenaar 	p[2] = (u >> 16) & 0xff;
94*5aad7d9aSMarcel Moolenaar 	p[3] = (u >> 24) & 0xff;
95*5aad7d9aSMarcel Moolenaar }
96*5aad7d9aSMarcel Moolenaar 
97*5aad7d9aSMarcel Moolenaar static __inline void
98*5aad7d9aSMarcel Moolenaar le64enc(void *pp, uint64_t u)
99*5aad7d9aSMarcel Moolenaar {
100*5aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
101*5aad7d9aSMarcel Moolenaar 
102*5aad7d9aSMarcel Moolenaar 	le32enc(p, (uint32_t)(u & 0xffffffffU));
103*5aad7d9aSMarcel Moolenaar 	le32enc(p + 4, (uint32_t)(u >> 32));
104*5aad7d9aSMarcel Moolenaar }
105*5aad7d9aSMarcel Moolenaar 
106*5aad7d9aSMarcel Moolenaar #endif /* _MKIMG_ENDIAN_H_ */
107