1 /*-
2  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  * $FreeBSD: src/lib/libarchive/archive_endian.h,v 1.4 2008/12/06 06:12:24 kientzle Exp $
27  *
28  * Borrowed from FreeBSD's <sys/endian.h>
29  */
30 
31 /* Note:  This is a purely internal header! */
32 /* Do not use this outside of libarchive internal code! */
33 
34 #ifndef ARCHIVE_ENDIAN_H_INCLUDED
35 #define ARCHIVE_ENDIAN_H_INCLUDED
36 
37 
38 /*
39  * Disabling inline keyword for compilers known to choke on it:
40  * - Watcom C++ in C code.  (For any version?)
41  * - SGI MIPSpro
42  * - Microsoft Visual C++ 6.0 (supposedly newer versions too)
43  */
44 #if defined(__WATCOMC__) || defined(__sgi)
45 #define	inline
46 #elif defined(_MSC_VER)
47 #define inline __inline
48 #endif
49 
50 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
51 
52 static inline uint16_t
53 archive_be16dec(const void *pp)
54 {
55 	unsigned char const *p = (unsigned char const *)pp;
56 
57 	return ((p[0] << 8) | p[1]);
58 }
59 
60 static inline uint32_t
61 archive_be32dec(const void *pp)
62 {
63 	unsigned char const *p = (unsigned char const *)pp;
64 
65 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
66 }
67 
68 static inline uint64_t
69 archive_be64dec(const void *pp)
70 {
71 	unsigned char const *p = (unsigned char const *)pp;
72 
73 	return (((uint64_t)archive_be32dec(p) << 32) | archive_be32dec(p + 4));
74 }
75 
76 static inline uint16_t
77 archive_le16dec(const void *pp)
78 {
79 	unsigned char const *p = (unsigned char const *)pp;
80 
81 	return ((p[1] << 8) | p[0]);
82 }
83 
84 static inline uint32_t
85 archive_le32dec(const void *pp)
86 {
87 	unsigned char const *p = (unsigned char const *)pp;
88 
89 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
90 }
91 
92 static inline uint64_t
93 archive_le64dec(const void *pp)
94 {
95 	unsigned char const *p = (unsigned char const *)pp;
96 
97 	return (((uint64_t)archive_le32dec(p + 4) << 32) | archive_le32dec(p));
98 }
99 
100 static inline void
101 archive_be16enc(void *pp, uint16_t u)
102 {
103 	unsigned char *p = (unsigned char *)pp;
104 
105 	p[0] = (u >> 8) & 0xff;
106 	p[1] = u & 0xff;
107 }
108 
109 static inline void
110 archive_be32enc(void *pp, uint32_t u)
111 {
112 	unsigned char *p = (unsigned char *)pp;
113 
114 	p[0] = (u >> 24) & 0xff;
115 	p[1] = (u >> 16) & 0xff;
116 	p[2] = (u >> 8) & 0xff;
117 	p[3] = u & 0xff;
118 }
119 
120 static inline void
121 archive_be64enc(void *pp, uint64_t u)
122 {
123 	unsigned char *p = (unsigned char *)pp;
124 
125 	archive_be32enc(p, u >> 32);
126 	archive_be32enc(p + 4, u & 0xffffffff);
127 }
128 
129 static inline void
130 archive_le16enc(void *pp, uint16_t u)
131 {
132 	unsigned char *p = (unsigned char *)pp;
133 
134 	p[0] = u & 0xff;
135 	p[1] = (u >> 8) & 0xff;
136 }
137 
138 static inline void
139 archive_le32enc(void *pp, uint32_t u)
140 {
141 	unsigned char *p = (unsigned char *)pp;
142 
143 	p[0] = u & 0xff;
144 	p[1] = (u >> 8) & 0xff;
145 	p[2] = (u >> 16) & 0xff;
146 	p[3] = (u >> 24) & 0xff;
147 }
148 
149 static inline void
150 archive_le64enc(void *pp, uint64_t u)
151 {
152 	unsigned char *p = (unsigned char *)pp;
153 
154 	archive_le32enc(p, u & 0xffffffff);
155 	archive_le32enc(p + 4, u >> 32);
156 }
157 
158 #endif
159