| 1 |
#ifndef __ZZIP_MEMDISK_H |
| 2 |
#define __ZZIP_MEMDISK_H |
| 3 |
|
| 4 |
/* |
| 5 |
* NOTE: this is part of libzzipmmapped (i.e. it is not libzzip). |
| 6 |
* |
| 7 |
* Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL) |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <zzip/types.h> |
| 11 |
#include <zzip/mmapped.h> |
| 12 |
#include <zzip/__hints.h> |
| 13 |
|
| 14 |
#ifdef __cplusplus |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
typedef struct _zzip_mem_disk ZZIP_MEM_DISK; |
| 19 |
typedef struct _zzip_mem_entry ZZIP_MEM_ENTRY; |
| 20 |
|
| 21 |
struct _zzip_mem_disk { |
| 22 |
ZZIP_DISK* disk; |
| 23 |
ZZIP_MEM_ENTRY* list; |
| 24 |
ZZIP_MEM_ENTRY* last; |
| 25 |
}; |
| 26 |
|
| 27 |
#ifndef zzip_mem_disk_extern |
| 28 |
#define zzip_mem_disk_extern |
| 29 |
#endif |
| 30 |
|
| 31 |
zzip_mem_disk_extern zzip__new__ ZZIP_MEM_DISK* |
| 32 |
zzip_mem_disk_new (void); |
| 33 |
zzip_mem_disk_extern zzip__new__ ZZIP_MEM_DISK* |
| 34 |
zzip_mem_disk_open (char* filename); |
| 35 |
zzip_mem_disk_extern zzip__new__ ZZIP_MEM_DISK* |
| 36 |
zzip_mem_disk_fdopen (int fd); |
| 37 |
zzip_mem_disk_extern zzip__new__ ZZIP_MEM_DISK * |
| 38 |
zzip_mem_disk_buffer(char *buffer, size_t buflen); |
| 39 |
zzip_mem_disk_extern void |
| 40 |
zzip_mem_disk_close (ZZIP_MEM_DISK* _zzip_restrict dir); |
| 41 |
|
| 42 |
zzip_mem_disk_extern long |
| 43 |
zzip_mem_disk_load (ZZIP_MEM_DISK* dir, ZZIP_DISK* disk); |
| 44 |
zzip_mem_disk_extern void |
| 45 |
zzip_mem_disk_unload (ZZIP_MEM_DISK* dir); |
| 46 |
ZZIP_EXTRA_BLOCK* |
| 47 |
zzip_mem_entry_extra_block (ZZIP_MEM_ENTRY* entry, short datatype) ZZIP_GNUC_DEPRECATED; |
| 48 |
ZZIP_EXTRA_BLOCK* |
| 49 |
zzip_mem_entry_find_extra_block (ZZIP_MEM_ENTRY* entry, short datatype, zzip_size_t blocksize); |
| 50 |
|
| 51 |
#ifdef USE_INLINE |
| 52 |
_zzip_inline ZZIP_DISK* zzip_disk (ZZIP_MEM_DISK* dir) { return dir->disk; } |
| 53 |
#else |
| 54 |
#define zzip_disk(_X_) ((_X_)->disk) |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
/* these functions are much faster than the orgiinal zzip_disk_ functions */ |
| 59 |
|
| 60 |
/* zzip_mem_entry <is similar to> zzip_disk_entry */ |
| 61 |
struct _zzip_mem_entry { |
| 62 |
struct _zzip_mem_entry* zz_next; |
| 63 |
char* zz_name; /* zero-terminated (from "filename") */ |
| 64 |
zzip_byte_t* zz_data; /* compressed content start (mmap addr) */ |
| 65 |
int zz_flags; /* (from "z_flags") */ |
| 66 |
int zz_compr; /* (from "z_compr") */ |
| 67 |
long zz_mktime; /* (from "z_dostime") */ |
| 68 |
long zz_crc32; /* (from "z_crc32") */ |
| 69 |
zzip_off_t zz_csize; /* (from "z_csize") overridden by zip64 */ |
| 70 |
zzip_off_t zz_usize; /* (from "z_usize") overridden by zip64 */ |
| 71 |
zzip_off_t zz_offset; /* (from "z_offset") overridden by zip64 */ |
| 72 |
int zz_diskstart; /* (from "z_diskstart") rridden by zip64 */ |
| 73 |
int zz_filetype; /* (from "z_filetype") */ |
| 74 |
char* zz_comment; /* zero-terminated (from "comment") */ |
| 75 |
ZZIP_EXTRA_BLOCK* zz_ext[3]; /* terminated by null in z_datatype */ |
| 76 |
zzip_size_t zz_extlen[3]; /* the extra blocks are NOT converted */ |
| 77 |
}; |
| 78 |
|
| 79 |
#define _zzip_mem_disk_findfirst(_d_) (!(_d_) ? 0 : ((_d_)->list)) |
| 80 |
#define _zzip_mem_disk_findnext(_d_,_e_) (!(_e_) ? (!(_d_) ? 0 : ((_d_)->list)) : ((_e_)->zz_next)) |
| 81 |
#define _zzip_mem_entry_findnext(_e_) (!(_e_) ? 0 : ((_e_)->zz_next)) |
| 82 |
|
| 83 |
#ifndef USE_INLINE |
| 84 |
#define zzip_mem_disk_findfirst _zzip_mem_disk_findfirst |
| 85 |
#define zzip_mem_disk_findnext _zzip_mem_disk_findnext |
| 86 |
#define zzip_mem_entry_findnext _zzip_mem_entry_findnext |
| 87 |
#else |
| 88 |
|
| 89 |
_zzip_inline ZZIP_MEM_ENTRY* |
| 90 |
zzip_mem_disk_findfirst(ZZIP_MEM_DISK* dir) { |
| 91 |
if (! dir) return 0; |
| 92 |
return _zzip_mem_disk_findfirst(dir); } |
| 93 |
_zzip_inline ZZIP_MEM_ENTRY* |
| 94 |
zzip_mem_disk_findnext(ZZIP_MEM_DISK* dir, ZZIP_MEM_ENTRY* entry) { |
| 95 |
if (! dir) return 0; |
| 96 |
return _zzip_mem_disk_findnext(dir, entry); } |
| 97 |
_zzip_inline ZZIP_MEM_ENTRY* |
| 98 |
zzip_mem_entry_findnext(ZZIP_MEM_ENTRY* entry) { |
| 99 |
if (! entry) return 0; |
| 100 |
return _zzip_mem_entry_findnext(entry); } |
| 101 |
#endif |
| 102 |
|
| 103 |
#define _zzip_mem_entry_to_name(_e_) (!(_e_) ? 0 : ((_e_)->zz_name)) |
| 104 |
#define _zzip_mem_entry_to_comment(_e_) (!(_e_) ? 0 : ((_e_)->zz_comment)) |
| 105 |
#define _zzip_mem_entry_strdup_name(_e_) (!(_e_) ? 0 : (strdup((_e_)->zz_name))) |
| 106 |
#define _zzip_mem_entry_to_data(_e_) (!(_e_) ? 0 : ((_e_)->zz_data)) |
| 107 |
|
| 108 |
#ifndef USE_INLINE |
| 109 |
#define zzip_mem_entry_to_name _zzip_mem_entry_to_name |
| 110 |
#define zzip_mem_entry_to_comment _zzip_mem_entry_to_comment |
| 111 |
#define zzip_mem_entry_strdup_name _zzip_mem_entry_strdup_name |
| 112 |
#define zzip_mem_entry_to_data _zzip_mem_entry_to_data |
| 113 |
#else |
| 114 |
_zzip_inline char* |
| 115 |
zzip_mem_entry_to_name(ZZIP_MEM_ENTRY* entry) { |
| 116 |
if (! entry) return 0; |
| 117 |
return _zzip_mem_entry_to_name(entry); } |
| 118 |
_zzip_inline char* |
| 119 |
zzip_mem_entry_to_comment(ZZIP_MEM_ENTRY* entry) { |
| 120 |
if (! entry) return 0; |
| 121 |
return _zzip_mem_entry_to_comment(entry); } |
| 122 |
zzip__new__ |
| 123 |
_zzip_inline char* |
| 124 |
zzip_mem_entry_strdup_name(ZZIP_MEM_ENTRY* entry) { |
| 125 |
if (! entry) return 0; |
| 126 |
return _zzip_mem_entry_strdup_name(entry); } |
| 127 |
_zzip_inline zzip_byte_t* |
| 128 |
zzip_mem_entry_to_data(ZZIP_MEM_ENTRY* entry) { |
| 129 |
if (! entry) return 0; |
| 130 |
return _zzip_mem_entry_to_data(entry); } |
| 131 |
#endif |
| 132 |
|
| 133 |
ZZIP_MEM_ENTRY* |
| 134 |
zzip_mem_disk_findfile(ZZIP_MEM_DISK* dir, |
| 135 |
char* filename, ZZIP_MEM_ENTRY* after, |
| 136 |
zzip_strcmp_fn_t compare); |
| 137 |
|
| 138 |
ZZIP_MEM_ENTRY* |
| 139 |
zzip_mem_disk_findmatch(ZZIP_MEM_DISK* dir, |
| 140 |
char* filespec, ZZIP_MEM_ENTRY* after, |
| 141 |
zzip_fnmatch_fn_t compare, int flags); |
| 142 |
|
| 143 |
/* named access -------------------------------------------------------- */ |
| 144 |
#define zzip_mem_entry_usize(_e_) ((_e_)->zz_usize) |
| 145 |
#define zzip_mem_entry_csize(_e_) ((_e_)->zz_csize) |
| 146 |
#define zzip_mem_entry_data_encrypted(_e_) ZZIP_IS_ENCRYPTED((_e_)->zz_flags) |
| 147 |
#define zzip_mem_entry_data_streamed(_e_) ZZIP_IS_STREAMED((_e_)->zz_flags) |
| 148 |
#define zzip_mem_entry_data_comprlevel(_e_) ((_e_)->zz_compr) |
| 149 |
#define zzip_mem_entry_data_stored(_e_) ((_e_)->zz_compr == ZZIP_IS_STORED) |
| 150 |
#define zzip_mem_entry_data_deflated(_e_) ((_e_)->zz_compr == ZZIP_IS_DEFLATED) |
| 151 |
|
| 152 |
/* zzip_mem_disk_file -------------------------------------------------- */ |
| 153 |
|
| 154 |
/* since only the header data is being cached, all the real data |
| 155 |
* operations are actually the same as in mmapped.h - just fopen makes |
| 156 |
* access to the header data in memory instead of the zip archive. |
| 157 |
*/ |
| 158 |
|
| 159 |
typedef ZZIP_DISK_FILE ZZIP_MEM_DISK_FILE; |
| 160 |
|
| 161 |
zzip__new__ ZZIP_MEM_DISK_FILE* |
| 162 |
zzip_mem_entry_fopen (ZZIP_MEM_DISK* dir, ZZIP_MEM_ENTRY* entry); |
| 163 |
zzip__new__ ZZIP_MEM_DISK_FILE* |
| 164 |
zzip_mem_disk_fopen (ZZIP_MEM_DISK* dir, char* filename); |
| 165 |
_zzip_size_t |
| 166 |
zzip_mem_disk_fread (void* ptr, _zzip_size_t size, _zzip_size_t nmemb, |
| 167 |
ZZIP_MEM_DISK_FILE* file); |
| 168 |
int |
| 169 |
zzip_mem_disk_fclose (ZZIP_MEM_DISK_FILE* file); |
| 170 |
int |
| 171 |
zzip_mem_disk_feof (ZZIP_MEM_DISK_FILE* file); |
| 172 |
|
| 173 |
/* convert dostime of entry to unix time_t */ |
| 174 |
long zzip_disk_entry_get_mktime(ZZIP_DISK_ENTRY* entry); |
| 175 |
|
| 176 |
#ifdef __cplusplus |
| 177 |
} |
| 178 |
#endif |
| 179 |
#endif |