Debian Codegraph @

mz_os.c

raw symbols SCIP
1 /* mz_os.c -- System functions
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6 Copyright (C) 1998-2010 Gilles Vollant
7 https://www.winimage.com/zLibDll/minizip.html
8
9 This program is distributed under the terms of the same license as zlib.
10 See the accompanying LICENSE file for the full text of the license.
11 */
12
13 #include "mz.h"cxx . . $ `<file>/mz.h`/
14 #include "mz_crypt.h"cxx . . $ `<file>/mz_crypt.h`/
15 #include "mz_os.h"cxx . . $ `<file>/mz_os.h`/
16 #include "mz_strm.h"cxx . . $ `<file>/mz_strm.h`/
17 #include "mz_strm_os.h"cxx . . $ `<file>/mz_strm_os.h`/
18
19 #include <ctype.h> /* tolower */
20 #include <string.h>
21
22 /***************************************************************************/
23
24 int32_tcxx . . $ int32_t# mz_path_combinecxx . . $ mz_path_combine(c42ad7cb79e2bab7).(char *path, const char *join, int32_tcxx . . $ int32_t# max_path) {
25 int32_tcxx . . $ int32_t# path_len = 0;
26
27 if (!path || !join || !max_path)
28 return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!;
29
30 path_len = (int32_tcxx . . $ int32_t#)strlencxx . . $ strlen(751346f8406fd082).(path);
31
32 if (path_len == 0) {
33 strncpycxx . . $ strncpy(255158a415b673b0).(path, join, max_path - 1);
34 path[max_path - 1] = 0;
35 } else {
36 mz_path_append_slashcxx . . $ mz_path_append_slash(7799835858448083).(path, max_path, MZ_PATH_SLASH_PLATFORMcxx . . $ `mz_os.h:47:11`!);
37 path_len = (int32_tcxx . . $ int32_t#)strlencxx . . $ strlen(751346f8406fd082).(path);
38 if (max_path > path_len)
39 strncatcxx . . $ strncat(255158a415b673b0).(path, join, max_path - path_len - 1);
40 }
41
42 return MZ_OKcxx . . $ `mz.h:21:9`!;
43 }
44
45 int32_tcxx . . $ int32_t# mz_path_append_slashcxx . . $ mz_path_append_slash(7799835858448083).(char *path, int32_tcxx . . $ int32_t# max_path, char slash) {
46 int32_tcxx . . $ int32_t# path_len = (int32_tcxx . . $ int32_t#)strlencxx . . $ strlen(751346f8406fd082).(path);
47 if ((path_len + 2) >= max_path)
48 return MZ_BUF_ERRORcxx . . $ `mz.h:25:9`!;
49 if (!mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(path[path_len - 1])) {
50 path[path_len] = slash;
51 path[path_len + 1] = 0;
52 }
53 return MZ_OKcxx . . $ `mz.h:21:9`!;
54 }
55
56 int32_tcxx . . $ int32_t# mz_path_remove_slashcxx . . $ mz_path_remove_slash(1ef8e6ec083aabb9).(char *path) {
57 int32_tcxx . . $ int32_t# path_len = (int32_tcxx . . $ int32_t#)strlencxx . . $ strlen(751346f8406fd082).(path);
58 while (path_len > 0) {
59 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(path[path_len - 1]))
60 path[path_len - 1] = 0;
61 else
62 break;
63
64 path_len -= 1;
65 }
66 return MZ_OKcxx . . $ `mz.h:21:9`!;
67 }
68
69 int32_tcxx . . $ int32_t# mz_path_has_slashcxx . . $ mz_path_has_slash(5c4306d8553b3696).(const char *path) {
70 int32_tcxx . . $ int32_t# path_len = (int32_tcxx . . $ int32_t#)strlencxx . . $ strlen(751346f8406fd082).(path);
71 if (path_len > 0 && !mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(path[path_len - 1]))
72 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
73 return MZ_OKcxx . . $ `mz.h:21:9`!;
74 }
75
76 int32_tcxx . . $ int32_t# mz_path_convert_slashescxx . . $ mz_path_convert_slashes(aaa73ea12dc6a7b7).(char *path, char slash) {
77 int32_tcxx . . $ int32_t# i = 0;
78
79 for (i = 0; i < (int32_tcxx . . $ int32_t#)strlencxx . . $ strlen(751346f8406fd082).(path); i += 1) {
80 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(path[i]))
81 path[i] = slash;
82 }
83 return MZ_OKcxx . . $ `mz.h:21:9`!;
84 }
85
86 int32_tcxx . . $ int32_t# mz_path_compare_wccxx . . $ mz_path_compare_wc(6dbe66b8d729cbab).(const char *path, const char *wildcard, uint8_tcxx . . $ uint8_t# ignore_case) {
87 while (*path != 0) {
88 switch (*wildcard) {
89 case '*':
90
91 if (*(wildcard + 1) == 0)
92 return MZ_OKcxx . . $ `mz.h:21:9`!;
93
94 while (*path != 0) {
95 if (mz_path_compare_wccxx . . $ mz_path_compare_wc(6dbe66b8d729cbab).(path, (wildcard + 1), ignore_case) == MZ_OKcxx . . $ `mz.h:21:9`!)
96 return MZ_OKcxx . . $ `mz.h:21:9`!;
97
98 path += 1;
99 }
100
101 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
102
103 default:
104 /* Ignore differences in path slashes on platforms */
105 if ((*path == '\\' && *wildcard == '/') || (*path == '/' && *wildcard == '\\'))
106 break;
107
108 if (ignore_case) {
109 if (tolowercxx . . $ tolower(7864480464b09eea).(*path) != tolowercxx . . $ tolower(7864480464b09eea).(*wildcard))
110 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
111 } else {
112 if (*path != *wildcard)
113 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
114 }
115
116 break;
117 }
118
119 path += 1;
120 wildcard += 1;
121 }
122
123 if ((*wildcard != 0) && (*wildcard != '*'))
124 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
125
126 return MZ_OKcxx . . $ `mz.h:21:9`!;
127 }
128
129 int32_tcxx . . $ int32_t# mz_path_resolvecxx . . $ mz_path_resolve(89c8e27db0bdb94e).(const char *path, char *output, int32_tcxx . . $ int32_t# max_output) {
130 const char *source = path;
131 const char *check = output;
132 char *target = output;
133
134 if (max_output <= 0)
135 return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!;
136
137 while (*source != 0 && max_output > 1) {
138 check = source;
139 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*check))
140 check += 1;
141
142 if (source == path || target == output || check != source) {
143 /* Skip double paths */
144 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*check)) {
145 source += 1;
146 continue;
147 }
148 if (*check == '.') {
149 check += 1;
150
151 /* Remove . if at end of string and not at the beginning */
152 if (*check == 0 && source != path && target != output) {
153 /* Copy last slash */
154 *target = *source;
155 target += 1;
156 max_output -= 1;
157 source += (check - source);
158 continue;
159 }
160 /* Remove . if not at end of string */
161 else if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*check)) {
162 source += (check - source);
163 /* Skip slash if at beginning of string */
164 if (target == output && *source != 0)
165 source += 1;
166 continue;
167 }
168 /* Go to parent directory .. */
169 else if (*check == '.') {
170 check += 1;
171 if (*check == 0 || mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*check)) {
172 source += (check - source);
173
174 /* Search backwards for previous slash or the start of the output string */
175 if (target != output) {
176 target -= 1;
177 do {
178 if (target == output || mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*target))
179 break;
180
181 target -= 1;
182 max_output += 1;
183 } while (target > output);
184 }
185
186 if ((target == output) && *source != 0)
187 source += 1;
188 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*target) && *source == 0)
189 target += 1;
190
191 *target = 0;
192 continue;
193 }
194 }
195 }
196 }
197
198 *target = *source;
199
200 source += 1;
201 target += 1;
202 max_output -= 1;
203 }
204
205 *target = 0;
206
207 if (*path == 0)
208 return MZ_INTERNAL_ERRORcxx . . $ `mz.h:33:9`!;
209
210 return MZ_OKcxx . . $ `mz.h:21:9`!;
211 }
212
213 int32_tcxx . . $ int32_t# mz_path_remove_filenamecxx . . $ mz_path_remove_filename(1ef8e6ec083aabb9).(char *path) {
214 char *path_ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
215
216 if (!path)
217 return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!;
218
219 path_ptr = path + strlencxx . . $ strlen(751346f8406fd082).(path) - 1;
220
221 while (path_ptr > path) {
222 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*path_ptr)) {
223 *path_ptr = 0;
224 break;
225 }
226
227 path_ptr -= 1;
228 }
229
230 if (path_ptr == path)
231 *path_ptr = 0;
232
233 return MZ_OKcxx . . $ `mz.h:21:9`!;
234 }
235
236 int32_tcxx . . $ int32_t# mz_path_remove_extensioncxx . . $ mz_path_remove_extension(1ef8e6ec083aabb9).(char *path) {
237 char *path_ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
238
239 if (!path)
240 return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!;
241
242 path_ptr = path + strlencxx . . $ strlen(751346f8406fd082).(path) - 1;
243
244 while (path_ptr > path) {
245 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*path_ptr))
246 break;
247 if (*path_ptr == '.') {
248 *path_ptr = 0;
249 break;
250 }
251
252 path_ptr -= 1;
253 }
254
255 if (path_ptr == path)
256 *path_ptr = 0;
257
258 return MZ_OKcxx . . $ `mz.h:21:9`!;
259 }
260
261 int32_tcxx . . $ int32_t# mz_path_get_filenamecxx . . $ mz_path_get_filename(2ef3bc9ee8f8b3e9).(const char *path, const char **filename) {
262 const char *match = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
263
264 if (!path || !filename)
265 return MZ_PARAM_ERRORcxx . . $ `mz.h:31:9`!;
266
267 *filename = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
268
269 for (match = path; *match != 0; match += 1) {
270 if (mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*match))
271 *filename = match + 1;
272 }
273
274 if (!*filename)
275 return MZ_EXIST_ERRORcxx . . $ `mz.h:36:9`!;
276
277 return MZ_OKcxx . . $ `mz.h:21:9`!;
278 }
279
280 int32_tcxx . . $ int32_t# mz_dir_makecxx . . $ mz_dir_make(5c4306d8553b3696).(const char *path) {
281 int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!;
282 char *current_dir = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
283 char *match = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
284 char hold = 0;
285
286 if (!*path)
287 return MZ_OKcxx . . $ `mz.h:21:9`!;
288
289 current_dir = strdupcxx . . $ strdup(a8b8e32347182212).(path);
290 if (!current_dir)
291 return MZ_MEM_ERRORcxx . . $ `mz.h:24:9`!;
292
293 mz_path_remove_slashcxx . . $ mz_path_remove_slash(1ef8e6ec083aabb9).(current_dir);
294
295 err = mz_os_make_dircxx . . $ mz_os_make_dir(5c4306d8553b3696).(current_dir);
296 if (err != MZ_OKcxx . . $ `mz.h:21:9`!) {
297 match = current_dir + 1;
298 while (1) {
299 while (*match != 0 && !mz_os_is_dir_separatorcxx . . $ mz_os_is_dir_separator(784f3405e7e545be).(*match))
300 match += 1;
301 hold = *match;
302 *match = 0;
303
304 err = mz_os_make_dircxx . . $ mz_os_make_dir(5c4306d8553b3696).(current_dir);
305 if (err != MZ_OKcxx . . $ `mz.h:21:9`!)
306 break;
307 if (hold == 0)
308 break;
309
310 *match = hold;
311 match += 1;
312 }
313 }
314
315 freecxx . . $ free(5d22bdacc48458e8).(current_dir);
316 return err;
317 }
318
319 int32_tcxx . . $ int32_t# mz_file_get_crccxx . . $ mz_file_get_crc(6974107818eeb505).(const char *path, uint32_tcxx . . $ uint32_t# *result_crc) {
320 void *stream = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
321 uint32_tcxx . . $ uint32_t# crc32 = 0;
322 int32_tcxx . . $ int32_t# read = 0;
323 int32_tcxx . . $ int32_t# err = MZ_OKcxx . . $ `mz.h:21:9`!;
324 uint8_tcxx . . $ uint8_t# buf[16384];
325
326 stream = mz_stream_os_createcxx . . $ mz_stream_os_create(50c7df7de5344ebb).();
327 if (!stream)
328 return MZ_MEM_ERRORcxx . . $ `mz.h:24:9`!;
329
330 err = mz_stream_os_opencxx . . $ mz_stream_os_open(7083a8984c0735e8).(stream, path, MZ_OPEN_MODE_READcxx . . $ `mz.h:50:9`!);
331 if (err == MZ_OKcxx . . $ `mz.h:21:9`!) {
332 do {
333 read = mz_stream_os_readcxx . . $ mz_stream_os_read(8bc41909eac9e5d8).(stream, buf, sizeof(buf));
334
335 if (read < 0) {
336 err = read;
337 break;
338 }
339
340 crc32 = mz_crypt_crc32_updatecxx . . $ mz_crypt_crc32_update(c72eefe6f8c9ad5).(crc32, buf, read);
341 } while ((err == MZ_OKcxx . . $ `mz.h:21:9`!) && (read > 0));
342
343 mz_stream_os_closecxx . . $ mz_stream_os_close(dedf1e9b1c614a07).(stream);
344 }
345
346 *result_crc = crc32;
347
348 mz_stream_os_deletecxx . . $ mz_stream_os_delete(539e082cf00e1f43).(&stream);
349
350 return err;
351 }
352
353 /***************************************************************************/