2 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Borrowed from FreeBSD's <sys/endian.h>
31 #ifndef ARCHIVE_ENDIAN_H_INCLUDED
32 #define ARCHIVE_ENDIAN_H_INCLUDED
34 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
36 static inline uint16_t
37 be16dec(const void *pp)
39 unsigned char const *p = (unsigned char const *)pp;
41 return ((p[0] << 8) | p[1]);
44 static inline uint32_t
45 be32dec(const void *pp)
47 unsigned char const *p = (unsigned char const *)pp;
49 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
52 static inline uint64_t
53 be64dec(const void *pp)
55 unsigned char const *p = (unsigned char const *)pp;
57 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
60 static inline uint16_t
61 le16dec(const void *pp)
63 unsigned char const *p = (unsigned char const *)pp;
65 return ((p[1] << 8) | p[0]);
68 static inline uint32_t
69 le32dec(const void *pp)
71 unsigned char const *p = (unsigned char const *)pp;
73 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
76 static inline uint64_t
77 le64dec(const void *pp)
79 unsigned char const *p = (unsigned char const *)pp;
81 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
85 be16enc(void *pp, uint16_t u)
87 unsigned char *p = (unsigned char *)pp;
89 p[0] = (u >> 8) & 0xff;
94 be32enc(void *pp, uint32_t u)
96 unsigned char *p = (unsigned char *)pp;
98 p[0] = (u >> 24) & 0xff;
99 p[1] = (u >> 16) & 0xff;
100 p[2] = (u >> 8) & 0xff;
105 be64enc(void *pp, uint64_t u)
107 unsigned char *p = (unsigned char *)pp;
110 be32enc(p + 4, u & 0xffffffff);
114 le16enc(void *pp, uint16_t u)
116 unsigned char *p = (unsigned char *)pp;
119 p[1] = (u >> 8) & 0xff;
123 le32enc(void *pp, uint32_t u)
125 unsigned char *p = (unsigned char *)pp;
128 p[1] = (u >> 8) & 0xff;
129 p[2] = (u >> 16) & 0xff;
130 p[3] = (u >> 24) & 0xff;
134 le64enc(void *pp, uint64_t u)
136 unsigned char *p = (unsigned char *)pp;
138 le32enc(p, u & 0xffffffff);
139 le32enc(p + 4, u >> 32);