summaryrefslogtreecommitdiff
path: root/Build/source/libs/zziplib/zziplib-0.13.58/zzip/memdisk.c
blob: dc00ea865087435dbab6ce2522ef294d7b0bce63 (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

/*
 * NOTE: this is part of libzzipmmapped (i.e. it is not libzzip).
 *                                            ==================
 *
 * The mem_disk cache will parse the central information of a zip archive
 * and store it internally. One the one hand it allows to find files
 * faster - no disk access is required and endian conversion is not
 * needed. If zzip is compiled with zip extensions then it is about
 * the only way to build maintainable code around the zip format.
 *
 * Note that 64bit support is almost entirely living in extension
 * blocks as well as different character encodings and file access
 * control bits that are mostly platform specific.
 *
 * Author:
 *    Guido Draheim <guidod@gmx.de>
 *
 * Copyright (c) 1999,2000,2001,2002,2003 Guido Draheim
 *          All rights reserved,
 *          use under the restrictions of the
 *          Lesser GNU General Public License
 *          or alternatively the restrictions
 *          of the Mozilla Public License 1.1
 */
#define _ZZIP_DISK_FILE_STRUCT 1

#include <zzip/types.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include <zlib.h>
#include <zzip/format.h>
#include <zzip/fetch.h>
#include <zzip/mmapped.h>
#include <zzip/memdisk.h>
#include <zzip/__fnmatch.h>

#define ___ {
#define ____ }

static const char *error[] = {
    "Ok",
#   define _zzip_mem_disk_open_fail 1
    "zzip_mem_disk_open: zzip_disk_open did fail",
#   define _zzip_mem_disk_fdopen_fail 2
    "zzip_mem_disk_fdopen: zzip_disk_mmap did fail"
#   define _zzip_mem_disk_buffer_fail 3
    "zzip_mem_disk_buffer: zzip_disk_buffer did fail",
    0
};

#define ZZIP_EXTRA_zip64 0x0001
typedef struct _zzip_extra_zip64
{                               /* ZIP64 extended information extra field */
    zzip_byte_t z_datatype[2];  /* Tag for this "extra" block type */
    zzip_byte_t z_datasize[2];  /* Size of this "extra" block */
    zzip_byte_t z_usize[8];     /* Original uncompressed file size */
    zzip_byte_t z_csize[8];     /* Size of compressed data */
    zzip_byte_t z_offset[8];    /* Offset of local header record */
    zzip_byte_t z_diskstart[4]; /* Number of the disk for file start */
} zzip_extra_zip64;

/*forward*/

static zzip__new__ ZZIP_MEM_ENTRY *
zzip_mem_entry_new(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry);
static void
zzip_mem_entry_free(ZZIP_MEM_ENTRY * _zzip_restrict item);

zzip__new__ ZZIP_MEM_DISK *
zzip_mem_disk_new(void)
{
    return calloc(1, sizeof(ZZIP_MEM_DISK));
}

/** create new diskdir handle.
 *  wraps underlying zzip_disk_open. */
zzip__new__ ZZIP_MEM_DISK *
zzip_mem_disk_open(char *filename)
{
    ZZIP_DISK *disk = zzip_disk_open(filename);
    if (! disk)
        { perror(error[_zzip_mem_disk_open_fail]); return 0; }
    ___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new();
    zzip_mem_disk_load(dir, disk);
    return dir;
    ____;
}

/** create new diskdir handle.
 *  wraps underlying zzip_disk_open. */
zzip__new__ ZZIP_MEM_DISK *
zzip_mem_disk_fdopen(int fd)
{
    ZZIP_DISK *disk = zzip_disk_mmap(fd);
    if (! disk)
        { perror(error[_zzip_mem_disk_fdopen_fail]); return 0; }
    ___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new();
    zzip_mem_disk_load(dir, disk);
    return dir;
    ____;
}

/** create new diskdir handle.
 *  wraps underlying zzip_disk_buffer. */
zzip__new__ ZZIP_MEM_DISK *
zzip_mem_disk_buffer(char *buffer, size_t buflen)
{
    ZZIP_DISK *disk = zzip_disk_buffer(buffer, buflen);
    if (! disk)
        { perror(error[_zzip_mem_disk_buffer_fail]); return 0; }
    ___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new();
    zzip_mem_disk_load(dir, disk);
    return dir;
    ____;
}

/** parse central dir.
 *  creates an internal copy of each entry converted to the local platform.
 *  returns: number of entries, or -1 on error (setting errno)
 */
long
zzip_mem_disk_load(ZZIP_MEM_DISK * dir, ZZIP_DISK * disk)
{
    if (! dir || ! disk)
        { errno=EINVAL; return -1; }
    if (dir->list)
        zzip_mem_disk_unload(dir);
    ___ long count = 0;
    ___ struct zzip_disk_entry *entry = zzip_disk_findfirst(disk);
    for (; entry; entry = zzip_disk_findnext(disk, entry))
    {
        ZZIP_MEM_ENTRY *item = zzip_mem_entry_new(disk, entry);
        if (! item)
            goto error;
        if (dir->last)
        {
            dir->last->zz_next = item;  /* chain last */
        } else
        {
            dir->list = item;
        }
        dir->last = item;       /* to earlier */
        count++;
    }
    ____;
    dir->disk = disk;
    return count;
    ____;
  error:
    zzip_mem_disk_unload(dir);
    return -1;
}

/** convert a zip disk entry to internal format.
 * creates a new item parsing the information out of the various places
 * in the zip archive. This is a good place to extend functionality if
 * you have a project with extra requirements as you can push more bits
 * right into the diskdir_entry for later usage in higher layers.
 * returns: new item, or null on error (setting errno)
 */
zzip__new__ ZZIP_MEM_ENTRY *
zzip_mem_entry_new(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry)
{
    if (! disk || ! entry)
        { errno=EINVAL; return 0; }
    ___ ZZIP_MEM_ENTRY *item = calloc(1, sizeof(*item));
    if (! item)
        return 0;               /* errno=ENOMEM; */
    ___ struct zzip_file_header *header =
        zzip_disk_entry_to_file_header(disk, entry);
    /*  there is a number of duplicated information in the file header
     *  or the disk entry block. Theoretically some part may be missing
     *  that exists in the other, ... but we will prefer the disk entry.
     */
    item->zz_comment = zzip_disk_entry_strdup_comment(disk, entry);
    item->zz_name = zzip_disk_entry_strdup_name(disk, entry);
    item->zz_data = zzip_file_header_to_data(header);
    item->zz_flags = zzip_disk_entry_get_flags(entry);
    item->zz_compr = zzip_disk_entry_get_compr(entry);
    item->zz_mktime = zzip_disk_entry_get_mktime(entry);
    item->zz_crc32 = zzip_disk_entry_get_crc32(entry);
    item->zz_csize = zzip_disk_entry_get_csize(entry);
    item->zz_usize = zzip_disk_entry_get_usize(entry);
    item->zz_diskstart = zzip_disk_entry_get_diskstart(entry);
    item->zz_filetype = zzip_disk_entry_get_filetype(entry);

    {                           /* copy the extra blocks to memory as well */
        int /*            */ ext1 = zzip_disk_entry_get_extras(entry);
        char *_zzip_restrict ptr1 = zzip_disk_entry_to_extras(entry);
        int /*            */ ext2 = zzip_file_header_get_extras(header);
        char *_zzip_restrict ptr2 = zzip_file_header_to_extras(header);

        if (ext1)
        {
            void *mem = malloc(ext1 + 2);
            item->zz_ext[1] = mem;
            memcpy(mem, ptr1, ext1);
            ((char *) (mem))[ext1 + 0] = 0;
            ((char *) (mem))[ext1 + 1] = 0;
        }
        if (ext2)
        {
            void *mem = malloc(ext2 + 2);
            item->zz_ext[2] = mem;
            memcpy(mem, ptr2, ext2);
            ((char *) (mem))[ext2 + 0] = 0;
            ((char *) (mem))[ext2 + 1] = 0;
        }
    }
    {
        /* override sizes/offsets with zip64 values for largefile support */
        zzip_extra_zip64 *block = (zzip_extra_zip64 *)
            zzip_mem_entry_extra_block(item, ZZIP_EXTRA_zip64);
        if (block)
        {
            item->zz_usize = __zzip_get64(block->z_usize);
            item->zz_csize = __zzip_get64(block->z_csize);
            item->zz_offset = __zzip_get64(block->z_offset);
            item->zz_diskstart = __zzip_get32(block->z_diskstart);
        }
    }
    /* NOTE:
     * All information from the central directory entry is now in memory.
     * Effectivly that allows us to modify it and write it back to disk.
     */
    return item;
    ____;
    ____;
}

/* find an extra block for the given datatype code.
 * We assume that the central directory has been preparsed to memory.
 */
ZZIP_EXTRA_BLOCK *
zzip_mem_entry_extra_block(ZZIP_MEM_ENTRY * entry, short datatype)
{
    int i = 2;
    while (1)
    {
        ZZIP_EXTRA_BLOCK *ext = entry->zz_ext[i];
        if (ext)
        {
            while (*(short *) (ext->z_datatype))
            {
                if (datatype == zzip_extra_block_get_datatype(ext))
                {
                    return ext;
                }
                ___ char *e = (char *) ext;
                e += zzip_extra_block_headerlength;
                e += zzip_extra_block_get_datasize(ext);
                ext = (void *) e;
                ____;
            }
        }
        if (! i)
            return 0;
        i--;
    }
}

void
zzip_mem_entry_free(ZZIP_MEM_ENTRY * _zzip_restrict item)
{
    if (item)
    {
	/* *INDENT-OFF* */
	if (item->zz_ext[0]) free (item->zz_ext[0]);
	if (item->zz_ext[1]) free (item->zz_ext[1]);
	if (item->zz_ext[2]) free (item->zz_ext[2]);
	if (item->zz_comment) free (item->zz_comment);
	if (item->zz_name) free (item->zz_name);
	free (item);
	/* *INDENT-ON* */
    }
}

void
zzip_mem_disk_unload(ZZIP_MEM_DISK * dir)
{
    ZZIP_MEM_ENTRY *item = dir->list;
    while (item)
    {
        ZZIP_MEM_ENTRY *next = item->zz_next;
        zzip_mem_entry_free(item);
        item = next;
    }
    dir->list = dir->last = 0;
    zzip_disk_close(dir->disk);
    dir->disk = 0;
}

void
zzip_mem_disk_close(ZZIP_MEM_DISK * _zzip_restrict dir)
{
    if (dir)
    {
        zzip_mem_disk_unload(dir);
        zzip_disk_close(dir->disk);
        free(dir);
    }
}

#if 0
static void
foo(short zz_datatype)
{
    switch (zz_datatype)
    {
    /* *INDENT-OFF* */
    case 0x0001: /* ZIP64 extended information extra field */
    case 0x0007: /* AV Info */
    case 0x0008: /* Reserved for future Unicode file name data (PFS) */
    case 0x0009: /* OS/2 */
    case 0x000a: /* NTFS */
    case 0x000c: /* OpenVMS */
    case 0x000d: /* Unix */
    case 0x000e: /* Reserved for file stream and fork descriptors */
    case 0x000f: /* Patch Descriptor */
    case 0x0014: /* PKCS#7 Store for X.509 Certificates */
    case 0x0015: /* X.509 Certificate ID and Signature for file */
    case 0x0016: /* X.509 Certificate ID for Central Directory */
    case 0x0017: /* Strong Encryption Header */
    case 0x0018: /* Record Management Controls */
    case 0x0019: /* PKCS#7 Encryption Recipient Certificate List */
    case 0x0065: /* IBM S/390, AS/400 attributes - uncompressed */
    case 0x0066: /* Reserved for IBM S/390, AS/400 attr - compressed */
    case 0x07c8: /* Macintosh */
    case 0x2605: /* ZipIt Macintosh */
    case 0x2705: /* ZipIt Macintosh 1.3.5+ */
    case 0x2805: /* ZipIt Macintosh 1.3.5+ */
    case 0x334d: /* Info-ZIP Macintosh */
    case 0x4341: /* Acorn/SparkFS  */
    case 0x4453: /* Windows NT security descriptor (binary ACL) */
    case 0x4704: /* VM/CMS */
    case 0x470f: /* MVS */
    case 0x4b46: /* FWKCS MD5 (see below) */
    case 0x4c41: /* OS/2 access control list (text ACL) */
    case 0x4d49: /* Info-ZIP OpenVMS */
    case 0x4f4c: /* Xceed original location extra field */
    case 0x5356: /* AOS/VS (ACL) */
    case 0x5455: /* extended timestamp */
    case 0x554e: /* Xceed unicode extra field */
    case 0x5855: /* Info-ZIP Unix (original, also OS/2, NT, etc) */
    case 0x6542: /* BeOS/BeBox */
    case 0x756e: /* ASi Unix */
    case 0x7855: /* Info-ZIP Unix (new) */
    case 0xfd4a: /* SMS/QDOS */
    /* *INDENT-ON* */
    }
}
#endif

ZZIP_MEM_ENTRY *
zzip_mem_disk_findfile(ZZIP_MEM_DISK * dir,
                       char *filename, ZZIP_MEM_ENTRY * after,
                       zzip_strcmp_fn_t compare)
{
    ZZIP_MEM_ENTRY *entry = (! after ? dir->list : after->zz_next);
    if (! compare)
        compare = (zzip_strcmp_fn_t) (strcmp);
    for (; entry; entry = entry->zz_next)
    {
        if (! compare(filename, entry->zz_name))
        {
            return entry;
        }
    }
    return 0;
}

ZZIP_MEM_ENTRY *
zzip_mem_disk_findmatch(ZZIP_MEM_DISK * dir,
                        char *filespec, ZZIP_MEM_ENTRY * after,
                        zzip_fnmatch_fn_t compare, int flags)
{
    ZZIP_MEM_ENTRY *entry = (! after ? dir->list : after->zz_next);
    if (! compare)
        compare = (zzip_fnmatch_fn_t) _zzip_fnmatch;
    for (; entry; entry = entry->zz_next)
    {
        if (! compare(filespec, entry->zz_name, flags))
        {
            return entry;
        }
    }
    return 0;
}

zzip__new__ ZZIP_MEM_DISK_FILE *
zzip_mem_entry_fopen(ZZIP_MEM_DISK * dir, ZZIP_MEM_ENTRY * entry)
{
    /* keep this in sync with zzip_disk_entry_fopen */
    ZZIP_DISK_FILE *file = malloc(sizeof(ZZIP_MEM_DISK_FILE));
    if (! file)
        return file;
    file->buffer = dir->disk->buffer;
    file->endbuf = dir->disk->endbuf;
    file->avail = zzip_mem_entry_usize(entry);

    if (! file->avail || zzip_mem_entry_data_stored(entry))
        { file->stored = zzip_mem_entry_to_data (entry); return file; }

    file->stored = 0;
    file->zlib.opaque = 0;
    file->zlib.zalloc = Z_NULL;
    file->zlib.zfree = Z_NULL;
    file->zlib.avail_in = zzip_mem_entry_csize(entry);
    file->zlib.next_in = zzip_mem_entry_to_data(entry);

    if (! zzip_mem_entry_data_deflated(entry) ||
        inflateInit2(&file->zlib, -MAX_WBITS) != Z_OK)
        { free (file); return 0; }

    return file;
}

zzip__new__ ZZIP_MEM_DISK_FILE *
zzip_mem_disk_fopen(ZZIP_MEM_DISK * dir, char *filename)
{
    ZZIP_MEM_ENTRY *entry = zzip_mem_disk_findfile(dir, filename, 0, 0);
    if (! entry)
        return 0;
    else
        return zzip_mem_entry_fopen(dir, entry);
}

_zzip_size_t
zzip_mem_disk_fread(void *ptr, _zzip_size_t size, _zzip_size_t nmemb,
                    ZZIP_MEM_DISK_FILE * file)
{
    return zzip_disk_fread(ptr, size, nmemb, file);
}

int
zzip_mem_disk_fclose(ZZIP_MEM_DISK_FILE * file)
{
    return zzip_disk_fclose(file);
}

int
zzip_mem_disk_feof(ZZIP_MEM_DISK_FILE * file)
{
    return zzip_disk_feof(file);
}

/* convert dostime of entry to unix time_t */
long
zzip_disk_entry_get_mktime(ZZIP_DISK_ENTRY * entry)
{
    uint16_t dostime = ZZIP_GET16(entry->z_dostime.time);
    uint16_t dosdate = ZZIP_GET16(entry->z_dostime.date);
    struct tm date;
    date.tm_sec = (dostime) & 0x1F;     /* bits 0..4 */
    date.tm_min = (dostime >> 5) & 0x3F;        /* bits 5..10 */
    date.tm_hour = (dostime >> 11);     /* bits 11..15 */
    date.tm_mday = (dosdate) & 0x1F;    /* bits 16..20 */
    date.tm_mon = (dosdate >> 5) & 0xF; /* bits 21..24 */
    date.tm_year = (dosdate >> 9) + 80; /* bits 25..31 */
    return mktime(&date);       /* well, unix has that function... */
}