forked from mmozeiko/pkg2zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg2zip_sys.c
362 lines (302 loc) · 7.48 KB
/
pkg2zip_sys.c
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
#include "pkg2zip_sys.h"
#include "pkg2zip_utils.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <errno.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static HANDLE gStdout;
static int gStdoutRedirected;
static UINT gOldCP;
void sys_output_init(void)
{
gOldCP = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
gStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode;
gStdoutRedirected = !GetConsoleMode(gStdout, &mode);
}
void sys_output_done(void)
{
SetConsoleOutputCP(gOldCP);
}
void sys_output(const char* msg, ...)
{
char buffer[1024];
va_list arg;
va_start(arg, msg);
vsnprintf(buffer, sizeof(buffer), msg, arg);
va_end(arg);
if (!gStdoutRedirected)
{
WCHAR wbuffer[sizeof(buffer)];
int wcount = MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, sizeof(buffer));
DWORD written;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wbuffer, wcount - 1, &written, NULL);
return;
}
fputs(buffer, stdout);
}
void sys_error(const char* msg, ...)
{
char buffer[1024];
va_list arg;
va_start(arg, msg);
vsnprintf(buffer, sizeof(buffer), msg, arg);
va_end(arg);
DWORD mode;
if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), &mode))
{
WCHAR wbuffer[sizeof(buffer)];
int wcount = MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, sizeof(buffer));
DWORD written;
WriteConsoleW(GetStdHandle(STD_ERROR_HANDLE), wbuffer, wcount - 1, &written, NULL);
}
else
{
fputs(buffer, stderr);
}
SetConsoleOutputCP(gOldCP);
exit(EXIT_FAILURE);
}
static void sys_mkdir_real(const char* path)
{
WCHAR wpath[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH);
if (CreateDirectoryW(wpath, NULL) == 0)
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
sys_error("ERROR: cannot create '%s' folder\n", path);
}
}
}
sys_file sys_open(const char* fname, uint64_t* size)
{
WCHAR path[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, fname, -1, path, MAX_PATH);
HANDLE handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
sys_error("ERROR: cannot open '%s' file\n", fname);
}
LARGE_INTEGER sz;
if (!GetFileSizeEx(handle, &sz))
{
sys_error("ERROR: cannot get size of '%s' file\n", fname);
}
*size = sz.QuadPart;
return handle;
}
sys_file sys_create(const char* fname)
{
WCHAR path[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, fname, -1, path, MAX_PATH);
HANDLE handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
sys_error("ERROR:1: cannot create '%s' file\n", fname);
}
return handle;
}
void sys_close(sys_file file)
{
if (!CloseHandle(file))
{
sys_error("ERROR: failed to close file\n");
}
}
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size)
{
DWORD read;
OVERLAPPED ov;
ov.hEvent = NULL;
ov.Offset = (uint32_t)offset;
ov.OffsetHigh = (uint32_t)(offset >> 32);
if (!ReadFile(file, buffer, size, &read, &ov) || read != size)
{
sys_error("ERROR: failed to read %u bytes from file\n", size);
}
}
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size)
{
DWORD written;
OVERLAPPED ov;
ov.hEvent = NULL;
ov.Offset = (uint32_t)offset;
ov.OffsetHigh = (uint32_t)(offset >> 32);
if (!WriteFile(file, buffer, size, &written, &ov) || written != size)
{
sys_error("ERROR: failed to write %u bytes to file\n", size);
}
}
#else
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
static int gStdoutRedirected;
void sys_output_init(void)
{
gStdoutRedirected = !isatty(STDOUT_FILENO);
}
void sys_output_done(void)
{
}
void sys_output(const char* msg, ...)
{
va_list arg;
va_start(arg, msg);
vfprintf(stdout, msg, arg);
va_end(arg);
}
void sys_error(const char* msg, ...)
{
va_list arg;
va_start(arg, msg);
vfprintf(stderr, msg, arg);
va_end(arg);
exit(EXIT_FAILURE);
}
static void sys_mkdir_real(const char* path)
{
if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0)
{
if (errno != EEXIST)
{
sys_error("ERROR: cannot create '%s' folder\n", path);
}
}
}
sys_file sys_open(const char* fname, uint64_t* size)
{
int fd = open(fname, O_RDONLY);
if (fd < 0)
{
sys_error("ERROR: cannot open '%s' file\n", fname);
}
struct stat st;
if (fstat(fd, &st) != 0)
{
sys_error("ERROR: cannot get size of '%s' file\n", fname);
}
*size = st.st_size;
return (void*)(intptr_t)fd;
}
sys_file sys_create(const char* fname)
{
int fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
{
sys_error("ERROR:2: cannot create '%s' file\n", fname);
}
return (void*)(intptr_t)fd;
}
void sys_close(sys_file file)
{
if (close((int)(intptr_t)file) != 0)
{
sys_error("ERROR: failed to close file\n");
}
}
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size)
{
ssize_t read = pread((int)(intptr_t)file, buffer, size, offset);
if (read < 0 || read != (ssize_t)size)
{
sys_error("ERROR: failed to read %u bytes from file\n", size);
}
}
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size)
{
ssize_t wrote = pwrite((int)(intptr_t)file, buffer, size, offset);
if (wrote < 0 || wrote != (ssize_t)size)
{
sys_error("ERROR: failed to read %u bytes from file\n", size);
}
}
#endif
void sys_mkdir(const char* path)
{
char* last = strrchr(path, '/');
if (last)
{
*last = 0;
sys_mkdir(path);
*last = '/';
}
sys_mkdir_real(path);
}
void* sys_realloc(void* ptr, size_t size)
{
void* result = NULL;
if (!ptr && size)
{
result = malloc(size);
}
else if (ptr && !size)
{
free(ptr);
return NULL;
}
else if (ptr && size)
{
result = realloc(ptr, size);
}
else
{
sys_error("ERROR: internal error, wrong sys_realloc usage\n");
}
if (!result)
{
sys_error("ERROR: out of memory\n");
}
return result;
}
void sys_vstrncat(char* dst, size_t n, const char* format, ...)
{
char temp[1024];
va_list args;
va_start(args, format);
vsnprintf(temp, sizeof(temp), format, args);
va_end(args);
strncat(dst, temp, n - strlen(dst) - 1);
}
static uint64_t out_size;
static uint32_t out_next;
void sys_output_progress_init(uint64_t size)
{
out_size = size;
out_next = 0;
}
void sys_output_progress(uint64_t progress)
{
if (gStdoutRedirected)
{
return;
}
uint32_t now = (uint32_t)(progress * 100 / out_size);
if (now >= out_next)
{
sys_output("[*] unpacking... %u%%\r", now);
out_next = now + 1;
}
}
int sys_test_dir(const char* const path)
{
struct stat info;
int statRC = stat( path, &info );
if( statRC != 0 )
{
if (errno == ENOENT) { return 0; }
if (errno == ENOTDIR) { return 0; }
return -1;
}
return ( info.st_mode & S_IFDIR ) ? 1 : 0;
}