| 1 | /* mz_strm_split.c -- Stream for split files |
| 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_os.h" |
| 13 | #include "mz_strm.h" |
| 14 | #include "mz_strm_split.h" |
| 15 | |
| 16 | #include <stdio.h> /* snprintf */ |
| 17 | #include <string.h> |
| 18 | |
| 19 | #if defined(_MSC_VER) && (_MSC_VER < 1900) |
| 20 | # define snprintf _snprintf |
| 21 | #endif |
| 22 | |
| 23 | /***************************************************************************/ |
| 24 | |
| 25 | #define MZ_ZIP_MAGIC_DISKHEADER (0x08074b50) |
| 26 | |
| 27 | /***************************************************************************/ |
| 28 | |
| 29 | static mz_stream_vtbl mz_stream_split_vtbl = { |
| 30 | mz_stream_split_open, mz_stream_split_is_open, mz_stream_split_read, mz_stream_split_write, |
| 31 | mz_stream_split_tell, mz_stream_split_seek, mz_stream_split_close, mz_stream_split_error, |
| 32 | mz_stream_split_create, mz_stream_split_delete, mz_stream_split_get_prop_int64, mz_stream_split_set_prop_int64}; |
| 33 | |
| 34 | /***************************************************************************/ |
| 35 | |
| 36 | typedef struct mz_stream_split_s { |
| 37 | mz_stream stream; |
| 38 | int64_t disk_size; |
| 39 | int64_t total_in; |
| 40 | int64_t total_in_disk; |
| 41 | int64_t total_out; |
| 42 | int64_t total_out_disk; |
| 43 | int32_t is_open; |
| 44 | int32_t mode; |
| 45 | char *path_cd; |
| 46 | char *path_disk; |
| 47 | uint32_t path_disk_size; |
| 48 | int32_t number_disk; |
| 49 | int64_t current_disk_size; |
| 50 | int32_t current_disk; |
| 51 | int32_t reached_end; |
| 52 | } mz_stream_split; |
| 53 | |
| 54 | /***************************************************************************/ |
| 55 | |
| 56 | #if 0 |
| 57 | # define mz_stream_split_print printf |
| 58 | #else |
| 59 | # define mz_stream_split_print(fmt, ...) |
| 60 | #endif |
| 61 | |
| 62 | /***************************************************************************/ |
| 63 | |
| 64 | static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk) { |
| 65 | mz_stream_split *split = (mz_stream_split *)stream; |
| 66 | uint32_t magic = 0; |
| 67 | int64_t position = 0; |
| 68 | int32_t i = 0; |
| 69 | int32_t err = MZ_OK; |
| 70 | int16_t disk_part = 0; |
| 71 | |
| 72 | /* Check if we are reading or writing a disk part or the cd disk */ |
| 73 | if (number_disk >= 0) { |
| 74 | if ((split->mode & MZ_OPEN_MODE_WRITE) == 0) |
| 75 | disk_part = MZ_OPEN_MODE_READ; |
| 76 | else if (split->disk_size > 0) |
| 77 | disk_part = MZ_OPEN_MODE_WRITE; |
| 78 | } |
| 79 | |
| 80 | /* Construct disk path */ |
| 81 | if (disk_part > 0) { |
| 82 | for (i = (int32_t)strlen(split->path_disk) - 1; i >= 0; i -= 1) { |
| 83 | if (split->path_disk[i] != '.') |
| 84 | continue; |
| 85 | snprintf(&split->path_disk[i], split->path_disk_size - (uint32_t)i, ".z%02" PRId32, number_disk + 1); |
| 86 | break; |
| 87 | } |
| 88 | } else { |
| 89 | strncpy(split->path_disk, split->path_cd, split->path_disk_size - 1); |
| 90 | split->path_disk[split->path_disk_size - 1] = 0; |
| 91 | } |
| 92 | |
| 93 | mz_stream_split_print("Split - Goto disk - %s (disk %" PRId32 ")\n", split->path_disk, number_disk); |
| 94 | |
| 95 | /* If disk part doesn't exist during reading then return MZ_EXIST_ERROR */ |
| 96 | if (disk_part == MZ_OPEN_MODE_READ) { |
| 97 | if (strcmp(split->path_disk, split->path_cd) == 0) { |
| 98 | err = MZ_EXIST_ERROR; |
| 99 | } else { |
| 100 | err = mz_os_file_exists(split->path_disk); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (err == MZ_OK) |
| 105 | err = mz_stream_open(split->stream.base, split->path_disk, split->mode); |
| 106 | |
| 107 | if (err == MZ_OK) { |
| 108 | split->total_in_disk = 0; |
| 109 | split->total_out_disk = 0; |
| 110 | split->current_disk = number_disk; |
| 111 | |
| 112 | if (split->mode & MZ_OPEN_MODE_WRITE) { |
| 113 | if ((split->current_disk == 0) && (split->disk_size > 0)) { |
| 114 | err = mz_stream_write_uint32(split->stream.base, MZ_ZIP_MAGIC_DISKHEADER); |
| 115 | |
| 116 | split->total_out_disk += 4; |
| 117 | split->total_out += split->total_out_disk; |
| 118 | } |
| 119 | } else if (split->mode & MZ_OPEN_MODE_READ) { |
| 120 | if (split->current_disk == 0) { |
| 121 | err = mz_stream_read_uint32(split->stream.base, &magic); |
| 122 | if (magic != MZ_ZIP_MAGIC_DISKHEADER) |
| 123 | err = MZ_FORMAT_ERROR; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (err == MZ_OK) { |
| 129 | /* Get the size of the current disk we are on */ |
| 130 | position = mz_stream_tell(split->stream.base); |
| 131 | mz_stream_seek(split->stream.base, 0, MZ_SEEK_END); |
| 132 | split->current_disk_size = mz_stream_tell(split->stream.base); |
| 133 | mz_stream_seek(split->stream.base, position, MZ_SEEK_SET); |
| 134 | |
| 135 | split->is_open = 1; |
| 136 | } |
| 137 | |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | static int32_t mz_stream_split_close_disk(void *stream) { |
| 142 | mz_stream_split *split = (mz_stream_split *)stream; |
| 143 | |
| 144 | if (mz_stream_is_open(split->stream.base) != MZ_OK) |
| 145 | return MZ_OK; |
| 146 | |
| 147 | mz_stream_split_print("Split - Close disk\n"); |
| 148 | return mz_stream_close(split->stream.base); |
| 149 | } |
| 150 | |
| 151 | static int32_t mz_stream_split_goto_disk(void *stream, int32_t number_disk) { |
| 152 | mz_stream_split *split = (mz_stream_split *)stream; |
| 153 | int32_t err = MZ_OK; |
| 154 | int32_t err_is_open = MZ_OK; |
| 155 | |
| 156 | err_is_open = mz_stream_is_open(split->stream.base); |
| 157 | |
| 158 | if ((split->disk_size == 0) && (split->mode & MZ_OPEN_MODE_WRITE)) { |
| 159 | if (err_is_open != MZ_OK) |
| 160 | err = mz_stream_split_open_disk(stream, number_disk); |
| 161 | } else if ((number_disk != split->current_disk) || (err_is_open != MZ_OK)) { |
| 162 | err = mz_stream_split_close_disk(stream); |
| 163 | if (err == MZ_OK) { |
| 164 | err = mz_stream_split_open_disk(stream, number_disk); |
| 165 | if (err == MZ_OK) |
| 166 | split->number_disk = number_disk; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode) { |
| 174 | mz_stream_split *split = (mz_stream_split *)stream; |
| 175 | int32_t number_disk = 0; |
| 176 | |
| 177 | split->mode = mode; |
| 178 | split->path_cd = strdup(path); |
| 179 | |
| 180 | if (!split->path_cd) |
| 181 | return MZ_MEM_ERROR; |
| 182 | |
| 183 | mz_stream_split_print("Split - Open - %s (disk %" PRId32 ")\n", split->path_cd, number_disk); |
| 184 | |
| 185 | split->path_disk_size = (uint32_t)strlen(path) + 10; |
| 186 | split->path_disk = (char *)malloc(split->path_disk_size); |
| 187 | |
| 188 | if (!split->path_disk) { |
| 189 | free(split->path_cd); |
| 190 | return MZ_MEM_ERROR; |
| 191 | } |
| 192 | |
| 193 | strncpy(split->path_disk, path, split->path_disk_size - 1); |
| 194 | split->path_disk[split->path_disk_size - 1] = 0; |
| 195 | |
| 196 | if ((mode & MZ_OPEN_MODE_WRITE) && ((mode & MZ_OPEN_MODE_APPEND) == 0)) { |
| 197 | number_disk = 0; |
| 198 | split->current_disk = -1; |
| 199 | } else { |
| 200 | number_disk = -1; |
| 201 | split->current_disk = 0; |
| 202 | } |
| 203 | |
| 204 | return mz_stream_split_goto_disk(stream, number_disk); |
| 205 | } |
| 206 | |
| 207 | int32_t mz_stream_split_is_open(void *stream) { |
| 208 | mz_stream_split *split = (mz_stream_split *)stream; |
| 209 | if (split->is_open != 1) |
| 210 | return MZ_OPEN_ERROR; |
| 211 | return MZ_OK; |
| 212 | } |
| 213 | |
| 214 | int32_t mz_stream_split_read(void *stream, void *buf, int32_t size) { |
| 215 | mz_stream_split *split = (mz_stream_split *)stream; |
| 216 | int32_t bytes_left = size; |
| 217 | int32_t read = 0; |
| 218 | int32_t err = MZ_OK; |
| 219 | uint8_t *buf_ptr = (uint8_t *)buf; |
| 220 | |
| 221 | err = mz_stream_split_goto_disk(stream, split->number_disk); |
| 222 | if (err != MZ_OK) |
| 223 | return err; |
| 224 | |
| 225 | while (bytes_left > 0) { |
| 226 | read = mz_stream_read(split->stream.base, buf_ptr, bytes_left); |
| 227 | |
| 228 | mz_stream_split_print("Split - Read disk - %" PRId32 "\n", read); |
| 229 | |
| 230 | if (read < 0) |
| 231 | return read; |
| 232 | if (read == 0) { |
| 233 | if (split->current_disk < 0) /* No more disks to goto */ |
| 234 | break; |
| 235 | if (size != bytes_left) /* Report read from previous disk before switching */ |
| 236 | break; |
| 237 | err = mz_stream_split_goto_disk(stream, split->current_disk + 1); |
| 238 | if (err == MZ_EXIST_ERROR) |
| 239 | split->current_disk = -1; |
| 240 | if (err != MZ_OK) |
| 241 | return err; |
| 242 | } |
| 243 | |
| 244 | bytes_left -= read; |
| 245 | buf_ptr += read; |
| 246 | split->total_in += read; |
| 247 | split->total_in_disk += read; |
| 248 | } |
| 249 | return size - bytes_left; |
| 250 | } |
| 251 | |
| 252 | int32_t mz_stream_split_write(void *stream, const void *buf, int32_t size) { |
| 253 | mz_stream_split *split = (mz_stream_split *)stream; |
| 254 | int64_t position = 0; |
| 255 | int32_t written = 0; |
| 256 | int32_t bytes_left = size; |
| 257 | int32_t bytes_to_write = 0; |
| 258 | int32_t bytes_avail = 0; |
| 259 | int32_t number_disk = -1; |
| 260 | int32_t err = MZ_OK; |
| 261 | const uint8_t *buf_ptr = (const uint8_t *)buf; |
| 262 | |
| 263 | position = mz_stream_tell(split->stream.base); |
| 264 | |
| 265 | while (bytes_left > 0) { |
| 266 | bytes_to_write = bytes_left; |
| 267 | |
| 268 | if (split->disk_size > 0) { |
| 269 | if ((split->total_out_disk == split->disk_size && split->total_out > 0) || |
| 270 | (split->number_disk == -1 && split->number_disk != split->current_disk)) { |
| 271 | if (split->number_disk != -1) |
| 272 | number_disk = split->current_disk + 1; |
| 273 | |
| 274 | err = mz_stream_split_goto_disk(stream, number_disk); |
| 275 | if (err != MZ_OK) |
| 276 | return err; |
| 277 | |
| 278 | position = 0; |
| 279 | } |
| 280 | |
| 281 | if (split->number_disk != -1) { |
| 282 | bytes_avail = (int32_t)(split->disk_size - split->total_out_disk); |
| 283 | if (bytes_to_write > bytes_avail) |
| 284 | bytes_to_write = bytes_avail; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | written = mz_stream_write(split->stream.base, buf_ptr, bytes_to_write); |
| 289 | if (written != bytes_to_write) |
| 290 | return MZ_WRITE_ERROR; |
| 291 | |
| 292 | mz_stream_split_print("Split - Write disk - %" PRId32 "\n", written); |
| 293 | |
| 294 | bytes_left -= written; |
| 295 | buf_ptr += written; |
| 296 | |
| 297 | split->total_out += written; |
| 298 | split->total_out_disk += written; |
| 299 | |
| 300 | position += written; |
| 301 | if (position > split->current_disk_size) |
| 302 | split->current_disk_size = position; |
| 303 | } |
| 304 | |
| 305 | return size - bytes_left; |
| 306 | } |
| 307 | |
| 308 | int64_t mz_stream_split_tell(void *stream) { |
| 309 | mz_stream_split *split = (mz_stream_split *)stream; |
| 310 | int32_t err = MZ_OK; |
| 311 | err = mz_stream_split_goto_disk(stream, split->number_disk); |
| 312 | if (err != MZ_OK) |
| 313 | return err; |
| 314 | return mz_stream_tell(split->stream.base); |
| 315 | } |
| 316 | |
| 317 | int32_t mz_stream_split_seek(void *stream, int64_t offset, int32_t origin) { |
| 318 | mz_stream_split *split = (mz_stream_split *)stream; |
| 319 | int64_t disk_left = 0; |
| 320 | int64_t position = 0; |
| 321 | int32_t err = MZ_OK; |
| 322 | |
| 323 | err = mz_stream_split_goto_disk(stream, split->number_disk); |
| 324 | |
| 325 | if (err != MZ_OK) |
| 326 | return err; |
| 327 | |
| 328 | mz_stream_split_print("Split - Seek disk - %" PRId64 " (origin %" PRId32 ")\n", offset, origin); |
| 329 | |
| 330 | if ((origin == MZ_SEEK_CUR) && (split->number_disk != -1)) { |
| 331 | position = mz_stream_tell(split->stream.base); |
| 332 | disk_left = split->current_disk_size - position; |
| 333 | |
| 334 | while (offset > disk_left) { |
| 335 | err = mz_stream_split_goto_disk(stream, split->current_disk + 1); |
| 336 | if (err != MZ_OK) |
| 337 | return err; |
| 338 | |
| 339 | offset -= disk_left; |
| 340 | disk_left = split->current_disk_size; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return mz_stream_seek(split->stream.base, offset, origin); |
| 345 | } |
| 346 | |
| 347 | int32_t mz_stream_split_close(void *stream) { |
| 348 | mz_stream_split *split = (mz_stream_split *)stream; |
| 349 | int32_t err = MZ_OK; |
| 350 | |
| 351 | err = mz_stream_split_close_disk(stream); |
| 352 | split->is_open = 0; |
| 353 | return err; |
| 354 | } |
| 355 | |
| 356 | int32_t mz_stream_split_error(void *stream) { |
| 357 | mz_stream_split *split = (mz_stream_split *)stream; |
| 358 | return mz_stream_error(split->stream.base); |
| 359 | } |
| 360 | |
| 361 | int32_t mz_stream_split_get_prop_int64(void *stream, int32_t prop, int64_t *value) { |
| 362 | mz_stream_split *split = (mz_stream_split *)stream; |
| 363 | switch (prop) { |
| 364 | case MZ_STREAM_PROP_TOTAL_OUT: |
| 365 | *value = split->total_out; |
| 366 | break; |
| 367 | case MZ_STREAM_PROP_DISK_NUMBER: |
| 368 | *value = split->number_disk; |
| 369 | break; |
| 370 | case MZ_STREAM_PROP_DISK_SIZE: |
| 371 | *value = split->disk_size; |
| 372 | break; |
| 373 | default: |
| 374 | return MZ_EXIST_ERROR; |
| 375 | } |
| 376 | return MZ_OK; |
| 377 | } |
| 378 | |
| 379 | int32_t mz_stream_split_set_prop_int64(void *stream, int32_t prop, int64_t value) { |
| 380 | mz_stream_split *split = (mz_stream_split *)stream; |
| 381 | switch (prop) { |
| 382 | case MZ_STREAM_PROP_DISK_NUMBER: |
| 383 | split->number_disk = (int32_t)value; |
| 384 | break; |
| 385 | case MZ_STREAM_PROP_DISK_SIZE: |
| 386 | split->disk_size = value; |
| 387 | break; |
| 388 | default: |
| 389 | return MZ_EXIST_ERROR; |
| 390 | } |
| 391 | return MZ_OK; |
| 392 | } |
| 393 | |
| 394 | void *mz_stream_split_create(void) { |
| 395 | mz_stream_split *split = (mz_stream_split *)calloc(1, sizeof(mz_stream_split)); |
| 396 | if (split) |
| 397 | split->stream.vtbl = &mz_stream_split_vtbl; |
| 398 | return split; |
| 399 | } |
| 400 | |
| 401 | void mz_stream_split_delete(void **stream) { |
| 402 | mz_stream_split *split = NULL; |
| 403 | if (!stream) |
| 404 | return; |
| 405 | split = (mz_stream_split *)*stream; |
| 406 | if (split) { |
| 407 | free(split->path_cd); |
| 408 | free(split->path_disk); |
| 409 | free(split); |
| 410 | } |
| 411 | *stream = NULL; |
| 412 | } |
| 413 | |
| 414 | void *mz_stream_split_get_interface(void) { |
| 415 | return (void *)&mz_stream_split_vtbl; |
| 416 | } |