Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmlibarchive/libarchive/archive_endian.h
3153 views
1
/*-
2
* Copyright (c) 2002 Thomas Moestl <[email protected]>
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
* Borrowed from FreeBSD's <sys/endian.h>
27
*/
28
29
#ifndef ARCHIVE_ENDIAN_H_INCLUDED
30
#define ARCHIVE_ENDIAN_H_INCLUDED
31
32
/* Note: This is a purely internal header! */
33
/* Do not use this outside of libarchive internal code! */
34
35
#ifndef __LIBARCHIVE_BUILD
36
#error This header is only to be used internally to libarchive.
37
#endif
38
39
/*
40
* Disabling inline keyword for compilers known to choke on it:
41
* - Watcom C++ in C code. (For any version?)
42
* - SGI MIPSpro
43
* - Microsoft Visual C++ 6.0 (supposedly newer versions too)
44
* - IBM VisualAge 6 (XL v6)
45
* - Sun WorkShop C (SunPro) before 5.9
46
*/
47
#if defined(__WATCOMC__) || defined(__sgi) || defined(__hpux) || defined(__BORLANDC__)
48
#define inline
49
#elif defined(__IBMC__) && __IBMC__ < 700
50
#define inline
51
#elif defined(__SUNPRO_C) && __SUNPRO_C < 0x590
52
#define inline
53
#elif defined(_MSC_VER) || defined(__osf__)
54
#define inline __inline
55
#endif
56
57
/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
58
59
static inline uint16_t
60
archive_be16dec(const void *pp)
61
{
62
unsigned char const *p = (unsigned char const *)pp;
63
64
/* Store into unsigned temporaries before left shifting, to avoid
65
promotion to signed int and then left shifting into the sign bit,
66
which is undefined behaviour. */
67
unsigned int p1 = p[1];
68
unsigned int p0 = p[0];
69
70
return ((p0 << 8) | p1);
71
}
72
73
static inline uint32_t
74
archive_be32dec(const void *pp)
75
{
76
unsigned char const *p = (unsigned char const *)pp;
77
78
/* Store into unsigned temporaries before left shifting, to avoid
79
promotion to signed int and then left shifting into the sign bit,
80
which is undefined behaviour. */
81
unsigned int p3 = p[3];
82
unsigned int p2 = p[2];
83
unsigned int p1 = p[1];
84
unsigned int p0 = p[0];
85
86
return ((p0 << 24) | (p1 << 16) | (p2 << 8) | p3);
87
}
88
89
static inline uint64_t
90
archive_be64dec(const void *pp)
91
{
92
unsigned char const *p = (unsigned char const *)pp;
93
94
return (((uint64_t)archive_be32dec(p) << 32) | archive_be32dec(p + 4));
95
}
96
97
static inline uint16_t
98
archive_le16dec(const void *pp)
99
{
100
unsigned char const *p = (unsigned char const *)pp;
101
102
/* Store into unsigned temporaries before left shifting, to avoid
103
promotion to signed int and then left shifting into the sign bit,
104
which is undefined behaviour. */
105
unsigned int p1 = p[1];
106
unsigned int p0 = p[0];
107
108
return ((p1 << 8) | p0);
109
}
110
111
static inline uint32_t
112
archive_le32dec(const void *pp)
113
{
114
unsigned char const *p = (unsigned char const *)pp;
115
116
/* Store into unsigned temporaries before left shifting, to avoid
117
promotion to signed int and then left shifting into the sign bit,
118
which is undefined behaviour. */
119
unsigned int p3 = p[3];
120
unsigned int p2 = p[2];
121
unsigned int p1 = p[1];
122
unsigned int p0 = p[0];
123
124
return ((p3 << 24) | (p2 << 16) | (p1 << 8) | p0);
125
}
126
127
static inline uint64_t
128
archive_le64dec(const void *pp)
129
{
130
unsigned char const *p = (unsigned char const *)pp;
131
132
return (((uint64_t)archive_le32dec(p + 4) << 32) | archive_le32dec(p));
133
}
134
135
static inline void
136
archive_be16enc(void *pp, uint16_t u)
137
{
138
unsigned char *p = (unsigned char *)pp;
139
140
p[0] = (u >> 8) & 0xff;
141
p[1] = u & 0xff;
142
}
143
144
static inline void
145
archive_be32enc(void *pp, uint32_t u)
146
{
147
unsigned char *p = (unsigned char *)pp;
148
149
p[0] = (u >> 24) & 0xff;
150
p[1] = (u >> 16) & 0xff;
151
p[2] = (u >> 8) & 0xff;
152
p[3] = u & 0xff;
153
}
154
155
static inline void
156
archive_be64enc(void *pp, uint64_t u)
157
{
158
unsigned char *p = (unsigned char *)pp;
159
160
archive_be32enc(p, (uint32_t)(u >> 32));
161
archive_be32enc(p + 4, (uint32_t)(u & 0xffffffff));
162
}
163
164
static inline void
165
archive_le16enc(void *pp, uint16_t u)
166
{
167
unsigned char *p = (unsigned char *)pp;
168
169
p[0] = u & 0xff;
170
p[1] = (u >> 8) & 0xff;
171
}
172
173
static inline void
174
archive_le32enc(void *pp, uint32_t u)
175
{
176
unsigned char *p = (unsigned char *)pp;
177
178
p[0] = u & 0xff;
179
p[1] = (u >> 8) & 0xff;
180
p[2] = (u >> 16) & 0xff;
181
p[3] = (u >> 24) & 0xff;
182
}
183
184
static inline void
185
archive_le64enc(void *pp, uint64_t u)
186
{
187
unsigned char *p = (unsigned char *)pp;
188
189
archive_le32enc(p, (uint32_t)(u & 0xffffffff));
190
archive_le32enc(p + 4, (uint32_t)(u >> 32));
191
}
192
193
#endif
194
195