| 1 |
/* |
| 2 |
* NOTE: this is part of libzzipmmapped (i.e. it is not libzzip). |
| 3 |
* ================== |
| 4 |
* |
| 5 |
* The mem_disk cache will parse the central information of a zip archive |
| 6 |
* and store it internally. One the one hand it allows to find files |
| 7 |
* faster - no disk access is required and endian conversion is not |
| 8 |
* needed. If zzip is compiled with zip extensions then it is about |
| 9 |
* the only way to build maintainable code around the zip format. |
| 10 |
* |
| 11 |
* Note that 64bit support is almost entirely living in extension |
| 12 |
* blocks as well as different character encodings and file access |
| 13 |
* control bits that are mostly platform specific. |
| 14 |
* |
| 15 |
* Author: |
| 16 |
* Guido Draheim <[email protected]> |
| 17 |
* |
| 18 |
* Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL) |
| 19 |
*/ |
| 20 |
#define _ZZIP_DISK_FILE_STRUCT 1 |
| 21 |
|
| 22 |
#include <zzip/types.h> |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include <time.h> |
| 28 |
|
| 29 |
#include <zlib.h> |
| 30 |
#include <zzip/format.h> |
| 31 |
#include <zzip/fetch.h> |
| 32 |
#include <zzip/mmapped.h> |
| 33 |
#include <zzip/memdisk.h> |
| 34 |
#include <zzip/__fnmatch.h> |
| 35 |
#include <zzip/__errno.h> |
| 36 |
|
| 37 |
#define ___ { |
| 38 |
#define ____ } |
| 39 |
|
| 40 |
#define DEBUG 1 |
| 41 |
#ifdef DEBUG |
| 42 |
#define debug1(msg) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__); } while(0) |
| 43 |
#define debug2(msg, arg1) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__, arg1); } while(0) |
| 44 |
#define debug3(msg, arg1, arg2) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__, arg1, arg2); } while(0) |
| 45 |
#define debug4(msg, arg1, arg2, arg3) do { fprintf(stderr, "DEBUG: %s : " msg "\n", __func__, arg1, arg2, arg3); } while(0) |
| 46 |
#else |
| 47 |
#define debug1(msg) |
| 48 |
#define debug2(msg, arg1) |
| 49 |
#define debug3(msg, arg1, arg2) |
| 50 |
#define debug4(msg, arg1, arg2, arg3) |
| 51 |
#endif |
| 52 |
|
| 53 |
#define ZZIP_EXTRA_zip64 0x0001 |
| 54 |
typedef struct zzip_extra_zip64 zzip_extra_zip64; |
| 55 |
|
| 56 |
/*forward*/ |
| 57 |
|
| 58 |
static zzip__new__ ZZIP_MEM_ENTRY * |
| 59 |
zzip_mem_entry_new(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry); |
| 60 |
static void |
| 61 |
zzip_mem_entry_free(ZZIP_MEM_ENTRY * _zzip_restrict item); |
| 62 |
|
| 63 |
/** => zzip_mem_disk_open |
| 64 |
* This function is internally used to prepare opening a disk. |
| 65 |
*/ |
| 66 |
zzip__new__ ZZIP_MEM_DISK * |
| 67 |
zzip_mem_disk_new(void) |
| 68 |
{ |
| 69 |
return calloc(1, sizeof(ZZIP_MEM_DISK)); |
| 70 |
} |
| 71 |
|
| 72 |
/** create new diskdir handle. |
| 73 |
* This function wraps underlying zzip_disk_open. |
| 74 |
*/ |
| 75 |
zzip__new__ ZZIP_MEM_DISK * |
| 76 |
zzip_mem_disk_open(char *filename) |
| 77 |
{ |
| 78 |
ZZIP_DISK *disk = zzip_disk_open(filename); |
| 79 |
if (! disk) |
| 80 |
{ |
| 81 |
debug2("can not open disk file %s", filename); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new(); |
| 85 |
if (zzip_mem_disk_load(dir, disk) == -1) |
| 86 |
{ |
| 87 |
debug2("unable to load disk %s", filename); |
| 88 |
} |
| 89 |
return dir; |
| 90 |
____; |
| 91 |
} |
| 92 |
|
| 93 |
/** => zzip_mem_disk_open |
| 94 |
* This function wraps the underlying zzip_disk_open. |
| 95 |
*/ |
| 96 |
zzip__new__ ZZIP_MEM_DISK * |
| 97 |
zzip_mem_disk_fdopen(int fd) |
| 98 |
{ |
| 99 |
ZZIP_DISK *disk = zzip_disk_mmap(fd); |
| 100 |
if (! disk) |
| 101 |
{ |
| 102 |
debug2("can not open disk fd %i", fd); |
| 103 |
return 0; |
| 104 |
} |
| 105 |
___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new(); |
| 106 |
if (zzip_mem_disk_load(dir, disk) == -1) |
| 107 |
{ |
| 108 |
debug2("unable to load disk fd %i", fd); |
| 109 |
} |
| 110 |
return dir; |
| 111 |
____; |
| 112 |
} |
| 113 |
|
| 114 |
/** create new diskdir handle. |
| 115 |
* This function wraps underlying zzip_disk_buffer. |
| 116 |
*/ |
| 117 |
zzip__new__ ZZIP_MEM_DISK * |
| 118 |
zzip_mem_disk_buffer(char *buffer, size_t buflen) |
| 119 |
{ |
| 120 |
ZZIP_DISK *disk = zzip_disk_buffer(buffer, buflen); |
| 121 |
if (! disk) |
| 122 |
{ |
| 123 |
debug2("can not open disk buf %p", buffer); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
___ ZZIP_MEM_DISK *dir = zzip_mem_disk_new(); |
| 127 |
if (zzip_mem_disk_load(dir, disk) == -1) |
| 128 |
{ |
| 129 |
debug2("unable to load disk buf %p", buffer); |
| 130 |
} |
| 131 |
return dir; |
| 132 |
____; |
| 133 |
} |
| 134 |
|
| 135 |
/** parse central dir. |
| 136 |
* creates an internal copy of each entry converted to the local platform. |
| 137 |
* returns: number of entries, or -1 on error (setting errno) |
| 138 |
*/ |
| 139 |
long |
| 140 |
zzip_mem_disk_load(ZZIP_MEM_DISK * dir, ZZIP_DISK * disk) |
| 141 |
{ |
| 142 |
if (! dir || ! disk) |
| 143 |
{ errno=EINVAL; return -1; } |
| 144 |
if (dir->list) |
| 145 |
zzip_mem_disk_unload(dir); |
| 146 |
___ long count = 0; |
| 147 |
___ struct zzip_disk_entry *entry = zzip_disk_findfirst(disk); |
| 148 |
if (!entry) goto error; |
| 149 |
for (; entry; entry = zzip_disk_findnext(disk, entry)) |
| 150 |
{ |
| 151 |
ZZIP_MEM_ENTRY *item = zzip_mem_entry_new(disk, entry); |
| 152 |
if (! item) |
| 153 |
{ |
| 154 |
debug1("unable to load entry"); |
| 155 |
goto error; |
| 156 |
} |
| 157 |
if (dir->last) |
| 158 |
{ |
| 159 |
dir->last->zz_next = item; /* chain last */ |
| 160 |
} else |
| 161 |
{ |
| 162 |
dir->list = item; |
| 163 |
} |
| 164 |
dir->last = item; /* to earlier */ |
| 165 |
count++; |
| 166 |
} |
| 167 |
____; |
| 168 |
dir->disk = disk; |
| 169 |
return count; |
| 170 |
____; |
| 171 |
error: |
| 172 |
zzip_mem_disk_unload(dir); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
/** convert a zip disk entry to internal format. |
| 177 |
* creates a new item parsing the information out of the various places |
| 178 |
* in the zip archive. This is a good place to extend functionality if |
| 179 |
* you have a project with extra requirements as you can push more bits |
| 180 |
* right into the diskdir_entry for later usage in higher layers. |
| 181 |
* returns: new item, or null on error (setting errno = ENOMEM|EBADMSG) |
| 182 |
*/ |
| 183 |
zzip__new__ ZZIP_MEM_ENTRY * |
| 184 |
zzip_mem_entry_new(ZZIP_DISK * disk, ZZIP_DISK_ENTRY * entry) |
| 185 |
{ |
| 186 |
if (! disk || ! entry) |
| 187 |
{ errno=EINVAL; return 0; } |
| 188 |
___ ZZIP_MEM_ENTRY *item = calloc(1, sizeof(*item)); |
| 189 |
if (! item) |
| 190 |
return 0; /* errno=ENOMEM; */ |
| 191 |
___ struct zzip_file_header *header = |
| 192 |
zzip_disk_entry_to_file_header(disk, entry); |
| 193 |
if (! header) |
| 194 |
{ |
| 195 |
debug1("no header in entry"); |
| 196 |
free (item); |
| 197 |
return 0; /* errno=EBADMSG; */ |
| 198 |
} |
| 199 |
/* there is a number of duplicated information in the file header |
| 200 |
* or the disk entry block. Theoretically some part may be missing |
| 201 |
* that exists in the other, ... but we will prefer the disk entry. |
| 202 |
*/ |
| 203 |
item->zz_comment = zzip_disk_entry_strdup_comment(disk, entry); |
| 204 |
item->zz_name = zzip_disk_entry_strdup_name(disk, entry); |
| 205 |
item->zz_data = zzip_file_header_to_data(header); |
| 206 |
item->zz_flags = zzip_disk_entry_get_flags(entry); |
| 207 |
item->zz_compr = zzip_disk_entry_get_compr(entry); |
| 208 |
item->zz_mktime = zzip_disk_entry_get_mktime(entry); |
| 209 |
item->zz_crc32 = zzip_disk_entry_get_crc32(entry); |
| 210 |
item->zz_csize = zzip_disk_entry_get_csize(entry); |
| 211 |
item->zz_usize = zzip_disk_entry_get_usize(entry); |
| 212 |
item->zz_diskstart = zzip_disk_entry_get_diskstart(entry); |
| 213 |
item->zz_filetype = zzip_disk_entry_get_filetype(entry); |
| 214 |
|
| 215 |
/* |
| 216 |
* If zz_data+zz_csize exceeds the size of the file, bail out |
| 217 |
*/ |
| 218 |
if ((item->zz_data + item->zz_csize) < disk->buffer || |
| 219 |
(item->zz_data + item->zz_csize) >= disk->endbuf) |
| 220 |
{ |
| 221 |
goto error; |
| 222 |
} |
| 223 |
/* |
| 224 |
* If the file is uncompressed, zz_csize and zz_usize should be the same |
| 225 |
* If they are not, we cannot guarantee that either is correct, so ... |
| 226 |
*/ |
| 227 |
if (item->zz_compr == ZZIP_IS_STORED && item->zz_csize != item->zz_usize) |
| 228 |
{ |
| 229 |
goto error; |
| 230 |
} |
| 231 |
/* zz_comment and zz_name are empty strings if not present on disk */ |
| 232 |
if (! item->zz_comment || ! item->zz_name) |
| 233 |
{ |
| 234 |
goto error; /* errno=ENOMEM */ |
| 235 |
} |
| 236 |
|
| 237 |
{ /* copy the extra blocks to memory as well (maximum 64K each) */ |
| 238 |
zzip_size_t /* */ ext1_len = zzip_disk_entry_get_extras(entry); |
| 239 |
zzip_byte_t *_zzip_restrict ext1_ptr = zzip_disk_entry_to_extras(entry); |
| 240 |
zzip_size_t /* */ ext2_len = zzip_file_header_get_extras(header); |
| 241 |
zzip_byte_t *_zzip_restrict ext2_ptr = zzip_file_header_to_extras(header); |
| 242 |
|
| 243 |
if (ext1_len > 0 && ext1_len <= 65535) |
| 244 |
{ |
| 245 |
if (ext1_ptr + ext1_len >= disk->endbuf) |
| 246 |
{ |
| 247 |
errno = EBADMSG; |
| 248 |
goto error; /* zzip_mem_entry_free(item); return 0; */ |
| 249 |
} else { |
| 250 |
void *mem = malloc(ext1_len); |
| 251 |
if (! mem) goto error; /* errno = ENOMEM */ |
| 252 |
item->zz_ext[1] = mem; |
| 253 |
item->zz_extlen[1] = ext1_len; |
| 254 |
memcpy(mem, ext1_ptr, ext1_len); |
| 255 |
} |
| 256 |
} |
| 257 |
if (ext2_len > 0 && ext2_len <= 65535) |
| 258 |
{ |
| 259 |
if (ext2_ptr + ext2_len >= disk->endbuf) |
| 260 |
{ |
| 261 |
errno = EBADMSG; |
| 262 |
goto error; /* zzip_mem_entry_free(item); return 0; */ |
| 263 |
} else { |
| 264 |
void *mem = malloc(ext2_len); |
| 265 |
if (! mem) goto error; /* errno = ENOMEM */ |
| 266 |
item->zz_ext[2] = mem; |
| 267 |
item->zz_extlen[2] = ext2_len; |
| 268 |
memcpy(mem, ext2_ptr, ext2_len); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
{ |
| 273 |
/* override sizes/offsets with zip64 values for largefile support */ |
| 274 |
struct zzip_extra_zip64 *block = (struct zzip_extra_zip64 *) |
| 275 |
zzip_mem_entry_find_extra_block(item, ZZIP_EXTRA_ZIP64_MAGIC, sizeof(struct zzip_extra_zip64)); |
| 276 |
if (block) |
| 277 |
{ |
| 278 |
item->zz_usize = zzip_extra_zip64_usize(block); |
| 279 |
item->zz_csize = zzip_extra_zip64_csize(block); |
| 280 |
item->zz_offset = zzip_extra_zip64_offset(block); |
| 281 |
item->zz_diskstart = zzip_extra_zip64_diskstart(block); |
| 282 |
} |
| 283 |
} |
| 284 |
/* NOTE: |
| 285 |
* All information from the central directory entry is now in memory. |
| 286 |
* Effectivly that allows us to modify it and write it back to disk. |
| 287 |
*/ |
| 288 |
return item; |
| 289 |
____; |
| 290 |
error: |
| 291 |
zzip_mem_entry_free(item); |
| 292 |
return 0; |
| 293 |
____; |
| 294 |
} |
| 295 |
|
| 296 |
/** => zzip_mem_entry_find_extra_block. |
| 297 |
* |
| 298 |
* Note that for this function only the block_header is asserted |
| 299 |
* to be completely in memory, so the returned pointer should be checked. |
| 300 |
*/ |
| 301 |
ZZIP_EXTRA_BLOCK * |
| 302 |
zzip_mem_entry_extra_block(ZZIP_MEM_ENTRY * entry, short datatype) |
| 303 |
{ |
| 304 |
return zzip_mem_entry_find_extra_block(entry, datatype, 16); |
| 305 |
} |
| 306 |
|
| 307 |
/** get extra block. |
| 308 |
* This function finds an extra block for the given datatype code. |
| 309 |
* The returned EXTRA_BLOCK is still in disk-encoding but |
| 310 |
* already a pointer into an allocated heap space block. |
| 311 |
* |
| 312 |
* The second argument of this function ensures that the |
| 313 |
* complete datasize is in memory. |
| 314 |
*/ |
| 315 |
ZZIP_EXTRA_BLOCK * |
| 316 |
zzip_mem_entry_find_extra_block(ZZIP_MEM_ENTRY * entry, short datatype, zzip_size_t blocksize) |
| 317 |
{ |
| 318 |
int i = 2; |
| 319 |
while (1) |
| 320 |
{ |
| 321 |
char* ext = (char*)( entry->zz_ext[i] ); |
| 322 |
char* ext_end = ext + entry->zz_extlen[i]; |
| 323 |
if (ext) |
| 324 |
{ |
| 325 |
/* |
| 326 |
* Make sure that |
| 327 |
* 1) the extra block header |
| 328 |
* AND |
| 329 |
* 2) the block we're looking for |
| 330 |
* fit into the extra block! |
| 331 |
*/ |
| 332 |
while (ext + zzip_extra_block_headerlength + blocksize <= ext_end) |
| 333 |
{ |
| 334 |
if (datatype == zzip_extra_block_get_datatype(ext)) |
| 335 |
{ |
| 336 |
if (blocksize <= zzip_extra_block_get_datasize(ext) + zzip_extra_block_headerlength) |
| 337 |
{ |
| 338 |
return ((ZZIP_EXTRA_BLOCK*) ext); |
| 339 |
} |
| 340 |
} |
| 341 |
/* skip to start of next extra_block */ |
| 342 |
___ zzip_size_t datasize = zzip_extra_block_get_datasize(ext); |
| 343 |
ext += zzip_extra_block_headerlength; |
| 344 |
ext += datasize; |
| 345 |
____; |
| 346 |
} |
| 347 |
} |
| 348 |
if (! i) |
| 349 |
return 0; |
| 350 |
i--; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/** => zzip_mem_disk_close |
| 355 |
* This function ends usage of a file entry in a disk. |
| 356 |
*/ |
| 357 |
void |
| 358 |
zzip_mem_entry_free(ZZIP_MEM_ENTRY * _zzip_restrict item) |
| 359 |
{ |
| 360 |
if (item) |
| 361 |
{ |
| 362 |
/* *INDENT-OFF* */ |
| 363 |
if (item->zz_ext[0]) free (item->zz_ext[0]); |
| 364 |
if (item->zz_ext[1]) free (item->zz_ext[1]); |
| 365 |
if (item->zz_ext[2]) free (item->zz_ext[2]); |
| 366 |
if (item->zz_comment) free (item->zz_comment); |
| 367 |
if (item->zz_name) free (item->zz_name); |
| 368 |
free (item); |
| 369 |
/* *INDENT-ON* */ |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/** => zzip_mem_disk_close |
| 374 |
* This function will trigger an underlying disk_close |
| 375 |
*/ |
| 376 |
void |
| 377 |
zzip_mem_disk_unload(ZZIP_MEM_DISK * dir) |
| 378 |
{ |
| 379 |
ZZIP_MEM_ENTRY *item = dir->list; |
| 380 |
while (item) |
| 381 |
{ |
| 382 |
ZZIP_MEM_ENTRY *next = item->zz_next; |
| 383 |
zzip_mem_entry_free(item); |
| 384 |
item = next; |
| 385 |
} |
| 386 |
dir->list = dir->last = 0; |
| 387 |
zzip_disk_close(dir->disk); |
| 388 |
dir->disk = 0; |
| 389 |
} |
| 390 |
|
| 391 |
/** end usage. |
| 392 |
* This function closes the dir and disk handles. |
| 393 |
*/ |
| 394 |
void |
| 395 |
zzip_mem_disk_close(ZZIP_MEM_DISK * _zzip_restrict dir) |
| 396 |
{ |
| 397 |
if (dir) |
| 398 |
{ |
| 399 |
zzip_mem_disk_unload(dir); |
| 400 |
zzip_disk_close(dir->disk); |
| 401 |
free(dir); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
#if 0 |
| 406 |
static void |
| 407 |
foo(short zz_datatype) |
| 408 |
{ |
| 409 |
/* Header IDs of 0 through 31 are reserved for use by PKWARE.(APPNOTE.TXT) */ |
| 410 |
switch (zz_datatype) |
| 411 |
{ |
| 412 |
/* *INDENT-OFF* */ |
| 413 |
case 0x0001: /* ZIP64 extended information extra field */ |
| 414 |
case 0x0007: /* AV Info */ |
| 415 |
case 0x0008: /* Reserved for future Unicode file name data (PFS) */ |
| 416 |
case 0x0009: /* OS/2 */ |
| 417 |
case 0x000a: /* NTFS */ |
| 418 |
case 0x000c: /* OpenVMS */ |
| 419 |
case 0x000d: /* Unix */ |
| 420 |
case 0x000e: /* Reserved for file stream and fork descriptors */ |
| 421 |
case 0x000f: /* Patch Descriptor */ |
| 422 |
case 0x0014: /* PKCS#7 Store for X.509 Certificates */ |
| 423 |
case 0x0015: /* X.509 Certificate ID and Signature for file */ |
| 424 |
case 0x0016: /* X.509 Certificate ID for Central Directory */ |
| 425 |
case 0x0017: /* Strong Encryption Header */ |
| 426 |
case 0x0018: /* Record Management Controls */ |
| 427 |
case 0x0019: /* PKCS#7 Encryption Recipient Certificate List */ |
| 428 |
/* ......................................................... */ |
| 429 |
case 0x0065: /* IBM S/390, AS/400 attributes - uncompressed */ |
| 430 |
case 0x0066: /* Reserved for IBM S/390, AS/400 attr - compressed */ |
| 431 |
case 0x07c8: /* Macintosh */ |
| 432 |
case 0x2605: /* ZipIt Macintosh */ |
| 433 |
case 0x2705: /* ZipIt Macintosh 1.3.5+ */ |
| 434 |
case 0x2805: /* ZipIt Macintosh 1.3.5+ */ |
| 435 |
case 0x334d: /* Info-ZIP Macintosh */ |
| 436 |
case 0x4341: /* Acorn/SparkFS */ |
| 437 |
case 0x4453: /* Windows NT security descriptor (binary ACL) */ |
| 438 |
case 0x4704: /* VM/CMS */ |
| 439 |
case 0x470f: /* MVS */ |
| 440 |
case 0x4b46: /* FWKCS MD5 (see below) */ |
| 441 |
case 0x4c41: /* OS/2 access control list (text ACL) */ |
| 442 |
case 0x4d49: /* Info-ZIP OpenVMS */ |
| 443 |
case 0x4f4c: /* Xceed original location extra field */ |
| 444 |
case 0x5356: /* AOS/VS (ACL) */ |
| 445 |
case 0x5455: /* extended timestamp */ |
| 446 |
case 0x554e: /* Xceed unicode extra field */ |
| 447 |
case 0x5855: /* Info-ZIP Unix (original, also OS/2, NT, etc) */ |
| 448 |
case 0x6542: /* BeOS/BeBox */ |
| 449 |
case 0x756e: /* ASi Unix */ |
| 450 |
case 0x7855: /* Info-ZIP Unix (new) */ |
| 451 |
case 0xfd4a: /* SMS/QDOS */ |
| 452 |
/* *INDENT-ON* */ |
| 453 |
} |
| 454 |
} |
| 455 |
#endif |
| 456 |
|
| 457 |
/** search entries. |
| 458 |
* This function walks through the zip directory looking for a file. |
| 459 |
*/ |
| 460 |
ZZIP_MEM_ENTRY * |
| 461 |
zzip_mem_disk_findfile(ZZIP_MEM_DISK * dir, |
| 462 |
char *filename, ZZIP_MEM_ENTRY * after, |
| 463 |
zzip_strcmp_fn_t compare) |
| 464 |
{ |
| 465 |
ZZIP_MEM_ENTRY *entry = (! after ? dir->list : after->zz_next); |
| 466 |
if (! compare) |
| 467 |
compare = (zzip_strcmp_fn_t) (strcmp); |
| 468 |
for (; entry; entry = entry->zz_next) |
| 469 |
{ |
| 470 |
if (! compare(filename, entry->zz_name)) |
| 471 |
{ |
| 472 |
return entry; |
| 473 |
} |
| 474 |
} |
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 478 |
/* => zzip_mem_disk_findfile |
| 479 |
* This function uses an fnmatch-like comparator to find files. |
| 480 |
*/ |
| 481 |
ZZIP_MEM_ENTRY * |
| 482 |
zzip_mem_disk_findmatch(ZZIP_MEM_DISK * dir, |
| 483 |
char *filespec, ZZIP_MEM_ENTRY * after, |
| 484 |
zzip_fnmatch_fn_t compare, int flags) |
| 485 |
{ |
| 486 |
ZZIP_MEM_ENTRY *entry = (! after ? dir->list : after->zz_next); |
| 487 |
if (! compare) |
| 488 |
compare = (zzip_fnmatch_fn_t) _zzip_fnmatch; |
| 489 |
for (; entry; entry = entry->zz_next) |
| 490 |
{ |
| 491 |
if (! compare(filespec, entry->zz_name, flags)) |
| 492 |
{ |
| 493 |
return entry; |
| 494 |
} |
| 495 |
} |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
/** start usage. |
| 500 |
* This function opens a referenced file entry from a openend disk. |
| 501 |
*/ |
| 502 |
zzip__new__ ZZIP_MEM_DISK_FILE * |
| 503 |
zzip_mem_entry_fopen(ZZIP_MEM_DISK * dir, ZZIP_MEM_ENTRY * entry) |
| 504 |
{ |
| 505 |
/* keep this in sync with zzip_disk_entry_fopen */ |
| 506 |
ZZIP_DISK_FILE *file = malloc(sizeof(ZZIP_MEM_DISK_FILE)); |
| 507 |
if (! file) |
| 508 |
return file; |
| 509 |
file->buffer = dir->disk->buffer; |
| 510 |
file->endbuf = dir->disk->endbuf; |
| 511 |
file->avail = zzip_mem_entry_usize(entry); |
| 512 |
|
| 513 |
if (! file->avail || zzip_mem_entry_data_stored(entry)) |
| 514 |
{ file->stored = zzip_mem_entry_to_data (entry); return file; } |
| 515 |
|
| 516 |
file->stored = 0; |
| 517 |
file->zlib.opaque = 0; |
| 518 |
file->zlib.zalloc = Z_NULL; |
| 519 |
file->zlib.zfree = Z_NULL; |
| 520 |
file->zlib.avail_in = zzip_mem_entry_csize(entry); |
| 521 |
file->zlib.next_in = zzip_mem_entry_to_data(entry); |
| 522 |
|
| 523 |
debug2("compressed size %i", (int) file->zlib.avail_in); |
| 524 |
if (file->zlib.next_in + file->zlib.avail_in >= file->endbuf) |
| 525 |
goto error; |
| 526 |
if (file->zlib.next_in < file->buffer) |
| 527 |
goto error; |
| 528 |
|
| 529 |
if (! zzip_mem_entry_data_deflated(entry) || |
| 530 |
inflateInit2(&file->zlib, -MAX_WBITS) != Z_OK) |
| 531 |
{ free (file); return 0; } |
| 532 |
|
| 533 |
return file; |
| 534 |
error: |
| 535 |
errno = EBADMSG; |
| 536 |
return NULL; |
| 537 |
} |
| 538 |
|
| 539 |
/** => zzip_mem_disk_open |
| 540 |
* This function opens a file by name from an openend disk. |
| 541 |
*/ |
| 542 |
zzip__new__ ZZIP_MEM_DISK_FILE * |
| 543 |
zzip_mem_disk_fopen(ZZIP_MEM_DISK * dir, char *filename) |
| 544 |
{ |
| 545 |
ZZIP_MEM_ENTRY *entry = zzip_mem_disk_findfile(dir, filename, 0, 0); |
| 546 |
if (! entry) |
| 547 |
return 0; |
| 548 |
else |
| 549 |
return zzip_mem_entry_fopen(dir, entry); |
| 550 |
} |
| 551 |
|
| 552 |
/** get data |
| 553 |
* This function mimics the fread(2) behaviour. |
| 554 |
*/ |
| 555 |
_zzip_size_t |
| 556 |
zzip_mem_disk_fread(void *ptr, _zzip_size_t size, _zzip_size_t nmemb, |
| 557 |
ZZIP_MEM_DISK_FILE * file) |
| 558 |
{ |
| 559 |
return zzip_disk_fread(ptr, size, nmemb, file); |
| 560 |
} |
| 561 |
|
| 562 |
/** close disk. |
| 563 |
* This function mimics the fclose(2) behaviour. |
| 564 |
*/ |
| 565 |
int |
| 566 |
zzip_mem_disk_fclose(ZZIP_MEM_DISK_FILE * file) |
| 567 |
{ |
| 568 |
return zzip_disk_fclose(file); |
| 569 |
} |
| 570 |
|
| 571 |
/** check disk. |
| 572 |
* This function mimics the feof(2) behaviour. |
| 573 |
*/ |
| 574 |
int |
| 575 |
zzip_mem_disk_feof(ZZIP_MEM_DISK_FILE * file) |
| 576 |
{ |
| 577 |
return zzip_disk_feof(file); |
| 578 |
} |
| 579 |
|
| 580 |
/** helper. |
| 581 |
* This function converts a zip dostime of an entry to unix time_t |
| 582 |
*/ |
| 583 |
long |
| 584 |
zzip_disk_entry_get_mktime(ZZIP_DISK_ENTRY * entry) |
| 585 |
{ |
| 586 |
uint16_t dostime = ZZIP_GET16(entry->z_dostime.time); |
| 587 |
uint16_t dosdate = ZZIP_GET16(entry->z_dostime.date); |
| 588 |
struct tm date; |
| 589 |
date.tm_sec = (dostime) & 0x1F; /* bits 0..4 */ |
| 590 |
date.tm_min = (dostime >> 5) & 0x3F; /* bits 5..10 */ |
| 591 |
date.tm_hour = (dostime >> 11); /* bits 11..15 */ |
| 592 |
date.tm_mday = (dosdate) & 0x1F; /* bits 16..20 */ |
| 593 |
date.tm_mon = (dosdate >> 5) & 0xF; /* bits 21..24 */ |
| 594 |
date.tm_year = (dosdate >> 9) + 80; /* bits 25..31 */ |
| 595 |
return mktime(&date); /* well, unix has that function... */ |
| 596 |
} |