summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/assimp/contrib/zip/src/zip.c
blob: 80573096b1ecb9c4debb81e4fbe7d18efcf9a324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "zip.h"
#include "miniz.h"

#include <errno.h>
#include <sys/stat.h>
#include <time.h>

#if defined _WIN32 || defined __WIN32__
/* Win32, DOS */
#include <direct.h>

#define MKDIR(DIRNAME) _mkdir(DIRNAME)
#define STRCLONE(STR) ((STR) ? _strdup(STR) : NULL)
#define HAS_DEVICE(P)                                                          \
    ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
     (P)[1] == ':')
#define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
#define ISSLASH(C) ((C) == '/' || (C) == '\\')

#else
#define MKDIR(DIRNAME) mkdir(DIRNAME, 0755)
#define STRCLONE(STR) ((STR) ? strdup(STR) : NULL)
#endif

#ifndef FILESYSTEM_PREFIX_LEN
#define FILESYSTEM_PREFIX_LEN(P) 0
#endif

#ifndef ISSLASH
#define ISSLASH(C) ((C) == '/')
#endif

#define CLEANUP(ptr)           \
    do {                       \
        if (ptr) {             \
            free((void *)ptr); \
            ptr = NULL;        \
        }                      \
    } while (0)

static char *basename(const char *name) {
    char const *p;
    char const *base = name += FILESYSTEM_PREFIX_LEN(name);
    int all_slashes = 1;

    for (p = name; *p; p++) {
        if (ISSLASH(*p))
            base = p + 1;
        else
            all_slashes = 0;
    }

    /* If NAME is all slashes, arrange to return `/'. */
    if (*base == '\0' && ISSLASH(*name) && all_slashes) --base;

    return (char *)base;
}

static int mkpath(const char *path) {
    char const *p;
    char npath[MAX_PATH + 1] = {0};
    int len = 0;

    for (p = path; *p && len < MAX_PATH; p++) {
        if (ISSLASH(*p) && len > 0) {
            if (MKDIR(npath) == -1)
                if (errno != EEXIST) return -1;
        }
        npath[len++] = *p;
    }

    return 0;
}

static char *strrpl(const char *str, char oldchar, char newchar) {
    char *rpl = (char *)malloc(sizeof(char) * (1 + strlen(str)));
    char *begin = rpl;
    char c;
    while((c = *str++)) {
        if (c == oldchar) {
            c = newchar;
        }
        *rpl++ = c;
    }
    *rpl = '\0';

    return begin;
}

struct zip_entry_t {
    int index;
    const char *name;
    mz_uint64 uncomp_size;
    mz_uint64 comp_size;
    mz_uint32 uncomp_crc32;
    mz_uint64 offset;
    mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
    mz_uint64 header_offset;
    mz_uint16 method;
    mz_zip_writer_add_state state;
    tdefl_compressor comp;
};

struct zip_t {
    mz_zip_archive archive;
    mz_uint level;
    struct zip_entry_t entry;
    char mode;
};

struct zip_t *zip_open(const char *zipname, int level, char mode) {
    struct zip_t *zip = NULL;

    if (!zipname || strlen(zipname) < 1) {
        // zip_t archive name is empty or NULL
        goto cleanup;
    }

    if (level < 0) level = MZ_DEFAULT_LEVEL;
    if ((level & 0xF) > MZ_UBER_COMPRESSION) {
        // Wrong compression level
        goto cleanup;
    }

    zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
    if (!zip) goto cleanup;

    zip->level = level;
    zip->mode = mode;
    switch (mode) {
        case 'w':
            // Create a new archive.
            if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
                // Cannot initialize zip_archive writer
                goto cleanup;
            }
            break;

        case 'r':
        case 'a':
            if (!mz_zip_reader_init_file(
                    &(zip->archive), zipname,
                    level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
                // An archive file does not exist or cannot initialize
                // zip_archive reader
                goto cleanup;
            }

            if (mode == 'a' &&
                !mz_zip_writer_init_from_reader(&(zip->archive), zipname)) {
                mz_zip_reader_end(&(zip->archive));
                goto cleanup;
            }

            break;

        default:
            goto cleanup;
    }

    return zip;

cleanup:
    CLEANUP(zip);
    return NULL;
}

void zip_close(struct zip_t *zip) {
    if (zip) {
        // Always finalize, even if adding failed for some reason, so we have a
        // valid central directory.
        mz_zip_writer_finalize_archive(&(zip->archive));

        mz_zip_writer_end(&(zip->archive));
        mz_zip_reader_end(&(zip->archive));

        CLEANUP(zip);
    }
}

int zip_entry_open(struct zip_t *zip, const char *entryname) {
    char *locname = NULL;
    size_t entrylen = 0;
    mz_zip_archive *pzip = NULL;
    mz_uint num_alignment_padding_bytes, level;

    if (!zip || !entryname) {
        return -1;
    }

    entrylen = strlen(entryname);
    if (entrylen < 1) {
        return -1;
    }

    pzip = &(zip->archive);
    /*
      .ZIP File Format Specification Version: 6.3.3

      4.4.17.1 The name of the file, with optional relative path.
      The path stored MUST not contain a drive or
      device letter, or a leading slash.  All slashes
      MUST be forward slashes '/' as opposed to
      backwards slashes '\' for compatibility with Amiga
      and UNIX file systems etc.  If input came from standard
      input, there is no file name field.
    */
    locname = strrpl(entryname, '\\', '/');

    if (zip->mode == 'r') {
        zip->entry.index = mz_zip_reader_locate_file(pzip, locname, NULL, 0);
        CLEANUP(locname);
        return (zip->entry.index < 0) ? -1 : 0;
    }

    zip->entry.index = zip->archive.m_total_files;
    zip->entry.name = locname;
    if (!zip->entry.name) {
        // Cannot parse zip entry name
        return -1;
    }

    zip->entry.comp_size = 0;
    zip->entry.uncomp_size = 0;
    zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
    zip->entry.offset = zip->archive.m_archive_size;
    zip->entry.header_offset = zip->archive.m_archive_size;
    memset(zip->entry.header, 0,
           MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
    zip->entry.method = 0;

    num_alignment_padding_bytes =
        mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);

    if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
        // Wrong zip mode
        return -1;
    }
    if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
        // Wrong zip compression level
        return -1;
    }
    // no zip64 support yet
    if ((pzip->m_total_files == 0xFFFF) ||
        ((pzip->m_archive_size + num_alignment_padding_bytes +
          MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
          entrylen) > 0xFFFFFFFF)) {
        // No zip64 support yet
        return -1;
    }
    if (!mz_zip_writer_write_zeros(
            pzip, zip->entry.offset,
            num_alignment_padding_bytes + sizeof(zip->entry.header))) {
        // Cannot memset zip entry header
        return -1;
    }

    zip->entry.header_offset += num_alignment_padding_bytes;
    if (pzip->m_file_offset_alignment) {
        MZ_ASSERT((zip->entry.header_offset &
                   (pzip->m_file_offset_alignment - 1)) == 0);
    }
    zip->entry.offset +=
        num_alignment_padding_bytes + sizeof(zip->entry.header);

    if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
                       entrylen) != entrylen) {
        // Cannot write data to zip entry
        return -1;
    }

    zip->entry.offset += entrylen;
    level = zip->level & 0xF;
    if (level) {
        zip->entry.state.m_pZip = pzip;
        zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
        zip->entry.state.m_comp_size = 0;

        if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
                       &(zip->entry.state),
                       tdefl_create_comp_flags_from_zip_params(
                           level, -15, MZ_DEFAULT_STRATEGY)) !=
            TDEFL_STATUS_OKAY) {
            // Cannot initialize the zip compressor
            return -1;
        }
    }

    return 0;
}

int zip_entry_close(struct zip_t *zip) {
    mz_zip_archive *pzip = NULL;
    mz_uint level;
    tdefl_status done;
    mz_uint16 entrylen;
    time_t t;
    struct tm *tm;
    mz_uint16 dos_time, dos_date;
    int status = -1;

    if (!zip) {
        // zip_t handler is not initialized
        return -1;
    }

    if (zip->mode == 'r') {
        return 0;
    }

    pzip = &(zip->archive);
    level = zip->level & 0xF;
    if (level) {
        done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
        if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
            // Cannot flush compressed buffer
            goto cleanup;
        }
        zip->entry.comp_size = zip->entry.state.m_comp_size;
        zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
        zip->entry.method = MZ_DEFLATED;
    }

    entrylen = (mz_uint16)strlen(zip->entry.name);
    t = time(NULL);
    tm = localtime(&t);
    dos_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) +
                           ((tm->tm_sec) >> 1));
    dos_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) +
                           ((tm->tm_mon + 1) << 5) + tm->tm_mday);

    // no zip64 support yet
    if ((zip->entry.comp_size > 0xFFFFFFFF) ||
        (zip->entry.offset > 0xFFFFFFFF)) {
        // No zip64 support, yet
        goto cleanup;
    }

    if (!mz_zip_writer_create_local_dir_header(
            pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
            zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
            dos_time, dos_date)) {
        // Cannot create zip entry header
        goto cleanup;
    }

    if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
                       zip->entry.header, sizeof(zip->entry.header)) !=
        sizeof(zip->entry.header)) {
        // Cannot write zip entry header
        goto cleanup;
    }

    if (!mz_zip_writer_add_to_central_dir(
            pzip, zip->entry.name, entrylen, NULL, 0, "", 0,
            zip->entry.uncomp_size, zip->entry.comp_size,
            zip->entry.uncomp_crc32, zip->entry.method, 0, dos_time, dos_date,
            zip->entry.header_offset, 0)) {
        // Cannot write to zip central dir
        goto cleanup;
    }

    pzip->m_total_files++;
    pzip->m_archive_size = zip->entry.offset;
    status = 0;

cleanup:
    CLEANUP(zip->entry.name);
    return status;
}

int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
    mz_uint level;
    mz_zip_archive *pzip = NULL;
    tdefl_status status;

    if (!zip) {
        // zip_t handler is not initialized
        return -1;
    }

    pzip = &(zip->archive);
    if (buf && bufsize > 0) {
        zip->entry.uncomp_size += bufsize;
        zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
            zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);

        level = zip->level & 0xF;
        if (!level) {
            if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
                                bufsize) != bufsize)) {
                // Cannot write buffer
                return -1;
            }
            zip->entry.offset += bufsize;
            zip->entry.comp_size += bufsize;
        } else {
            status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
                                           TDEFL_NO_FLUSH);
            if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
                // Cannot compress buffer
                return -1;
            }
        }
    }

    return 0;
}

int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
    int status = 0;
    size_t n = 0;
    FILE *stream = NULL;
    mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE] = {0};

    if (!zip) {
        // zip_t handler is not initialized
        return -1;
    }

    stream = fopen(filename, "rb");
    if (!stream) {
        // Cannot open filename
        return -1;
    }

    while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
           0) {
        if (zip_entry_write(zip, buf, n) < 0) {
            status = -1;
            break;
        }
    }
    fclose(stream);

    return status;
}

int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) {
    mz_zip_archive *pzip = NULL;
    mz_uint idx;

    if (!zip) {
        // zip_t handler is not initialized
        return -1;
    }

    if (zip->mode != 'r' || zip->entry.index < 0) {
        // the entry is not found or we do not have read access
        return -1;
    }

    pzip = &(zip->archive);
    idx = (mz_uint)zip->entry.index;
    if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
        // the entry is a directory
        return -1;
    }

    *buf = mz_zip_reader_extract_to_heap(pzip, idx, bufsize, 0);
    return (*buf) ? 0 : -1;
}

int zip_entry_fread(struct zip_t *zip, const char *filename) {
    mz_zip_archive *pzip = NULL;
    mz_uint idx;

    if (!zip) {
        // zip_t handler is not initialized
        return -1;
    }

    if (zip->mode != 'r' || zip->entry.index < 0) {
        // the entry is not found or we do not have read access
        return -1;
    }

    pzip = &(zip->archive);
    idx = (mz_uint)zip->entry.index;
    if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
        // the entry is a directory
        return -1;
    }

    return (mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) ? 0 : -1;
}

int zip_entry_extract(struct zip_t *zip,
                      size_t (*on_extract)(void *arg, unsigned long long offset,
                                           const void *buf, size_t bufsize),
                      void *arg) {
    mz_zip_archive *pzip = NULL;
    mz_uint idx;

    if (!zip) {
        // zip_t handler is not initialized
        return -1;
    }

    if (zip->mode != 'r' || zip->entry.index < 0) {
        // the entry is not found or we do not have read access
        return -1;
    }

    pzip = &(zip->archive);
    idx = (mz_uint)zip->entry.index;

    return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0))
               ? 0
               : -1;
}

int zip_create(const char *zipname, const char *filenames[], size_t len) {
    int status = 0;
    size_t i;
    mz_zip_archive zip_archive;

    if (!zipname || strlen(zipname) < 1) {
        // zip_t archive name is empty or NULL
        return -1;
    }

    // Create a new archive.
    if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
        // Cannot memset zip archive
        return -1;
    }

    if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
        // Cannot initialize zip_archive writer
        return -1;
    }

    for (i = 0; i < len; ++i) {
        const char *name = filenames[i];
        if (!name) {
            status = -1;
            break;
        }

        if (!mz_zip_writer_add_file(&zip_archive, basename(name), name, "", 0,
                                    ZIP_DEFAULT_COMPRESSION_LEVEL)) {
            // Cannot add file to zip_archive
            status = -1;
            break;
        }
    }

    mz_zip_writer_finalize_archive(&zip_archive);
    mz_zip_writer_end(&zip_archive);
    return status;
}

int zip_extract(const char *zipname, const char *dir,
                int (*on_extract)(const char *filename, void *arg), void *arg) {
    int status = -1;
    mz_uint i, n;
    char path[MAX_PATH + 1] = {0};
    mz_zip_archive zip_archive;
    mz_zip_archive_file_stat info;
    size_t dirlen = 0;

    if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
        // Cannot memset zip archive
        return -1;
    }

    if (!zipname || !dir) {
        // Cannot parse zip archive name
        return -1;
    }

    dirlen = strlen(dir);
    if (dirlen + 1 > MAX_PATH) {
        return -1;
    }

    // Now try to open the archive.
    if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
        // Cannot initialize zip_archive reader
        return -1;
    }

    strcpy(path, dir);
    if (!ISSLASH(path[dirlen - 1])) {
#if defined _WIN32 || defined __WIN32__
        path[dirlen] = '\\';
#else
        path[dirlen] = '/';
#endif
        ++dirlen;
    }

    // Get and print information about each file in the archive.
    n = mz_zip_reader_get_num_files(&zip_archive);
    for (i = 0; i < n; ++i) {
        if (!mz_zip_reader_file_stat(&zip_archive, i, &info)) {
            // Cannot get information about zip archive;
            goto out;
        }
        strncpy(&path[dirlen], info.m_filename, MAX_PATH - dirlen);
        if (mkpath(path) < 0) {
            // Cannot make a path
            goto out;
        }

        if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) {
            if (!mz_zip_reader_extract_to_file(&zip_archive, i, path, 0)) {
                // Cannot extract zip archive to file
                goto out;
            }
        }

        if (on_extract) {
            if (on_extract(path, arg) < 0) {
                goto out;
            }
        }
    }
    status = 0;

out:
    // Close the archive, freeing any resources it was using
    if (!mz_zip_reader_end(&zip_archive)) {
        // Cannot end zip reader
        status = -1;
    }

    return status;
}