HAMMER 60I/Many: Mirroring
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_endian.h
blob61af4161c3798d49ac5d40212a390c3db5bbcff4
1 /*-
2 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/lib/libarchive/archive_endian.h,v 1.2 2008/02/26 07:17:47 kientzle Exp $
28 * Borrowed from FreeBSD's <sys/endian.h>
31 /* Note: This is a purely internal header! */
32 /* Do not use this outside of libarchive internal code! */
34 #ifndef ARCHIVE_ENDIAN_H_INCLUDED
35 #define ARCHIVE_ENDIAN_H_INCLUDED
38 /* Watcom C++ doesn't support 'inline' in C code. (For any version?) */
39 #if defined( __WATCOMC__ )
40 #define inline
41 #endif
43 /* Visual C++ 6.0 doesn't support 'inline' in C code. (Does VC7? VC8?) */
44 #if defined(_MSC_VER)
45 #define inline
46 #endif
48 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
50 static inline uint16_t
51 archive_be16dec(const void *pp)
53 unsigned char const *p = (unsigned char const *)pp;
55 return ((p[0] << 8) | p[1]);
58 static inline uint32_t
59 archive_be32dec(const void *pp)
61 unsigned char const *p = (unsigned char const *)pp;
63 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
66 static inline uint64_t
67 archive_be64dec(const void *pp)
69 unsigned char const *p = (unsigned char const *)pp;
71 return (((uint64_t)archive_be32dec(p) << 32) | archive_be32dec(p + 4));
74 static inline uint16_t
75 archive_le16dec(const void *pp)
77 unsigned char const *p = (unsigned char const *)pp;
79 return ((p[1] << 8) | p[0]);
82 static inline uint32_t
83 archive_le32dec(const void *pp)
85 unsigned char const *p = (unsigned char const *)pp;
87 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
90 static inline uint64_t
91 archive_le64dec(const void *pp)
93 unsigned char const *p = (unsigned char const *)pp;
95 return (((uint64_t)archive_le32dec(p + 4) << 32) | archive_le32dec(p));
98 static inline void
99 archive_be16enc(void *pp, uint16_t u)
101 unsigned char *p = (unsigned char *)pp;
103 p[0] = (u >> 8) & 0xff;
104 p[1] = u & 0xff;
107 static inline void
108 archive_be32enc(void *pp, uint32_t u)
110 unsigned char *p = (unsigned char *)pp;
112 p[0] = (u >> 24) & 0xff;
113 p[1] = (u >> 16) & 0xff;
114 p[2] = (u >> 8) & 0xff;
115 p[3] = u & 0xff;
118 static inline void
119 archive_be64enc(void *pp, uint64_t u)
121 unsigned char *p = (unsigned char *)pp;
123 archive_be32enc(p, u >> 32);
124 archive_be32enc(p + 4, u & 0xffffffff);
127 static inline void
128 archive_le16enc(void *pp, uint16_t u)
130 unsigned char *p = (unsigned char *)pp;
132 p[0] = u & 0xff;
133 p[1] = (u >> 8) & 0xff;
136 static inline void
137 archive_le32enc(void *pp, uint32_t u)
139 unsigned char *p = (unsigned char *)pp;
141 p[0] = u & 0xff;
142 p[1] = (u >> 8) & 0xff;
143 p[2] = (u >> 16) & 0xff;
144 p[3] = (u >> 24) & 0xff;
147 static inline void
148 archive_le64enc(void *pp, uint64_t u)
150 unsigned char *p = (unsigned char *)pp;
152 archive_le32enc(p, u & 0xffffffff);
153 archive_le32enc(p + 4, u >> 32);
156 #endif