This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtility.h
269 lines (243 loc) · 7.41 KB
/
Utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// Utility.hpp
//
// Copyright (c) Shareaza Development Team, 2002-2008.
// This file is part of SHAREAZA (shareaza.sourceforge.net)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#pragma once
#include <stdlib.h>
#include "interop.h"
// Work-around for Microsoft double declaration
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define _interlockedbittestandset _ms_set
#define _interlockedbittestandreset _ms_reset
#define _interlockedbittestandset64 _ms_set64
#define _interlockedbittestandreset64 _ms_reset64
#include <intrin.h>
#undef _interlockedbittestandset
#undef _interlockedbittestandreset
#undef _interlockedbittestandset
#undef _interlockedbittestandreset
#endif
//! \brief platform independent signed 8 bit integer type.
typedef char int8;
//! \brief platform independent signed 16 bit integer type.
typedef short int16;
//! \brief platform independent signed 32 bit integer type.
typedef long int32;
//! \brief platform independent signed 64 bit integer type.
typedef long long int64;
//! \brief platform independent unsigned 8 bit integer type.
typedef unsigned char uint8;
//! \brief platform independent unsigned 16 bit integer type.
typedef unsigned short uint16;
//! \brief platform independent unsigned 32 bit integer type.
typedef unsigned long uint32;
//! \brief platform independent unsigned 64 bit integer type.
typedef unsigned long long uint64;
//! \brief alias for unsigned char.
typedef unsigned char uchar;
//! \brief alias for signed char.
typedef signed char schar;
//! \brief alias for wchar_t.
typedef wchar_t wchar;
//! \brief unsigned integer type of the same size as a wchar_t
typedef uint16 uwchar;
//! \brief enumeration to specify the byte ordering of a sequence.
enum Endianess
{
//! \brief specifies little endian order,
//! the least significant byte comes first.
littleEndian,
//! \brief specifies big endian order,
//! the most significant byte comes first.
bigEndian
};
//! \brief This namespace is used to hold machine dependent definitions for
//! the target machine.
namespace Machine
{
//! \brief Specifies the natural byte ordering of the target machine.
//! \todo detect endianess during compilation.
const Endianess endianess = littleEndian; // x86
}
//! \brief generic function to swap the byte ordering of a given type
//!
//! The byte ordering can be swapped meaningfully only for unsigned integer types
//! therefore specializations are provided only for those types. We use
//! template specialization in order to avoid automatic argument conversion.
template <typename T>
struct SwapEndianess
{
};
template <>
struct SwapEndianess<uint8>
{
uint8 operator()(uint8 value) const { return value; }
};
template <>
struct SwapEndianess<uint16>
{
uint16 operator()(uint16 value) const
{
return _byteswap_ushort(value);
}
};
template <>
struct SwapEndianess<uint32>
{
uint32 operator()(uint32 value) const
{
return _byteswap_ulong(value);
}
};
template <>
struct SwapEndianess<uint64>
{
uint64 operator()(uint64 value) const
{
return _byteswap_uint64(value);
}
};
template <typename T>
inline T swapEndianess(T value)
{
return SwapEndianess<T>()(value);
}
//! \brief Generic function object to give its char serialization a given
//! specified byte ordering.
//!
//! The byte ordering of the argument is swapped unless it matches the byte
//! ordering of the target machine.
//! We use partial specialization to achieve this.
template <typename T, Endianess endianPolicy>
struct TransformTo
{
T operator()(T value) const { return swapEndianess<T>(value); }
};
template <typename T>
struct TransformTo<T, Machine::endianess>
{
T operator()(T value) const { return value; }
};
//! \brief Generic function object to reconstruct a value out of its serialized
//! form with a specified byte ordering.
//!
//! This function objects behaves the same as TransformTo does but its purpose
//! is different. Having both functions allows to make that purpose explicit in
//! code.
template <typename T, Endianess endianPolicy>
struct TransformFrom
{
T operator()(T value) const { return TransformTo<T, endianPolicy>()(value); }
};
//! \brief Generic function to bring a given value into little endian order.
template <typename T>
inline T transformToLE(T value)
{
return TransformTo<T, littleEndian>()(value);
}
//! \brief Generic function to bring a given value into big endian order.
template <typename T>
inline T transformToBE(T value)
{
return TransformTo<T, bigEndian>()(value);
}
//! \brief Generic function to reconstruct a given value from little endian
//! order.
template <typename T>
inline T transformFromLE(T value)
{
return TransformFrom<T, littleEndian>()(value);
}
//! \brief Generic function to reconstruct a given value from big endian
//! order.
template <typename T>
inline T transformFromBE(T value)
{
return TransformFrom<T, bigEndian>()(value);
}
template <typename T, T v>
struct StaticSwapEndianess;
template <uint8 v>
struct StaticSwapEndianess<uint8, v>
{
static const uint8 value = v;
};
template <uint16 v>
struct StaticSwapEndianess<uint16, v>
{
static const uint16 value = (v << 8) | (v >> 8);
};
template <uint32 v>
struct StaticSwapEndianess<uint32, v>
{
static const uint32 value = (v << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | (v >> 24);
};
template <uint64 v>
struct StaticSwapEndianess<uint64, v>
{
static const uint64 value = StaticSwapEndianess<uint32, (v >> 32)>::value | (uint64(StaticSwapEndianess<uint32, v>::value) << 32);
};
template <typename T, T v, Endianess>
struct StaticTransformTo
{
static const T value = StaticSwapEndianess<T, v>::value;
};
template <typename T, T v>
struct StaticTransformTo<T, v, Machine::endianess>
{
static const T value = v;
};
//! \brief for_each with predicate.
//!
//! A generalization of the for_each algorithm that takes a predicate that
//! must be fulfilled in order to apply the given function. This function
//! may mutate the input sequence, provided no iterators become invalid.
template <class InputIterator, class Predicate, class Function>
inline void for_each_if(InputIterator first, InputIterator last,
Predicate pred, Function f)
{
for (; first != last; ++first)
{
if (pred(*first))
f(*first);
}
}
//! Helper function to rotate the bits of a given unsigned value.
template <typename T>
inline T rotateLeft(T value, uint8 shift);
template <>
inline uint8 rotateLeft(uint8 value, uint8 shift)
{
return uint8(value << shift | value >> (8 - shift));
}
template <>
inline uint16 rotateLeft(uint16 value, uint8 shift)
{
return uint16(value << shift | value >> (16 - shift));
}
template <>
inline uint32 rotateLeft(uint32 value, uint8 shift)
{
return uint32(value << shift | value >> (32 - shift));
}
template <>
inline uint64 rotateLeft(uint64 value, uint8 shift)
{
return uint64(value << shift | value >> (64 - shift));
}