forked from shizukachan/atelier_pak_decrypt
-
Notifications
You must be signed in to change notification settings - Fork 25
/
util.h
333 lines (285 loc) · 8.56 KB
/
util.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
/*
Common code for Gust (Koei/Tecmo) PC games tools
Copyright © 2019-2021 VitaSmith
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#if !defined(_WIN32)
#include <libgen.h>
#endif
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#pragma once
// Flags to indicate whether the data being processed or the platform are Big Endian
typedef enum { big_endian = 0, little_endian = !0 } endianness;
extern endianness data_endianness;
extern const endianness platform_endianness;
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
#ifndef GUST_TOOLS_VERSION
#define GUST_TOOLS_VERSION_STR "[DEV VERSION]"
#else
#define GUST_TOOLS_VERSION_STR STRINGIFY(GUST_TOOLS_VERSION)
#endif
#if defined(_WIN32)
#include <windows.h>
#define ftell64 _ftelli64
#define fseek64 _fseeki64
#if !defined(S_ISDIR)
#define S_ISDIR(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFDIR)
#endif
#if !defined(S_ISREG)
#define S_ISREG(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFREG)
#endif
#define CREATE_DIR(path) CreateDirectory_utf8(path, NULL)
#define PATH_SEP '\\'
#else
#if defined(__APPLE__)
#define ftell64 ftello
#define fseek64 fseeko
#else
#define ftell64 ftello64
#define fseek64 fseeko64
#endif
#define CREATE_DIR(path) (mkdir(path, 0755) == 0)
#define PATH_SEP '/'
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef array_size
#define array_size(a) (sizeof(a) / sizeof(*a))
#endif
#ifndef is_power_of_2
#define is_power_of_2(x) (((x) & ((x) - 1)) == 0)
#endif
#define align_to_4(x) (((x) + 0x3) & ~0x3)
#define align_to_8(x) (((x) + 0x7) & ~0x7)
#define align_to_16(x) (((x) + 0xf) & ~0xf)
#define sizeof_16 (uint16_t)sizeof
#define sizeof_32 (uint32_t)sizeof
#define sizeof_64 (uint64_t)sizeof
#if defined(_WIN32)
char* _basename_win32(const char* path, bool remove_extension);
char* _dirname_win32(const char* path);
#define _basename(path) _basename_win32(path, false)
#define _appname(path) _basename_win32(path, true)
#define _dirname(path) _dirname_win32(path)
#else
char* _basename_unix(const char* path);
char* _dirname_unix(const char* path);
#define _basename(path) _basename_unix(path)
#define _appname(path) _basename_unix(path)
#define _dirname(path) _dirname_unix(path)
#define stricmp strcasecmp
#endif
#if defined (_MSC_VER)
#include <stdlib.h>
#define bswap_uint16 _byteswap_ushort
#define bswap_uint32 _byteswap_ulong
#define bswap_uint64 _byteswap_uint64
#else
#define bswap_uint16 __builtin_bswap16
#define bswap_uint32 __builtin_bswap32
#define bswap_uint64 __builtin_bswap64
#endif
#define BSWAP_UINT16(x) x = bswap_uint16(x)
#define BSWAP_UINT32(x) x = bswap_uint32(x)
#define BSWAP_UINT64(x) x = bswap_uint64(x)
// Returns the position of the msb/lsb. v should be nonzero
static __inline uint32_t find_msb(uint32_t v)
{
#if defined (_MSC_VER)
DWORD pos;
BitScanReverse(&pos, v);
return pos;
#else
return 31- __builtin_clz(v);
#endif
}
static __inline uint32_t find_lsb(uint64_t v)
{
#if defined (_MSC_VER)
DWORD pos;
BitScanForward64(&pos, v);
return pos;
#else
return __builtin_ctzll(v);
#endif
}
static __inline uint16_t getle16(const void* p)
{
uint16_t v = *(const uint16_t*)(const uint8_t*)(p);
return (platform_endianness == little_endian) ? v : bswap_uint16(v);
}
static __inline void setle16(const void* p, const uint16_t v)
{
if (platform_endianness == little_endian)
*((uint16_t*)p) = v;
else
*((uint16_t*)p) = bswap_uint16(v);
}
static __inline uint16_t getbe16(const void* p)
{
if (platform_endianness == little_endian)
return bswap_uint16(getle16(p));
else
return *(const uint16_t*)(const uint8_t*)(p);
}
static __inline void setbe16(const void* p, const uint16_t v)
{
if (platform_endianness == little_endian)
*((uint16_t*)p) = bswap_uint16(v);
else
*((uint16_t*)p) = v;
}
static __inline uint16_t getv16(uint16_t v)
{
return (platform_endianness == data_endianness) ? v : bswap_uint16(v);
}
static __inline uint16_t getp16(const void* p)
{
return getv16(*(const uint16_t*)(const uint8_t*)(p));
}
static __inline uint32_t getle24(const void* _p)
{
uint8_t* p = (uint8_t*)_p;
if (platform_endianness == little_endian)
return p[0] | (p[1] << 8) | (p[2] << 16);
else
return (p[0] << 16) | (p[1] << 8) | p[2];
}
static __inline void setle24(const void* _p, const uint32_t v)
{
uint8_t* p = (uint8_t*)_p;
if (platform_endianness == little_endian) {
p[0] = v & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
} else {
p[0] = (v >> 16) & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = v & 0xff;
}
}
static __inline uint32_t getbe24(const void* _p)
{
uint8_t* p = (uint8_t*)_p;
if (platform_endianness == little_endian)
return (p[0] << 16) | (p[1] << 8) | p[2];
else
return p[0] | (p[1] << 8) | (p[2] << 16);
}
static __inline void setbe24(const void* _p, const uint32_t v)
{
uint8_t* p = (uint8_t*)_p;
if (platform_endianness == little_endian) {
p[0] = (v >> 16) & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = v & 0xff;
} else {
p[0] = v & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
}
}
static __inline uint32_t getle32(const void* p)
{
uint32_t v = *(const uint32_t*)(const uint8_t*)(p);
return (platform_endianness == little_endian) ? v : bswap_uint32(v);
}
static __inline void setle32(const void* p, const uint32_t v)
{
if (platform_endianness == little_endian)
*((uint32_t*)p) = v;
else
*((uint32_t*)p) = bswap_uint32(v);
}
static __inline uint32_t getbe32(const void* p)
{
uint32_t v = *(const uint32_t*)(const uint8_t*)(p);
return (platform_endianness == little_endian) ? bswap_uint32(v) : v;
}
static __inline void setbe32(const void* p, const uint32_t v)
{
if (platform_endianness == little_endian)
*((uint32_t*)p) = bswap_uint32(v);
else
*((uint32_t*)p) = v;
}
static __inline uint32_t getv32(uint32_t v)
{
return (platform_endianness == data_endianness) ? v : bswap_uint32(v);
}
static __inline uint32_t getp32(const void* p)
{
return getv32(*(const uint32_t*)(const uint8_t*)(p));
}
static __inline void fix_endian32(void* buffer, size_t nb_elts)
{
if (platform_endianness != data_endianness)
for (size_t i = 0; i < nb_elts; i++)
BSWAP_UINT32(((uint32_t*)buffer)[i]);
}
static __inline uint64_t getle64(const void* p)
{
uint64_t v = *(const uint64_t*)(const uint8_t*)(p);
return (platform_endianness == little_endian) ? v : bswap_uint64(v);
}
static __inline void setle64(const void* p, const uint64_t v)
{
if (platform_endianness == little_endian)
*((uint64_t*)p) = v;
else
*((uint64_t*)p) = bswap_uint64(v);
}
static __inline uint64_t getbe64(const void* p)
{
uint64_t v = *(const uint64_t*)(const uint8_t*)(p);
return (platform_endianness == little_endian) ? bswap_uint64(v) : v;
}
static __inline void setbe64(const void* p, const uint64_t v)
{
if (platform_endianness == little_endian)
*((uint64_t*)p) = bswap_uint64(v);
else
*((uint64_t*)p) = v;
}
static __inline uint64_t getv64(uint64_t v)
{
return (platform_endianness == data_endianness) ? v : bswap_uint64(v);
}
static __inline uint64_t getp64(const void* p)
{
return getv64(*(const uint64_t*)(const uint8_t*)(p));
}
bool create_path(char* path);
char* change_extension(const char* path, const char* extension);
size_t get_trailing_slash(const char* path);
bool is_file(const char* path);
bool is_directory(const char* path);
uint32_t read_file_max(const char* path, uint8_t** buf, uint32_t max_size);
#define read_file(path, buf) read_file_max(path, buf, 0)
uint64_t get_file_size(const char* path);
void create_backup(const char* path);
bool write_file(const uint8_t* buf, const uint32_t size, const char* path, const bool backup);