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"
14 #include "mz_crypt.h"
15 #include "mz_os.h"
16 #include "mz_strm.h"
17 #include "mz_strm_os.h"
18
19 #include <ctype.h> /* tolower */
20 #include <string.h>
21
22 /***************************************************************************/
23
24 int32_t mz_path_combine(char *path, const char *join, int32_t max_path) {
25 int32_t path_len = 0;
26
27 if (!path || !join || !max_path)
28 return MZ_PARAM_ERROR;
29
30 path_len = (int32_t)strlen(path);
31
32 if (path_len == 0) {
33 strncpy(path, join, max_path - 1);
34 path[max_path - 1] = 0;
35 } else {
36 mz_path_append_slash(path, max_path, MZ_PATH_SLASH_PLATFORM);
37 path_len = (int32_t)strlen(path);
38 if (max_path > path_len)
39 strncat(path, join, max_path - path_len - 1);
40 }
41
42 return MZ_OK;
43 }
44
45 int32_t mz_path_append_slash(char *path, int32_t max_path, char slash) {
46 int32_t path_len = (int32_t)strlen(path);
47 if ((path_len + 2) >= max_path)
48 return MZ_BUF_ERROR;
49 if (!mz_os_is_dir_separator(path[path_len - 1])) {
50 path[path_len] = slash;
51 path[path_len + 1] = 0;
52 }
53 return MZ_OK;
54 }
55
56 int32_t mz_path_remove_slash(char *path) {
57 int32_t path_len = (int32_t)strlen(path);
58 while (path_len > 0) {
59 if (mz_os_is_dir_separator(path[path_len - 1]))
60 path[path_len - 1] = 0;
61 else
62 break;
63
64 path_len -= 1;
65 }
66 return MZ_OK;
67 }
68
69 int32_t mz_path_has_slash(const char *path) {
70 int32_t path_len = (int32_t)strlen(path);
71 if (path_len > 0 && !mz_os_is_dir_separator(path[path_len - 1]))
72 return MZ_EXIST_ERROR;
73 return MZ_OK;
74 }
75
76 int32_t mz_path_convert_slashes(char *path, char slash) {
77 int32_t i = 0;
78
79 for (i = 0; i < (int32_t)strlen(path); i += 1) {
80 if (mz_os_is_dir_separator(path[i]))
81 path[i] = slash;
82 }
83 return MZ_OK;
84 }
85
86 int32_t mz_path_compare_wc(const char *path, const char *wildcard, uint8_t ignore_case) {
87 while (*path != 0) {
88 switch (*wildcard) {
89 case '*':
90
91 if (*(wildcard + 1) == 0)
92 return MZ_OK;
93
94 while (*path != 0) {
95 if (mz_path_compare_wc(path, (wildcard + 1), ignore_case) == MZ_OK)
96 return MZ_OK;
97
98 path += 1;
99 }
100
101 return MZ_EXIST_ERROR;
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 (tolower(*path) != tolower(*wildcard))
110 return MZ_EXIST_ERROR;
111 } else {
112 if (*path != *wildcard)
113 return MZ_EXIST_ERROR;
114 }
115
116 break;
117 }
118
119 path += 1;
120 wildcard += 1;
121 }
122
123 if ((*wildcard != 0) && (*wildcard != '*'))
124 return MZ_EXIST_ERROR;
125
126 return MZ_OK;
127 }
128
129 int32_t mz_path_resolve(const char *path, char *output, 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_ERROR;
136
137 while (*source != 0 && max_output > 1) {
138 check = source;
139 if (mz_os_is_dir_separator(*check))
140 check += 1;
141
142 if (source == path || target == output || check != source) {
143 /* Skip double paths */
144 if (mz_os_is_dir_separator(*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_separator(*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_separator(*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_separator(*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_separator(*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_ERROR;
209
210 return MZ_OK;
211 }
212
213 int32_t mz_path_remove_filename(char *path) {
214 char *path_ptr = NULL;
215
216 if (!path)
217 return MZ_PARAM_ERROR;
218
219 path_ptr = path + strlen(path) - 1;
220
221 while (path_ptr > path) {
222 if (mz_os_is_dir_separator(*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_OK;
234 }
235
236 int32_t mz_path_remove_extension(char *path) {
237 char *path_ptr = NULL;
238
239 if (!path)
240 return MZ_PARAM_ERROR;
241
242 path_ptr = path + strlen(path) - 1;
243
244 while (path_ptr > path) {
245 if (mz_os_is_dir_separator(*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_OK;
259 }
260
261 int32_t mz_path_get_filename(const char *path, const char **filename) {
262 const char *match = NULL;
263
264 if (!path || !filename)
265 return MZ_PARAM_ERROR;
266
267 *filename = NULL;
268
269 for (match = path; *match != 0; match += 1) {
270 if (mz_os_is_dir_separator(*match))
271 *filename = match + 1;
272 }
273
274 if (!*filename)
275 return MZ_EXIST_ERROR;
276
277 return MZ_OK;
278 }
279
280 int32_t mz_dir_make(const char *path) {
281 int32_t err = MZ_OK;
282 char *current_dir = NULL;
283 char *match = NULL;
284 char hold = 0;
285
286 if (!*path)
287 return MZ_OK;
288
289 current_dir = strdup(path);
290 if (!current_dir)
291 return MZ_MEM_ERROR;
292
293 mz_path_remove_slash(current_dir);
294
295 err = mz_os_make_dir(current_dir);
296 if (err != MZ_OK) {
297 match = current_dir + 1;
298 while (1) {
299 while (*match != 0 && !mz_os_is_dir_separator(*match))
300 match += 1;
301 hold = *match;
302 *match = 0;
303
304 err = mz_os_make_dir(current_dir);
305 if (err != MZ_OK)
306 break;
307 if (hold == 0)
308 break;
309
310 *match = hold;
311 match += 1;
312 }
313 }
314
315 free(current_dir);
316 return err;
317 }
318
319 int32_t mz_file_get_crc(const char *path, uint32_t *result_crc) {
320 void *stream = NULL;
321 uint32_t crc32 = 0;
322 int32_t read = 0;
323 int32_t err = MZ_OK;
324 uint8_t buf[16384];
325
326 stream = mz_stream_os_create();
327 if (!stream)
328 return MZ_MEM_ERROR;
329
330 err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
331 if (err == MZ_OK) {
332 do {
333 read = mz_stream_os_read(stream, buf, sizeof(buf));
334
335 if (read < 0) {
336 err = read;
337 break;
338 }
339
340 crc32 = mz_crypt_crc32_update(crc32, buf, read);
341 } while ((err == MZ_OK) && (read > 0));
342
343 mz_stream_os_close(stream);
344 }
345
346 *result_crc = crc32;
347
348 mz_stream_os_delete(&stream);
349
350 return err;
351 }
352
353 /***************************************************************************/