-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
131 lines (96 loc) · 3.53 KB
/
common.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
#ifndef COMMON_H
#define COMMON_H
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#define _DARWIN_USE_64_BIT_INODE
#include <fcntl.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "banned.h"
#define MAGIC_OFFSET ((uint64_t) 1572864u)
#define IMAGE_SIZE ((uint64_t) 1474560u)
#define LABEL_OFFSET 3u
#define IMAGES_GAP 98304
#define FAT_MAGIC_1 0xEB3C9000u
#define FAT_MAGIC_2 0xEB3E9000u
#define FAT_MAGIC_3 0xEB3F9000u
#define FAT_MAGIC_4 0xEB1E9000u
#define GOTEK_MAX_FDDS 1000u
#define NONNULLARGS __attribute__((nonnull))
enum RET_CODES
{
FAIL_COMPILER = -1, /* makes compiler happy and allows implicit conversion to int without warning */
FAIL_SUCC = EXIT_SUCCESS,
FAIL_ARGS = 1,
FAIL_OPEN = 2,
FAIL_LSEEK = 10,
FAIL_READ = 11,
FAIL_WRITE = 12,
FAIL_CLOSE = 13,
FAIL_FSTAT = 14,
FAIL_IOCTL = 20,
FAIL_MMAP = 30,
FAIL_CHRNOTSUPP = 40,
FAIL_NOTSUPP = 41,
FAIL_NOMETA = 45,
FAIL_FAIL = 50,
};
#define CHECK_ERROR_GENERIC(f, t, e) ({ \
__typeof__(t) __rc = (f); \
if (__rc == -1) { \
fprintf(stderr, "%s: %s: %s\n", __func__, #f, strerror(errno)); \
return((e)); \
} \
__rc; \
})
#define CHECK_ERROR(f, e) CHECK_ERROR_GENERIC(f, int, e)
#define CHECK_ERROR_MMAP(f, e) ({ \
void *__rc_p = (f); \
if (__rc_p == MAP_FAILED) { \
fprintf(stderr, "%s: %s: %s\n", __func__, #f, strerror(errno)); \
return((e)); \
} \
__rc_p; \
})
#define CHECK_ERROR_NOVALRET(f, e) (void)CHECK_ERROR_GENERIC(f, int, e)
/* Functions */
void safe_close(int *const fd);
/* Macros */
#define BSWAP16(value) \
((((value) & 0xff) << 8) | ((value) >> 8))
#define BSWAP32(value) \
(((uint32_t)BSWAP16_ONLY_ON_LE((uint16_t)((value) & 0xffff)) << 16) | \
(uint32_t)BSWAP16_ONLY_ON_LE((uint16_t)((value) >> 16)))
/* Compatibility */
#if defined(__APPLE__) && defined(__MACH__)
#define ADDITIONAL_OPEN_FLAGS 0
#if defined(__BIG_ENDIAN__)
#define BSWAP16_ONLY_ON_LE(value) (value)
#define BSWAP32_ONLY_ON_LE(value) (value)
#define BSWAP16_ONLY_ON_BE(value) BSWAP16(value)
#define BSWAP32_ONLY_ON_BE(value) BSWAP32(value)
#else
#define BSWAP16_ONLY_ON_LE(value) BSWAP16(value)
#define BSWAP32_ONLY_ON_LE(value) BSWAP32(value)
#define BSWAP16_ONLY_ON_BE(value) (value)
#define BSWAP32_ONLY_ON_BE(value) (value)
#endif
#elif defined(__linux__)
#include <endian.h>
#if BYTE_ORDER == LITTLE_ENDIAN
#define BSWAP16_ONLY_ON_LE(value) (htobe16(value))
#define BSWAP32_ONLY_ON_LE(value) (htobe32(value))
#define BSWAP16_ONLY_ON_BE(value) (value)
#define BSWAP32_ONLY_ON_BE(value) (value)
#else
#define BSWAP16_ONLY_ON_LE(value) (value)
#define BSWAP32_ONLY_ON_LE(value) (value)
#define BSWAP16_ONLY_ON_BE(value) (htobe16(value))
#define BSWAP32_ONLY_ON_BE(value) (htobe32(value))
#endif
#define ADDITIONAL_OPEN_FLAGS O_LARGEFILE
#endif /* MACOS and Linux checks */
#endif /* COMMON_H */