forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtkrzw_sys_config.h
434 lines (370 loc) · 11.8 KB
/
tkrzw_sys_config.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*************************************************************************************************
* System-dependent configurations
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#ifndef _TKRZW_SYS_CONFIG_H
#define _TKRZW_SYS_CONFIG_H
#if __cplusplus < 201412L
#error expecting C++17 standard
#endif
#if defined(__linux__)
#define _SYS_LINUX_
#define _TKRZW_OSNAME "Linux"
#elif defined(__FreeBSD__)
#define _SYS_FREEBSD_
#define _TKRZW_OSNAME "FreeBSD"
#elif defined(__NetBSD__)
#define _SYS_NETBSD_
#define _TKRZW_OSNAME "NetBSD"
#elif defined(__OpenBSD__)
#define _SYS_OPENBSD_
#define _TKRZW_OSNAME "OpenBSD"
#elif defined(__sun__) || defined(__sun)
#define _SYS_SUNOS_
#define _TKRZW_OSNAME "SunOS"
#elif defined(__hpux)
#define _SYS_HPUX_
#define _TKRZW_OSNAME "HP-UX"
#elif defined(__osf)
#define _SYS_TRU64_
#define _TKRZW_OSNAME "Tru64"
#elif defined(_AIX)
#define _SYS_AIX_
#define _TKRZW_OSNAME "AIX"
#elif defined(__APPLE__) && defined(__MACH__)
#define _SYS_MACOSX_
#define _TKRZW_OSNAME "Mac OS X"
#elif defined(_MSC_VER)
#define _SYS_MSVC_
#define _TKRZW_OSNAME "Windows (VC++)"
#elif defined(_WIN32)
#define _SYS_MINGW_
#define _TKRZW_OSNAME "Windows (MinGW)"
#elif defined(__CYGWIN__)
#define _SYS_CYGWIN_
#define _TKRZW_OSNAME "Windows (Cygwin)"
#else
#define _SYS_GENERIC_
#define _TKRZW_OSNAME "Generic"
#endif
#include <algorithm>
#include <atomic>
#include <chrono>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <limits>
#include <list>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <random>
#include <regex>
#include <set>
#include <shared_mutex>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <cassert>
#include <cerrno>
#include <cmath>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
extern "C" {
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <dirent.h>
} // extern "C"
namespace tkrzw {
/**
* Makes a hexadecimal expression of a buffer.
* @param buf The soruce buffer.
* @param size The size of the source buffer.
* @param folding The number of columns in one line. 0 means no folding.
* @return The hexadecimal string.
*/
std::string HexDump(const void* buf, size_t size, int32_t folding = 0);
/**
* Makes a hexadecimal expression of a string.
* @param str The string.
* @param folding The number of columns in one line. 0 means no folding.
* @return The hexadecimal string.
*/
std::string HexDumpStr(const std::string_view& str, int32_t folding = 0);
/**
* Normalizes a 16-bit number in the native order into the network byte order.
* @param num The 16-bit number in the native order.
* @return The number in the network byte order.
*/
uint16_t HostToNet16(uint16_t num);
/**
* Normalizes a 32-bit number in the native order into the network byte order.
* @param num The 32-bit number in the native order.
* @return The number in the network byte order.
*/
uint32_t HostToNet32(uint32_t num);
/**
* Normalizes a 64-bit number in the native order into the network byte order.
* @param num The 64-bit number in the native order.
* @return The number in the network byte order.
*/
uint64_t HostToNet64(uint64_t num);
/**
* Denormalizes a 16-bit number in the network byte order into the native order.
* @param num The 16-bit number in the network byte order.
* @return The converted number in the native order.
*/
uint16_t NetToHost16(uint16_t num);
/**
* Denormalizes a 32-bit number in the network byte order into the native order.
* @param num The 32-bit number in the network byte order.
* @return The converted number in the native order.
*/
uint32_t NetToHost32(uint32_t num);
/**
* Denormalizes a 64-bit number in the network byte order into the native order.
* @param num The 64-bit number in the network byte order.
* @return The converted number in the native order.
*/
uint64_t NetToHost64(uint64_t num);
/**
* Writes a number in fixed length format into a buffer.
* @param buf The desitination buffer.
* @param num The number.
* @param width The width of bytes from the LSB.
*/
void WriteFixNum(void* buf, uint64_t num, size_t width);
/**
* Reads a number in fixed length format from a buffer.
* @param buf The source buffer.
* @param width The width of bytes from the LSB.
* @return The read number.
*/
uint64_t ReadFixNum(const void* buf, size_t width);
/**
* Writes a number in variable length format into a buffer.
* @param buf The desitination buffer.
* @param num The number.
* @return The length of the written region.
*/
size_t WriteVarNum(void* buf, uint64_t num);
/**
* Reads a number in variable length format from a buffer.
* @param buf The source buffer.
* @param size The size of the source buffer.
* @param np The pointer to the variable into which the read number is assigned.
* @return The length of the read region, or 0 on failure.
*/
size_t ReadVarNum(const void* buf, size_t size, uint64_t* np);
/**
* Checks the size of variable length format of a number.
* @return The size of variable length format.
*/
size_t SizeVarNum(uint64_t num);
#if defined(_TKRWX_BIGEND)
constexpr bool _IS_BIG_ENDIAN = true;
#else
constexpr bool _IS_BIG_ENDIAN = false;
#endif
constexpr int32_t FILEPERM = 00644;
constexpr int32_t DIRPERM = 00755;
inline std::string HexDump(const void* buf, size_t size, int32_t folding) {
const unsigned char* rp = (const unsigned char*)buf;
const unsigned char* ep = rp + size;
std::string str;
int32_t nc = 0;
while (rp < ep) {
if (rp > buf) {
if (++nc == folding) {
str += "\n";
nc = 0;
} else {
str += " ";
}
}
const int32_t upper = (*rp & 0xF0) >> 4;
if (upper < 10) {
str.append(1, upper + '0');
} else {
str.append(1, upper - 10 + 'A');
}
const int32_t lower = *rp & 0x0F;
if (lower < 10) {
str.append(1, lower + '0');
} else {
str.append(1, lower - 10 + 'A');
}
rp++;
}
return str;
}
inline std::string HexDumpStr(const std::string_view& str, int32_t folding) {
return HexDump(str.data(), str.size(), folding);
}
inline uint16_t HostToNet16(uint16_t num) {
if (_IS_BIG_ENDIAN) return num;
return ((num & 0x00ffU) << 8) | ((num & 0xff00U) >> 8);
}
inline uint32_t HostToNet32(uint32_t num) {
if (_IS_BIG_ENDIAN) return num;
return ((num & 0x000000ffUL) << 24) | ((num & 0x0000ff00UL) << 8) | \
((num & 0x00ff0000UL) >> 8) | ((num & 0xff000000UL) >> 24);
}
inline uint64_t HostToNet64(uint64_t num) {
if (_IS_BIG_ENDIAN) return num;
return ((num & 0x00000000000000ffULL) << 56) | ((num & 0x000000000000ff00ULL) << 40) |
((num & 0x0000000000ff0000ULL) << 24) | ((num & 0x00000000ff000000ULL) << 8) |
((num & 0x000000ff00000000ULL) >> 8) | ((num & 0x0000ff0000000000ULL) >> 24) |
((num & 0x00ff000000000000ULL) >> 40) | ((num & 0xff00000000000000ULL) >> 56);
}
inline uint16_t NetToHost16(uint16_t num) {
return HostToNet16(num);
}
inline uint32_t NetToHost32(uint32_t num) {
return HostToNet32(num);
}
inline uint64_t NetToHost64(uint64_t num) {
return HostToNet64(num);
}
inline void WriteFixNum(void* buf, uint64_t num, size_t width) {
assert(buf != nullptr && width <= sizeof(int64_t));
num = HostToNet64(num);
std::memcpy(buf, (const char*)&num + sizeof(num) - width, width);
}
inline uint64_t ReadFixNum(const void* buf, size_t width) {
assert(buf != nullptr && width <= sizeof(int64_t));
uint64_t num = 0;
std::memcpy(&num, buf, width);
return NetToHost64(num) >> ((sizeof(num) - width) * 8);
}
inline size_t WriteVarNum(void* buf, uint64_t num) {
assert(buf != nullptr);
unsigned char* wp = (unsigned char*)buf;
if (num < (1ULL << 7)) {
*(wp++) = num;
} else if (num < (1ULL << 14)) {
*(wp++) = (num >> 7) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 21)) {
*(wp++) = (num >> 14) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 28)) {
*(wp++) = (num >> 21) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 35)) {
*(wp++) = (num >> 28) | 0x80;
*(wp++) = ((num >> 21) & 0x7f) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 42)) {
*(wp++) = (num >> 35) | 0x80;
*(wp++) = ((num >> 28) & 0x7f) | 0x80;
*(wp++) = ((num >> 21) & 0x7f) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 49)) {
*(wp++) = (num >> 42) | 0x80;
*(wp++) = ((num >> 35) & 0x7f) | 0x80;
*(wp++) = ((num >> 28) & 0x7f) | 0x80;
*(wp++) = ((num >> 21) & 0x7f) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 56)) {
*(wp++) = (num >> 49) | 0x80;
*(wp++) = ((num >> 42) & 0x7f) | 0x80;
*(wp++) = ((num >> 35) & 0x7f) | 0x80;
*(wp++) = ((num >> 28) & 0x7f) | 0x80;
*(wp++) = ((num >> 21) & 0x7f) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else if (num < (1ULL << 63)) {
*(wp++) = (num >> 56) | 0x80;
*(wp++) = ((num >> 49) & 0x7f) | 0x80;
*(wp++) = ((num >> 42) & 0x7f) | 0x80;
*(wp++) = ((num >> 35) & 0x7f) | 0x80;
*(wp++) = ((num >> 28) & 0x7f) | 0x80;
*(wp++) = ((num >> 21) & 0x7f) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
} else {
*(wp++) = (num >> 63) | 0x80;
*(wp++) = ((num >> 56) & 0x7f) | 0x80;
*(wp++) = ((num >> 49) & 0x7f) | 0x80;
*(wp++) = ((num >> 42) & 0x7f) | 0x80;
*(wp++) = ((num >> 35) & 0x7f) | 0x80;
*(wp++) = ((num >> 28) & 0x7f) | 0x80;
*(wp++) = ((num >> 21) & 0x7f) | 0x80;
*(wp++) = ((num >> 14) & 0x7f) | 0x80;
*(wp++) = ((num >> 7) & 0x7f) | 0x80;
*(wp++) = num & 0x7f;
}
return wp - (unsigned char*)buf;
}
inline size_t ReadVarNum(const void* buf, size_t size, uint64_t* np) {
assert(buf != nullptr && size <= (1 << 30) && np != nullptr);
const unsigned char* rp = (const unsigned char*)buf;
const unsigned char* ep = rp + size;
uint64_t num = 0;
uint32_t c;
do {
if (rp >= ep) {
*np = 0;
return 0;
}
c = *rp;
num = (num << 7) + (c & 0x7f);
rp++;
} while (c >= 0x80);
*np = num;
return rp - (const unsigned char*)buf;
}
inline size_t SizeVarNum(uint64_t num) {
if (num < (1ULL << 7)) return 1;
if (num < (1ULL << 14)) return 2;
if (num < (1ULL << 21)) return 3;
if (num < (1ULL << 28)) return 4;
if (num < (1ULL << 35)) return 5;
if (num < (1ULL << 42)) return 6;
if (num < (1ULL << 49)) return 7;
if (num < (1ULL << 56)) return 8;
if (num < (1ULL << 63)) return 9;
return 10;
}
} // namespace tkrzw
#endif // _TKRZW_SYS_CONFIG_H
// END OF FILE