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
275aad7d9aSMarcel Moolenaar #ifndef _MKIMG_ENDIAN_H_
285aad7d9aSMarcel Moolenaar #define _MKIMG_ENDIAN_H_
295aad7d9aSMarcel Moolenaar
30*4dbd43cdSXin LI #include <stdint.h>
31*4dbd43cdSXin LI
325aad7d9aSMarcel Moolenaar static __inline uint16_t
be16dec(const void * pp)335aad7d9aSMarcel Moolenaar be16dec(const void *pp)
345aad7d9aSMarcel Moolenaar {
355aad7d9aSMarcel Moolenaar uint8_t const *p = (uint8_t const *)pp;
365aad7d9aSMarcel Moolenaar
375aad7d9aSMarcel Moolenaar return ((p[0] << 8) | p[1]);
385aad7d9aSMarcel Moolenaar }
395aad7d9aSMarcel Moolenaar
405aad7d9aSMarcel Moolenaar static __inline void
be16enc(void * pp,uint16_t u)415aad7d9aSMarcel Moolenaar be16enc(void *pp, uint16_t u)
425aad7d9aSMarcel Moolenaar {
435aad7d9aSMarcel Moolenaar uint8_t *p = (uint8_t *)pp;
445aad7d9aSMarcel Moolenaar
455aad7d9aSMarcel Moolenaar p[0] = (u >> 8) & 0xff;
465aad7d9aSMarcel Moolenaar p[1] = u & 0xff;
475aad7d9aSMarcel Moolenaar }
485aad7d9aSMarcel Moolenaar
495aad7d9aSMarcel Moolenaar static __inline void
be32enc(void * pp,uint32_t u)505aad7d9aSMarcel Moolenaar be32enc(void *pp, uint32_t u)
515aad7d9aSMarcel Moolenaar {
525aad7d9aSMarcel Moolenaar uint8_t *p = (uint8_t *)pp;
535aad7d9aSMarcel Moolenaar
545aad7d9aSMarcel Moolenaar p[0] = (u >> 24) & 0xff;
555aad7d9aSMarcel Moolenaar p[1] = (u >> 16) & 0xff;
565aad7d9aSMarcel Moolenaar p[2] = (u >> 8) & 0xff;
575aad7d9aSMarcel Moolenaar p[3] = u & 0xff;
585aad7d9aSMarcel Moolenaar }
595aad7d9aSMarcel Moolenaar
605aad7d9aSMarcel Moolenaar static __inline void
be64enc(void * pp,uint64_t u)615aad7d9aSMarcel Moolenaar be64enc(void *pp, uint64_t u)
625aad7d9aSMarcel Moolenaar {
635aad7d9aSMarcel Moolenaar uint8_t *p = (uint8_t *)pp;
645aad7d9aSMarcel Moolenaar
655aad7d9aSMarcel Moolenaar be32enc(p, (uint32_t)(u >> 32));
665aad7d9aSMarcel Moolenaar be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
675aad7d9aSMarcel Moolenaar }
685aad7d9aSMarcel Moolenaar
695aad7d9aSMarcel Moolenaar static __inline uint16_t
le16dec(const void * pp)705aad7d9aSMarcel Moolenaar le16dec(const void *pp)
715aad7d9aSMarcel Moolenaar {
725aad7d9aSMarcel Moolenaar uint8_t const *p = (uint8_t const *)pp;
735aad7d9aSMarcel Moolenaar
745aad7d9aSMarcel Moolenaar return ((p[1] << 8) | p[0]);
755aad7d9aSMarcel Moolenaar }
765aad7d9aSMarcel Moolenaar
775aad7d9aSMarcel Moolenaar static __inline void
le16enc(void * pp,uint16_t u)785aad7d9aSMarcel Moolenaar le16enc(void *pp, uint16_t u)
795aad7d9aSMarcel Moolenaar {
805aad7d9aSMarcel Moolenaar uint8_t *p = (uint8_t *)pp;
815aad7d9aSMarcel Moolenaar
825aad7d9aSMarcel Moolenaar p[0] = u & 0xff;
835aad7d9aSMarcel Moolenaar p[1] = (u >> 8) & 0xff;
845aad7d9aSMarcel Moolenaar }
855aad7d9aSMarcel Moolenaar
865aad7d9aSMarcel Moolenaar static __inline void
le32enc(void * pp,uint32_t u)875aad7d9aSMarcel Moolenaar le32enc(void *pp, uint32_t u)
885aad7d9aSMarcel Moolenaar {
895aad7d9aSMarcel Moolenaar uint8_t *p = (uint8_t *)pp;
905aad7d9aSMarcel Moolenaar
915aad7d9aSMarcel Moolenaar p[0] = u & 0xff;
925aad7d9aSMarcel Moolenaar p[1] = (u >> 8) & 0xff;
935aad7d9aSMarcel Moolenaar p[2] = (u >> 16) & 0xff;
945aad7d9aSMarcel Moolenaar p[3] = (u >> 24) & 0xff;
955aad7d9aSMarcel Moolenaar }
965aad7d9aSMarcel Moolenaar
975aad7d9aSMarcel Moolenaar static __inline void
le64enc(void * pp,uint64_t u)985aad7d9aSMarcel Moolenaar le64enc(void *pp, uint64_t u)
995aad7d9aSMarcel Moolenaar {
1005aad7d9aSMarcel Moolenaar uint8_t *p = (uint8_t *)pp;
1015aad7d9aSMarcel Moolenaar
1025aad7d9aSMarcel Moolenaar le32enc(p, (uint32_t)(u & 0xffffffffU));
1035aad7d9aSMarcel Moolenaar le32enc(p + 4, (uint32_t)(u >> 32));
1045aad7d9aSMarcel Moolenaar }
1055aad7d9aSMarcel Moolenaar
1065aad7d9aSMarcel Moolenaar #endif /* _MKIMG_ENDIAN_H_ */
107