xref: /freebsd/usr.bin/mkimg/endian.h (revision 4dbd43cd05cdbf399587529a0e841287a982165d)
15aad7d9aSMarcel Moolenaar /*-
25aad7d9aSMarcel Moolenaar  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
35aad7d9aSMarcel Moolenaar  * All rights reserved.
45aad7d9aSMarcel Moolenaar  *
55aad7d9aSMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
65aad7d9aSMarcel Moolenaar  * modification, are permitted provided that the following conditions
75aad7d9aSMarcel Moolenaar  * are met:
85aad7d9aSMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
95aad7d9aSMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
105aad7d9aSMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
115aad7d9aSMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
125aad7d9aSMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
135aad7d9aSMarcel Moolenaar  *
145aad7d9aSMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
155aad7d9aSMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165aad7d9aSMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175aad7d9aSMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
185aad7d9aSMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195aad7d9aSMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
205aad7d9aSMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215aad7d9aSMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
225aad7d9aSMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
235aad7d9aSMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
245aad7d9aSMarcel Moolenaar  * SUCH DAMAGE.
255aad7d9aSMarcel Moolenaar  *
265aad7d9aSMarcel Moolenaar  * $FreeBSD$
275aad7d9aSMarcel Moolenaar  */
285aad7d9aSMarcel Moolenaar 
295aad7d9aSMarcel Moolenaar #ifndef _MKIMG_ENDIAN_H_
305aad7d9aSMarcel Moolenaar #define _MKIMG_ENDIAN_H_
315aad7d9aSMarcel Moolenaar 
32*4dbd43cdSXin LI #include <stdint.h>
33*4dbd43cdSXin LI 
345aad7d9aSMarcel Moolenaar static __inline uint16_t
355aad7d9aSMarcel Moolenaar be16dec(const void *pp)
365aad7d9aSMarcel Moolenaar {
375aad7d9aSMarcel Moolenaar 	uint8_t const *p = (uint8_t const *)pp;
385aad7d9aSMarcel Moolenaar 
395aad7d9aSMarcel Moolenaar 	return ((p[0] << 8) | p[1]);
405aad7d9aSMarcel Moolenaar }
415aad7d9aSMarcel Moolenaar 
425aad7d9aSMarcel Moolenaar static __inline void
435aad7d9aSMarcel Moolenaar be16enc(void *pp, uint16_t u)
445aad7d9aSMarcel Moolenaar {
455aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
465aad7d9aSMarcel Moolenaar 
475aad7d9aSMarcel Moolenaar 	p[0] = (u >> 8) & 0xff;
485aad7d9aSMarcel Moolenaar 	p[1] = u & 0xff;
495aad7d9aSMarcel Moolenaar }
505aad7d9aSMarcel Moolenaar 
515aad7d9aSMarcel Moolenaar static __inline void
525aad7d9aSMarcel Moolenaar be32enc(void *pp, uint32_t u)
535aad7d9aSMarcel Moolenaar {
545aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
555aad7d9aSMarcel Moolenaar 
565aad7d9aSMarcel Moolenaar 	p[0] = (u >> 24) & 0xff;
575aad7d9aSMarcel Moolenaar 	p[1] = (u >> 16) & 0xff;
585aad7d9aSMarcel Moolenaar 	p[2] = (u >> 8) & 0xff;
595aad7d9aSMarcel Moolenaar 	p[3] = u & 0xff;
605aad7d9aSMarcel Moolenaar }
615aad7d9aSMarcel Moolenaar 
625aad7d9aSMarcel Moolenaar static __inline void
635aad7d9aSMarcel Moolenaar be64enc(void *pp, uint64_t u)
645aad7d9aSMarcel Moolenaar {
655aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
665aad7d9aSMarcel Moolenaar 
675aad7d9aSMarcel Moolenaar 	be32enc(p, (uint32_t)(u >> 32));
685aad7d9aSMarcel Moolenaar 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
695aad7d9aSMarcel Moolenaar }
705aad7d9aSMarcel Moolenaar 
715aad7d9aSMarcel Moolenaar static __inline uint16_t
725aad7d9aSMarcel Moolenaar le16dec(const void *pp)
735aad7d9aSMarcel Moolenaar {
745aad7d9aSMarcel Moolenaar 	uint8_t const *p = (uint8_t const *)pp;
755aad7d9aSMarcel Moolenaar 
765aad7d9aSMarcel Moolenaar 	return ((p[1] << 8) | p[0]);
775aad7d9aSMarcel Moolenaar }
785aad7d9aSMarcel Moolenaar 
795aad7d9aSMarcel Moolenaar static __inline void
805aad7d9aSMarcel Moolenaar le16enc(void *pp, uint16_t u)
815aad7d9aSMarcel Moolenaar {
825aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
835aad7d9aSMarcel Moolenaar 
845aad7d9aSMarcel Moolenaar 	p[0] = u & 0xff;
855aad7d9aSMarcel Moolenaar 	p[1] = (u >> 8) & 0xff;
865aad7d9aSMarcel Moolenaar }
875aad7d9aSMarcel Moolenaar 
885aad7d9aSMarcel Moolenaar static __inline void
895aad7d9aSMarcel Moolenaar le32enc(void *pp, uint32_t u)
905aad7d9aSMarcel Moolenaar {
915aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
925aad7d9aSMarcel Moolenaar 
935aad7d9aSMarcel Moolenaar 	p[0] = u & 0xff;
945aad7d9aSMarcel Moolenaar 	p[1] = (u >> 8) & 0xff;
955aad7d9aSMarcel Moolenaar 	p[2] = (u >> 16) & 0xff;
965aad7d9aSMarcel Moolenaar 	p[3] = (u >> 24) & 0xff;
975aad7d9aSMarcel Moolenaar }
985aad7d9aSMarcel Moolenaar 
995aad7d9aSMarcel Moolenaar static __inline void
1005aad7d9aSMarcel Moolenaar le64enc(void *pp, uint64_t u)
1015aad7d9aSMarcel Moolenaar {
1025aad7d9aSMarcel Moolenaar 	uint8_t *p = (uint8_t *)pp;
1035aad7d9aSMarcel Moolenaar 
1045aad7d9aSMarcel Moolenaar 	le32enc(p, (uint32_t)(u & 0xffffffffU));
1055aad7d9aSMarcel Moolenaar 	le32enc(p + 4, (uint32_t)(u >> 32));
1065aad7d9aSMarcel Moolenaar }
1075aad7d9aSMarcel Moolenaar 
1085aad7d9aSMarcel Moolenaar #endif /* _MKIMG_ENDIAN_H_ */
109