Debian Codegraph @

mz_os_posix.c

raw symbols SCIP
1 /* mz_os_posix.c -- System functions for posix
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6
7 This program is distributed under the terms of the same license as zlib.
8 See the accompanying LICENSE file for the full text of the license.
9 */
10
11 #include "mz.h"cxx . . $ `<file>/mz.h`/
12 #include "mz_strm.h"cxx . . $ `<file>/mz_strm.h`/
13 #include "mz_os.h"cxx . . $ `<file>/mz_os.h`/
14
15 #include <stdio.h> /* rename */
16 #include <errno.h>
17 #if defined(HAVE_ICONVcxx . . $ `<built-in>:412:9`!)
18 # include <iconv.h>
19 #endif
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #ifndef _WIN32
25 # include <utime.h>
26 # include <unistd.h>
27 #endif
28 #if defined(__APPLE__)
29 # include <mach/clock.h>
30 # include <mach/mach.h>
31 #endif
32
33 #if defined(HAVE_GETRANDOM)
34 # include <sys/random.h>
35 #endif
36 #if defined(HAVE_LIBBSD)
37 # include <stdlib.h> /* arc4random_buf */
38 #endif
39
40 #ifndef MZ_PRESERVE_NATIVE_STRUCTURE
41 # define MZ_PRESERVE_NATIVE_STRUCTUREcxx . . $ `mz_os_posix.c:41:11`! 1
42 #endif
43
44 /***************************************************************************/
45
46 #if defined(HAVE_ICONVcxx . . $ `<built-in>:412:9`!)
47 char *mz_os_utf8_string_createcxx . . $ mz_os_utf8_string_create(2050848944e00d54).(const char *string, int32_tcxx . . $ int32_t# encoding) {
48 iconv_tcxx . . $ iconv_t# cd;
49 const char *from_encoding = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
50 size_t result = 0;
51 size_t string_length = 0;
52 size_t string_utf8_size = 0;
53 char *string_utf8 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
54 char *string_utf8_ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
55
56 if (!string || encoding <= 0)
57 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
58
59 if (encoding == MZ_ENCODING_UTF8cxx . . $ `mz.h:144:9`!)
60 from_encoding = "UTF-8";
61 else {
62 /// up to CP2147483647
63 char string_encoding[13];
64 snprintfcxx . . $ snprintf(ea502d73e74c754c).(string_encoding, sizeof(string_encoding), "CP%03" PRId32cxx . . $ `<external>/15933738808807975373/inttypes.h:56:10`!, encoding);
65 from_encoding = string_encoding;
66 }
67
68 cd = iconv_opencxx . . $ iconv_open(12e6862c879384a7).("UTF-8", from_encoding);
69 if (cd == (iconv_tcxx . . $ iconv_t#)-1)
70 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
71
72 string_length = strlencxx . . $ strlen(751346f8406fd082).(string);
73 string_utf8_size = string_length * 2;
74 string_utf8 = (char *)calloccxx . . $ calloc(63a7bf2941bfcd7a).((int32_tcxx . . $ int32_t#)(string_utf8_size + 1), sizeof(char));
75 string_utf8_ptr = string_utf8;
76
77 if (string_utf8) {
78 result = iconvcxx . . $ iconv(df3c92a1a902995d).(cd, (char **)&string, &string_length, (char **)&string_utf8_ptr, &string_utf8_size);
79 }
80
81 iconv_closecxx . . $ iconv_close(dedf1e9b1c614a07).(cd);
82
83 if (result == (size_t)-1) {
84 freecxx . . $ free(5d22bdacc48458e8).(string_utf8);
85 string_utf8 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
86 }
87
88 return string_utf8;
89 }
90 #else
91 char *mz_os_utf8_string_create(const char *string, int32_t encoding) {
92 return strdup(string);
93 }
94 #endif
95
96 void mz_os_utf8_string_deletecxx . . $ mz_os_utf8_string_delete(3ed91490e421178f).(char **string) {
97 if (string) {
98 freecxx . . $ free(5d22bdacc48458e8).(*string);
99 *string = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
100 }
101 }
102
103 /***************************************************************************/
104
105 #if defined(HAVE_GETRANDOM)
106 int32_t mz_os_rand(uint8_t *buf, int32_t size) {
107 int32_t left = size;
108 int32_t written = 0;
109
110 while (left > 0) {
111 written = getrandom(buf, left, 0);
112 if (written < 0)
113 return MZ_INTERNAL_ERROR;
114
115 buf += written;
116 left -= written;
117 }
118 return size - left;
119 }
120 #elif defined(HAVE_ARC4RANDOM_BUF)
121 int32_t mz_os_rand(uint8_t *buf, int32_t size) {
122 if (size < 0)
123 return 0;
124 arc4random_buf(buf, (uint32_t)size);
125 return size;
126 }
127 #elif defined(HAVE_ARC4RANDOM)
128 int32_t mz_os_rand(uint8_t *buf, int32_t size) {
129 int32_t left = size;
130 for (; left > 2; left -= 3, buf += 3) {
131 uint32_t val = arc4random();
132
133 buf[0] = (val) & 0xFF;
134 buf[1] = (val >> 8) & 0xFF;
135 buf[2] = (val >> 16) & 0xFF;
136 }
137 for (; left > 0; left--, buf++) {
138 *buf = arc4random() & 0xFF;
139 }
140 return size - left;
141 }
142 #else
143 int32_tcxx . . $ int32_t# mz_os_randcxx . . $ mz_os_rand(7405b416d0535413).(uint8_tcxx . . $ uint8_t# *buf, int32_tcxx . . $ int32_t# size) {
144 static unsigned calls = 0;
145 int32_tcxx . . $ int32_t# i = 0;
146
147 /* Ensure different random header each time */
148 if (++calls == 1) {
149 # define PI_SEEDcxx . . $ `mz_os_posix.c:149:11`! 3141592654UL
150 srandcxx . . $ srand(cb2f890c2fdad230).((unsigned)(timecxx . . $ time(7f38134db6521d23).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) ^ PI_SEEDcxx . . $ `mz_os_posix.c:149:11`!));
151 }
152
153 while (i < size)
154 buf[i++] = (randcxx . . $ rand(b126dc7c1de90089).() >> 7) & 0xff;
155
156 return size;
157 }
158 #endif
159
160 int32_tcxx . . $ int32_t# mz_os_renamecxx . . $ mz_os_rename(5ae29d726ae57f88).(const char *source_path, const char *target_path) {
161 if (renamecxx . . $ rename(5ae29d726ae57f88).(source_path, target_path) == -1)
162 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
163
164 return MZ_OKcxx . . $ `mz.h:21:9`!;
165 }
166
167 int32_tcxx . . $ int32_t# mz_os_unlinkcxx . . $ mz_os_unlink(5c4306d8553b3696).(const char *path) {
168 if (unlinkcxx . . $ unlink(5c4306d8553b3696).(path) == -1)
169 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
170
171 return MZ_OKcxx . . $ `mz.h:21:9`!;
172 }
173
174 int32_tcxx . . $ int32_t# mz_os_file_existscxx . . $ mz_os_file_exists(5c4306d8553b3696).(const char *path) {
175 struct statcxx . . $ stat# path_stat;
176
177 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&path_stat, 0, sizeof(path_stat));
178 if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &path_stat) == 0)
179 return MZ_OKcxx . . $ `mz.h:21:9`!;
180 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
181 }
182
183 int64_tcxx . . $ int64_t# mz_os_get_file_sizecxx . . $ mz_os_get_file_size(3c95bd14d5d5d286).(const char *path) {
184 struct statcxx . . $ stat# path_stat;
185
186 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&path_stat, 0, sizeof(path_stat));
187 if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &path_stat) == 0) {
188 /* Stat returns size taken up by directory entry, so return 0 */
189 if (S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(path_stat.st_mode))
190 return 0;
191
192 return path_stat.st_sizecxx . . $ stat#st_size.;
193 }
194
195 return 0;
196 }
197
198 int32_tcxx . . $ int32_t# mz_os_get_file_datecxx . . $ mz_os_get_file_date(d6a2abb5a2f53069).(const char *path, time_tcxx . . $ time_t# *modified_date, time_tcxx . . $ time_t# *accessed_date, time_tcxx . . $ time_t# *creation_date) {
199 struct statcxx . . $ stat# path_stat;
200 char *name = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
201 int32_tcxx . . $ int32_t# err = MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
202
203 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&path_stat, 0, sizeof(path_stat));
204
205 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(path, "-") != 0) {
206 /* Not all systems allow stat'ing a file with / appended */
207 name = strdupcxx . . $ strdup(a8b8e32347182212).(path);
208 mz_path_remove_slashcxx . . $ mz_path_remove_slash(1ef8e6ec083aabb9).(name);
209
210 if (statcxx . . $ stat(a9cd1e23e96f423c).(name, &path_stat) == 0) {
211 if (modified_date)
212 *modified_date = path_stat.st_mtimecxx . . $ `<external>/16045971866008360577/struct_stat.h:78:11`!;
213 if (accessed_date)
214 *accessed_date = path_stat.st_atimecxx . . $ `<external>/16045971866008360577/struct_stat.h:77:11`!;
215 /* Creation date not supported */
216 if (creation_date)
217 *creation_date = 0;
218
219 err = MZ_OKcxx . . $ `mz.h:21:9`!;
220 }
221
222 freecxx . . $ free(5d22bdacc48458e8).(name);
223 }
224
225 return err;
226 }
227
228 int32_tcxx . . $ int32_t# mz_os_set_file_datecxx . . $ mz_os_set_file_date(3744568063355712).(const char *path, time_tcxx . . $ time_t# modified_date, time_tcxx . . $ time_t# accessed_date, time_tcxx . . $ time_t# creation_date) {
229 struct utimbufcxx . . $ utimbuf# ut;
230
231 ut.actimecxx . . $ utimbuf#actime. = accessed_date;
232 ut.modtimecxx . . $ utimbuf#modtime. = modified_date;
233
234 /* Creation date not supported */
235 MZ_UNUSEDcxx . . $ `mz.h:147:9`!(creation_date);
236
237 if (utimecxx . . $ utime(cc883b4c11abccda).(path, &ut) != 0)
238 return MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
239
240 return MZ_OKcxx . . $ `mz.h:21:9`!;
241 }
242
243 int32_tcxx . . $ int32_t# mz_os_get_file_attribscxx . . $ mz_os_get_file_attribs(6974107818eeb505).(const char *path, uint32_tcxx . . $ uint32_t# *attributes) {
244 struct statcxx . . $ stat# path_stat;
245 int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!;
246
247 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&path_stat, 0, sizeof(path_stat));
248 if (lstatcxx . . $ lstat(a9cd1e23e96f423c).(path, &path_stat) == -1)
249 err = MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
250 *attributes = path_stat.st_modecxx . . $ stat#st_mode.;
251 return err;
252 }
253
254 int32_tcxx . . $ int32_t# mz_os_set_file_attribscxx . . $ mz_os_set_file_attribs(1df18506ef0a631b).(const char *path, uint32_tcxx . . $ uint32_t# attributes) {
255 int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!;
256
257 if (chmodcxx . . $ chmod(1df18506ef0a631b).(path, (mode_tcxx . . $ mode_t#)attributes) == -1)
258 err = MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
259
260 return err;
261 }
262
263 int32_tcxx . . $ int32_t# mz_os_make_dircxx . . $ mz_os_make_dir(5c4306d8553b3696).(const char *path) {
264 int32_tcxx . . $ int32_t# err = 0;
265
266 err = mkdircxx . . $ mkdir(1df18506ef0a631b).(path, 0755);
267
268 if (err != 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!)
269 return MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
270
271 return MZ_OKcxx . . $ `mz.h:21:9`!;
272 }
273
274 DIRcxx . . $ DIR# *mz_os_open_dircxx . . $ mz_os_open_dir(f465d4b65cec8dd2).(const char *path) {
275 return opendircxx . . $ opendir(f465d4b65cec8dd2).(path);
276 }
277
278 struct direntcxx . . $ dirent# *mz_os_read_dircxx . . $ mz_os_read_dir(ffbaae344e1a615a).(DIRcxx . . $ DIR# *dir) {
279 if (!dir)
280 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
281 return readdircxx . . $ readdir(ffbaae344e1a615a).(dir);
282 }
283
284 int32_tcxx . . $ int32_t# mz_os_close_dircxx . . $ mz_os_close_dir(ac30a447bc7c2f80).(DIRcxx . . $ DIR# *dir) {
285 if (!dir)
286 return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!;
287 if (closedircxx . . $ closedir(ac30a447bc7c2f80).(dir) == -1)
288 return MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
289 return MZ_OKcxx . . $ `mz.h:21:9`!;
290 }
291
292 int32_tcxx . . $ int32_t# mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(const char c) {
293 #if MZ_PRESERVE_NATIVE_STRUCTUREcxx . . $ `mz_os_posix.c:41:11`!
294 // While not strictly adhering to 4.4.17.1,
295 // this preserves UNIX filesystem structure.
296 return c == '/';
297 #else
298 // While strictly adhering to 4.4.17.1,
299 // this corrupts UNIX filesystem structure (a filename with a '\\' will become a folder + a file).
300 return c == '\\' || c == '/';
301 #endif
302 }
303
304 int32_tcxx . . $ int32_t# mz_os_is_dircxx . . $ mz_os_is_dir(5c4306d8553b3696).(const char *path) {
305 struct statcxx . . $ stat# path_stat;
306
307 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&path_stat, 0, sizeof(path_stat));
308 statcxx . . $ stat(a9cd1e23e96f423c).(path, &path_stat);
309 if (S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(path_stat.st_mode))
310 return MZ_OKcxx . . $ `mz.h:21:9`!;
311
312 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
313 }
314
315 int32_tcxx . . $ int32_t# mz_os_is_symlinkcxx . . $ mz_os_is_symlink(5c4306d8553b3696).(const char *path) {
316 struct statcxx . . $ stat# path_stat;
317
318 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&path_stat, 0, sizeof(path_stat));
319 lstatcxx . . $ lstat(a9cd1e23e96f423c).(path, &path_stat);
320 if (S_ISLNKcxx . . $ `<external>/531885356569766532/stat.h:133:10`!(path_stat.st_mode))
321 return MZ_OKcxx . . $ `mz.h:21:9`!;
322
323 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
324 }
325
326 int32_tcxx . . $ int32_t# mz_os_make_symlinkcxx . . $ mz_os_make_symlink(5ae29d726ae57f88).(const char *path, const char *target_path) {
327 if (symlinkcxx . . $ symlink(5ae29d726ae57f88).(target_path, path) != 0)
328 return MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
329 return MZ_OKcxx . . $ `mz.h:21:9`!;
330 }
331
332 int32_tcxx . . $ int32_t# mz_os_read_symlinkcxx . . $ mz_os_read_symlink(89c8e27db0bdb94e).(const char *path, char *target_path, int32_tcxx . . $ int32_t# max_target_path) {
333 size_t length = 0;
334
335 length = (size_t)readlinkcxx . . $ readlink(373c9bd46766ee5).(path, target_path, max_target_path - 1);
336 if (length == (size_t)-1)
337 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
338
339 target_path[length] = 0;
340 return MZ_OKcxx . . $ `mz.h:21:9`!;
341 }
342
343 uint64_tcxx . . $ uint64_t# mz_os_ms_timecxx . . $ mz_os_ms_time(f708dce9a8856c70).(void) {
344 struct timespeccxx . . $ timespec# ts;
345
346 #if defined(__APPLE__)
347 clock_serv_t cclock;
348 mach_timespec_t mts;
349
350 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
351 clock_get_time(cclock, &mts);
352 mach_port_deallocate(mach_task_self(), cclock);
353
354 ts.tv_sec = mts.tv_sec;
355 ts.tv_nsec = mts.tv_nsec;
356 #elif !defined(_POSIX_MONOTONIC_CLOCKcxx . . $ `<external>/4533468968537266721/posix_opt.h:164:9`!) || _POSIX_MONOTONIC_CLOCKcxx . . $ `<external>/4533468968537266721/posix_opt.h:164:9`! < 0
357 clock_gettime(CLOCK_REALTIME, &ts);
358 #elif _POSIX_MONOTONIC_CLOCKcxx . . $ `<external>/4533468968537266721/posix_opt.h:164:9`! > 0
359 clock_gettime(CLOCK_MONOTONIC, &ts);
360 #else
361 if (sysconfcxx . . $ sysconf(dc1f09c09fbff20f).(_SC_MONOTONIC_CLOCKcxx . . $ `<external>/6393649343499073842/confname.h:391:9`!) > 0)
362 clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_MONOTONICcxx . . $ `<external>/89459239771508533/time.h:48:10`!, &ts);
363 else
364 clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_REALTIMEcxx . . $ `<external>/89459239771508533/time.h:46:10`!, &ts);
365 #endif
366
367 return ((uint64_tcxx . . $ uint64_t#)ts.tv_seccxx . . $ timespec#tv_sec. * 1000) + ((uint64_tcxx . . $ uint64_t#)ts.tv_nseccxx . . $ timespec#tv_nsec. / 1000000);
368 }