| 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" |
| 12 | #include "mz_strm.h" |
| 13 | #include "mz_os.h" |
| 14 | |
| 15 | #include <stdio.h> /* rename */ |
| 16 | #include <errno.h> |
| 17 | #if defined(HAVE_ICONV) |
| 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_STRUCTURE 1 |
| 42 | #endif |
| 43 | |
| 44 | /***************************************************************************/ |
| 45 | |
| 46 | #if defined(HAVE_ICONV) |
| 47 | char *mz_os_utf8_string_create(const char *string, int32_t encoding) { |
| 48 | iconv_t cd; |
| 49 | const char *from_encoding = NULL; |
| 50 | size_t result = 0; |
| 51 | size_t string_length = 0; |
| 52 | size_t string_utf8_size = 0; |
| 53 | char *string_utf8 = NULL; |
| 54 | char *string_utf8_ptr = NULL; |
| 55 | |
| 56 | if (!string || encoding <= 0) |
| 57 | return NULL; |
| 58 | |
| 59 | if (encoding == MZ_ENCODING_UTF8) |
| 60 | from_encoding = "UTF-8"; |
| 61 | else { |
| 62 | /// up to CP2147483647 |
| 63 | char string_encoding[13]; |
| 64 | snprintf(string_encoding, sizeof(string_encoding), "CP%03" PRId32, encoding); |
| 65 | from_encoding = string_encoding; |
| 66 | } |
| 67 | |
| 68 | cd = iconv_open("UTF-8", from_encoding); |
| 69 | if (cd == (iconv_t)-1) |
| 70 | return NULL; |
| 71 | |
| 72 | string_length = strlen(string); |
| 73 | string_utf8_size = string_length * 2; |
| 74 | string_utf8 = (char *)calloc((int32_t)(string_utf8_size + 1), sizeof(char)); |
| 75 | string_utf8_ptr = string_utf8; |
| 76 | |
| 77 | if (string_utf8) { |
| 78 | result = iconv(cd, (char **)&string, &string_length, (char **)&string_utf8_ptr, &string_utf8_size); |
| 79 | } |
| 80 | |
| 81 | iconv_close(cd); |
| 82 | |
| 83 | if (result == (size_t)-1) { |
| 84 | free(string_utf8); |
| 85 | string_utf8 = NULL; |
| 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_delete(char **string) { |
| 97 | if (string) { |
| 98 | free(*string); |
| 99 | *string = NULL; |
| 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_t mz_os_rand(uint8_t *buf, int32_t size) { |
| 144 | static unsigned calls = 0; |
| 145 | int32_t i = 0; |
| 146 | |
| 147 | /* Ensure different random header each time */ |
| 148 | if (++calls == 1) { |
| 149 | # define PI_SEED 3141592654UL |
| 150 | srand((unsigned)(time(NULL) ^ PI_SEED)); |
| 151 | } |
| 152 | |
| 153 | while (i < size) |
| 154 | buf[i++] = (rand() >> 7) & 0xff; |
| 155 | |
| 156 | return size; |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | int32_t mz_os_rename(const char *source_path, const char *target_path) { |
| 161 | if (rename(source_path, target_path) == -1) |
| 162 | return MZ_EXIST_ERROR; |
| 163 | |
| 164 | return MZ_OK; |
| 165 | } |
| 166 | |
| 167 | int32_t mz_os_unlink(const char *path) { |
| 168 | if (unlink(path) == -1) |
| 169 | return MZ_EXIST_ERROR; |
| 170 | |
| 171 | return MZ_OK; |
| 172 | } |
| 173 | |
| 174 | int32_t mz_os_file_exists(const char *path) { |
| 175 | struct stat path_stat; |
| 176 | |
| 177 | memset(&path_stat, 0, sizeof(path_stat)); |
| 178 | if (stat(path, &path_stat) == 0) |
| 179 | return MZ_OK; |
| 180 | return MZ_EXIST_ERROR; |
| 181 | } |
| 182 | |
| 183 | int64_t mz_os_get_file_size(const char *path) { |
| 184 | struct stat path_stat; |
| 185 | |
| 186 | memset(&path_stat, 0, sizeof(path_stat)); |
| 187 | if (stat(path, &path_stat) == 0) { |
| 188 | /* Stat returns size taken up by directory entry, so return 0 */ |
| 189 | if (S_ISDIR(path_stat.st_mode)) |
| 190 | return 0; |
| 191 | |
| 192 | return path_stat.st_size; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date) { |
| 199 | struct stat path_stat; |
| 200 | char *name = NULL; |
| 201 | int32_t err = MZ_INTERNAL_ERROR; |
| 202 | |
| 203 | memset(&path_stat, 0, sizeof(path_stat)); |
| 204 | |
| 205 | if (strcmp(path, "-") != 0) { |
| 206 | /* Not all systems allow stat'ing a file with / appended */ |
| 207 | name = strdup(path); |
| 208 | mz_path_remove_slash(name); |
| 209 | |
| 210 | if (stat(name, &path_stat) == 0) { |
| 211 | if (modified_date) |
| 212 | *modified_date = path_stat.st_mtime; |
| 213 | if (accessed_date) |
| 214 | *accessed_date = path_stat.st_atime; |
| 215 | /* Creation date not supported */ |
| 216 | if (creation_date) |
| 217 | *creation_date = 0; |
| 218 | |
| 219 | err = MZ_OK; |
| 220 | } |
| 221 | |
| 222 | free(name); |
| 223 | } |
| 224 | |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date) { |
| 229 | struct utimbuf ut; |
| 230 | |
| 231 | ut.actime = accessed_date; |
| 232 | ut.modtime = modified_date; |
| 233 | |
| 234 | /* Creation date not supported */ |
| 235 | MZ_UNUSED(creation_date); |
| 236 | |
| 237 | if (utime(path, &ut) != 0) |
| 238 | return MZ_INTERNAL_ERROR; |
| 239 | |
| 240 | return MZ_OK; |
| 241 | } |
| 242 | |
| 243 | int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes) { |
| 244 | struct stat path_stat; |
| 245 | int32_t err = MZ_OK; |
| 246 | |
| 247 | memset(&path_stat, 0, sizeof(path_stat)); |
| 248 | if (lstat(path, &path_stat) == -1) |
| 249 | err = MZ_INTERNAL_ERROR; |
| 250 | *attributes = path_stat.st_mode; |
| 251 | return err; |
| 252 | } |
| 253 | |
| 254 | int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes) { |
| 255 | int32_t err = MZ_OK; |
| 256 | |
| 257 | if (chmod(path, (mode_t)attributes) == -1) |
| 258 | err = MZ_INTERNAL_ERROR; |
| 259 | |
| 260 | return err; |
| 261 | } |
| 262 | |
| 263 | int32_t mz_os_make_dir(const char *path) { |
| 264 | int32_t err = 0; |
| 265 | |
| 266 | err = mkdir(path, 0755); |
| 267 | |
| 268 | if (err != 0 && errno != EEXIST) |
| 269 | return MZ_INTERNAL_ERROR; |
| 270 | |
| 271 | return MZ_OK; |
| 272 | } |
| 273 | |
| 274 | DIR *mz_os_open_dir(const char *path) { |
| 275 | return opendir(path); |
| 276 | } |
| 277 | |
| 278 | struct dirent *mz_os_read_dir(DIR *dir) { |
| 279 | if (!dir) |
| 280 | return NULL; |
| 281 | return readdir(dir); |
| 282 | } |
| 283 | |
| 284 | int32_t mz_os_close_dir(DIR *dir) { |
| 285 | if (!dir) |
| 286 | return MZ_PARAM_ERROR; |
| 287 | if (closedir(dir) == -1) |
| 288 | return MZ_INTERNAL_ERROR; |
| 289 | return MZ_OK; |
| 290 | } |
| 291 | |
| 292 | int32_t mz_os_is_dir_separator(const char c) { |
| 293 | #if MZ_PRESERVE_NATIVE_STRUCTURE |
| 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_t mz_os_is_dir(const char *path) { |
| 305 | struct stat path_stat; |
| 306 | |
| 307 | memset(&path_stat, 0, sizeof(path_stat)); |
| 308 | stat(path, &path_stat); |
| 309 | if (S_ISDIR(path_stat.st_mode)) |
| 310 | return MZ_OK; |
| 311 | |
| 312 | return MZ_EXIST_ERROR; |
| 313 | } |
| 314 | |
| 315 | int32_t mz_os_is_symlink(const char *path) { |
| 316 | struct stat path_stat; |
| 317 | |
| 318 | memset(&path_stat, 0, sizeof(path_stat)); |
| 319 | lstat(path, &path_stat); |
| 320 | if (S_ISLNK(path_stat.st_mode)) |
| 321 | return MZ_OK; |
| 322 | |
| 323 | return MZ_EXIST_ERROR; |
| 324 | } |
| 325 | |
| 326 | int32_t mz_os_make_symlink(const char *path, const char *target_path) { |
| 327 | if (symlink(target_path, path) != 0) |
| 328 | return MZ_INTERNAL_ERROR; |
| 329 | return MZ_OK; |
| 330 | } |
| 331 | |
| 332 | int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path) { |
| 333 | size_t length = 0; |
| 334 | |
| 335 | length = (size_t)readlink(path, target_path, max_target_path - 1); |
| 336 | if (length == (size_t)-1) |
| 337 | return MZ_EXIST_ERROR; |
| 338 | |
| 339 | target_path[length] = 0; |
| 340 | return MZ_OK; |
| 341 | } |
| 342 | |
| 343 | uint64_t mz_os_ms_time(void) { |
| 344 | struct 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_CLOCK) || _POSIX_MONOTONIC_CLOCK < 0 |
| 357 | clock_gettime(CLOCK_REALTIME, &ts); |
| 358 | #elif _POSIX_MONOTONIC_CLOCK > 0 |
| 359 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 360 | #else |
| 361 | if (sysconf(_SC_MONOTONIC_CLOCK) > 0) |
| 362 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 363 | else |
| 364 | clock_gettime(CLOCK_REALTIME, &ts); |
| 365 | #endif |
| 366 | |
| 367 | return ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000); |
| 368 | } |