| 1 |
/* |
| 2 |
* Author: |
| 3 |
* Guido Draheim <[email protected]> |
| 4 |
* |
| 5 |
* Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL) |
| 6 |
* |
| 7 |
* The information was taken from appnote-981119-iz.zip |
| 8 |
* at http://www.freesoftware.com/pub/infozip/doc/ |
| 9 |
* which in turn is based on PKWARE's appnote.txt |
| 10 |
* (newer link: ftp://ftp.info-zip.org/pub/infozip/doc/) |
| 11 |
*/ |
| 12 |
#ifndef _ZZIP_FORMAT_H /* zzipformat.h */ |
| 13 |
#define _ZZIP_FORMAT_H |
| 14 |
|
| 15 |
#include <zzip/types.h> |
| 16 |
#include <zzip/__hints.h> |
| 17 |
/* we have ICO C 9X types defined */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Overall zipfile format |
| 21 |
* [local file header + file data stream + checksum descriptor] ... |
| 22 |
* ... [disk central directory] [disk trailer record] |
| 23 |
*/ |
| 24 |
|
| 25 |
# ifdef _MSC_VER |
| 26 |
# pragma pack(push, 1) |
| 27 |
# endif |
| 28 |
|
| 29 |
struct zzip_version |
| 30 |
{ |
| 31 |
zzip_byte_t version[1]; |
| 32 |
zzip_byte_t ostype[1]; |
| 33 |
} ZZIP_GNUC_PACKED; |
| 34 |
|
| 35 |
struct zzip_dostime |
| 36 |
{ |
| 37 |
zzip_byte_t time[2]; |
| 38 |
zzip_byte_t date[2]; |
| 39 |
} ZZIP_GNUC_PACKED; |
| 40 |
|
| 41 |
#ifdef ZZIP_NEED_PACKED |
| 42 |
/* if your compiler does interesting things about struct packing... */ |
| 43 |
typedef zzip_byte_t zzip_version_t[2]; |
| 44 |
typedef zzip_byte_t zzip_dostime_t[4]; |
| 45 |
#else |
| 46 |
typedef struct zzip_version zzip_version_t; |
| 47 |
typedef struct zzip_dostime zzip_dostime_t; |
| 48 |
#endif |
| 49 |
|
| 50 |
#define ZZIP_CHECKMAGIC(__p,__A,__B,__C,__D) \ |
| 51 |
( (((zzip_byte_t*)(__p))[0]==(__A)) && \ |
| 52 |
(((zzip_byte_t*)(__p))[1]==(__B)) && \ |
| 53 |
(((zzip_byte_t*)(__p))[2]==(__C)) && \ |
| 54 |
(((zzip_byte_t*)(__p))[3]==(__D)) ) |
| 55 |
#define ZZIP_CHECK(__p,__A,__B) \ |
| 56 |
( (((zzip_byte_t*)(__p))[0]==(__A)) && \ |
| 57 |
(((zzip_byte_t*)(__p))[1]==(__B)) ) |
| 58 |
|
| 59 |
/* A. Local file header */ |
| 60 |
struct zzip_file_header |
| 61 |
{ |
| 62 |
# define ZZIP_FILE_HEADER_MAGIC 0x04034b50 |
| 63 |
# define ZZIP_FILE_HEADER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\3','\4') |
| 64 |
zzip_byte_t z_magic[4]; /* local file header signature (0x04034b50) */ |
| 65 |
zzip_version_t z_extract; /* version needed to extract */ |
| 66 |
zzip_byte_t z_flags[2]; /* general purpose bit flag */ |
| 67 |
zzip_byte_t z_compr[2]; /* compression method */ |
| 68 |
zzip_dostime_t z_dostime; /* last mod file time (dos format) */ |
| 69 |
zzip_byte_t z_crc32[4]; /* crc-32 */ |
| 70 |
zzip_byte_t z_csize[4]; /* compressed size */ |
| 71 |
zzip_byte_t z_usize[4]; /* uncompressed size */ |
| 72 |
zzip_byte_t z_namlen[2]; /* filename length (null if stdin) */ |
| 73 |
zzip_byte_t z_extras[2]; /* extra field length */ |
| 74 |
/* followed by filename (of variable size) */ |
| 75 |
/* followed by extra field (of variable size) */ |
| 76 |
} ZZIP_GNUC_PACKED; |
| 77 |
#define zzip_file_header_headerlength (4U+2U+2U+2U+4U+4U+4U+4U+2U+2U) |
| 78 |
|
| 79 |
/* B. data descriptor |
| 80 |
* the data descriptor exists only if bit 3 of z_flags is set. It is byte aligned |
| 81 |
* and immediately follows the last byte of compressed data. It is only used if |
| 82 |
* the output media of the compressor was not seekable, eg. standard output. |
| 83 |
*/ |
| 84 |
struct zzip_file_trailer |
| 85 |
{ |
| 86 |
# define ZZIP_FILE_TRAILER_MAGIC 0x08074B50 |
| 87 |
# define ZZIP_FILE_TRAILER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\7','\8') |
| 88 |
zzip_byte_t z_magic[4]; /* data descriptor signature (0x08074b50) */ |
| 89 |
zzip_byte_t z_crc32[4]; /* crc-32 */ |
| 90 |
zzip_byte_t z_csize[4]; /* compressed size */ |
| 91 |
zzip_byte_t z_usize[4]; /* uncompressed size */ |
| 92 |
} ZZIP_GNUC_PACKED; |
| 93 |
#define zzip_file_trailer_headerlength (4U+4U+4U+4U) |
| 94 |
|
| 95 |
/* C. central directory structure: |
| 96 |
[file header] . . . end of central dir record |
| 97 |
*/ |
| 98 |
|
| 99 |
/* directory file header |
| 100 |
* - a single entry including filename, extras and comment may not exceed 64k. |
| 101 |
*/ |
| 102 |
|
| 103 |
struct zzip_disk_entry |
| 104 |
{ |
| 105 |
# define ZZIP_DISK_ENTRY_MAGIC 0x02014b50 |
| 106 |
# define ZZIP_DISK_ENTRY_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\1','\2') |
| 107 |
zzip_byte_t z_magic[4]; /* central file header signature (0x02014b50) */ |
| 108 |
zzip_version_t z_encoder; /* version made by */ |
| 109 |
zzip_version_t z_extract; /* version need to extract */ |
| 110 |
zzip_byte_t z_flags[2]; /* general purpose bit flag */ |
| 111 |
zzip_byte_t z_compr[2]; /* compression method */ |
| 112 |
zzip_dostime_t z_dostime; /* last mod file time&date (dos format) */ |
| 113 |
zzip_byte_t z_crc32[4]; /* crc-32 */ |
| 114 |
zzip_byte_t z_csize[4]; /* compressed size */ |
| 115 |
zzip_byte_t z_usize[4]; /* uncompressed size */ |
| 116 |
zzip_byte_t z_namlen[2]; /* filename length (null if stdin) */ |
| 117 |
zzip_byte_t z_extras[2]; /* extra field length */ |
| 118 |
zzip_byte_t z_comment[2]; /* file comment length */ |
| 119 |
zzip_byte_t z_diskstart[2]; /* disk number of start (if spanning zip over multiple disks) */ |
| 120 |
zzip_byte_t z_filetype[2]; /* internal file attributes, bit0 = ascii */ |
| 121 |
zzip_byte_t z_filemode[4]; /* extrnal file attributes, eg. msdos attrib byte */ |
| 122 |
zzip_byte_t z_offset[4]; /* relative offset of local file header, seekval if singledisk */ |
| 123 |
/* followed by filename (of variable size) */ |
| 124 |
/* followed by extra field (of variable size) */ |
| 125 |
/* followed by file comment (of variable size) */ |
| 126 |
} ZZIP_GNUC_PACKED; |
| 127 |
#define zzip_disk_entry_headerlength (4U+2U+2U+2U+2U+4U+4U+4U+4U+2U+2U+2U+2U+2U+4U+4U) |
| 128 |
|
| 129 |
|
| 130 |
struct zzip_root_dirent |
| 131 |
{ /* the old name of the structure above */ |
| 132 |
# define ZZIP_ROOT_DIRENT_MAGIC 0x02014b50 |
| 133 |
# define ZZIP_ROOT_DIRENT_CHECKMAGIC(__p) ZZIP_DISK_ENTRY_CHECKMAGIC(__p) |
| 134 |
zzip_byte_t z_magic[4]; |
| 135 |
zzip_version_t z_encoder; |
| 136 |
zzip_version_t z_extract; |
| 137 |
zzip_byte_t z_flags[2]; |
| 138 |
zzip_byte_t z_compr[2]; |
| 139 |
zzip_dostime_t z_dostime; |
| 140 |
zzip_byte_t z_crc32[4]; |
| 141 |
zzip_byte_t z_csize[4]; |
| 142 |
zzip_byte_t z_usize[4]; |
| 143 |
zzip_byte_t z_namlen[2]; |
| 144 |
zzip_byte_t z_extras[2]; |
| 145 |
zzip_byte_t z_comment[2]; |
| 146 |
zzip_byte_t z_diskstart[2]; |
| 147 |
zzip_byte_t z_filetype[2]; |
| 148 |
zzip_byte_t z_filemode[4]; |
| 149 |
zzip_byte_t z_off[4]; |
| 150 |
} ZZIP_GNUC_PACKED; |
| 151 |
|
| 152 |
|
| 153 |
/* end of central dir record */ |
| 154 |
struct zzip_disk_trailer |
| 155 |
{ |
| 156 |
# define ZZIP_DISK_TRAILER_MAGIC 0x06054b50 |
| 157 |
# define ZZIP_DISK_TRAILER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\5','\6') |
| 158 |
zzip_byte_t z_magic[4]; /* end of central dir signature (0x06054b50) */ |
| 159 |
zzip_byte_t z_disk[2]; /* number of this disk */ |
| 160 |
zzip_byte_t z_finaldisk[2]; /* number of the disk with the start of the central dir */ |
| 161 |
zzip_byte_t z_entries[2]; /* total number of entries in the central dir on this disk */ |
| 162 |
zzip_byte_t z_finalentries[2]; /* total number of entries in the central dir */ |
| 163 |
zzip_byte_t z_rootsize[4]; /* size of the central directory */ |
| 164 |
zzip_byte_t z_rootseek[4]; /* offset of start of central directory with respect to * |
| 165 |
* the starting disk number */ |
| 166 |
zzip_byte_t z_comment[2]; /* zipfile comment length */ |
| 167 |
/* followed by zipfile comment (of variable size) */ |
| 168 |
} ZZIP_GNUC_PACKED; |
| 169 |
#define zzip_disk_trailer_headerlength (4U+2U+2U+2U+2U+4U+4U+2U) |
| 170 |
|
| 171 |
/* extra field should be type + size + data + type + size + data ... */ |
| 172 |
struct zzip_extra_block |
| 173 |
{ /* fetch.h macros do not need this struct */ |
| 174 |
zzip_byte_t z_datatype[2]; /* as input type - a mere <char*> is okay */ |
| 175 |
zzip_byte_t z_datasize[2]; /* being returned by xx_to_extras usually */ |
| 176 |
} ZZIP_GNUC_PACKED; |
| 177 |
#define zzip_extra_block_headerlength (2U+2U) |
| 178 |
|
| 179 |
/* Zip64 extras block */ |
| 180 |
struct zzip_extra_zip64 |
| 181 |
{ |
| 182 |
# define ZZIP_EXTRA_ZIP64_MAGIC 0x0001 |
| 183 |
# define ZZIP_EXTRA_ZIP64_CHECK(__p) ZZIP_CHECK(__p,'\0','\1') |
| 184 |
zzip_byte_t z_datatype[2]; /* extras signature 0x0001 */ |
| 185 |
zzip_byte_t z_datasize[2]; /* structure length 0x0010 */ |
| 186 |
zzip_byte_t z_usize[8]; /* original size */ |
| 187 |
zzip_byte_t z_csize[8]; /* compressed size */ |
| 188 |
zzip_byte_t z_offset[8]; /* offset from file header */ |
| 189 |
zzip_byte_t z_diskstart[4]; /* disk where the file starts */ |
| 190 |
} ZZIP_GNUC_PACKED; |
| 191 |
|
| 192 |
/* Zip64 end of central dir locator */ |
| 193 |
struct zzip_disk64_locator |
| 194 |
{ |
| 195 |
# define ZZIP_DISK64_LOCATOR_MAGIC 0x07064b50 |
| 196 |
# define ZZIP_DISK64_LOCATOR_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\6','\7') |
| 197 |
zzip_byte_t z_magic[4]; /* end of central dir signature (0x06054b50) */ |
| 198 |
zzip_byte_t z_rootdisk[4]; /* number of disk with the zip64 directory */ |
| 199 |
zzip_byte_t z_rootseek[8]; /* relative offset of the zip64 directory */ |
| 200 |
zzip_byte_t z_numdisks[4]; /* total numer of disks */ |
| 201 |
/* followed by zip64 extensible data sector (of variable size) */ |
| 202 |
} ZZIP_GNUC_PACKED; |
| 203 |
|
| 204 |
/* Zip64 end of central dir record */ |
| 205 |
struct zzip_disk64_trailer |
| 206 |
{ |
| 207 |
# define ZZIP_DISK64_TRAILER_MAGIC 0x06064b50 |
| 208 |
# define ZZIP_DISK64_TRAILER_CHECKMAGIC(__p) ZZIP_CHECKMAGIC(__p,'P','K','\6','\6') |
| 209 |
zzip_byte_t z_magic[4]; /* end of central dir signature (0x06054b50) */ |
| 210 |
zzip_byte_t z_size[8]; /* size of this central directory record */ |
| 211 |
zzip_version_t z_encoder; /* version made by */ |
| 212 |
zzip_version_t z_extract; /* version need to extract */ |
| 213 |
zzip_byte_t z_disk[4]; /* number of this disk */ |
| 214 |
zzip_byte_t z_finaldisk[4]; /* number of the disk with the start of the central dir */ |
| 215 |
zzip_byte_t z_entries[8]; /* total number of entries in the central dir on this disk */ |
| 216 |
zzip_byte_t z_finalentries[8]; /* total number of entries in the central dir */ |
| 217 |
zzip_byte_t z_rootsize[8]; /* size of the central directory */ |
| 218 |
zzip_byte_t z_rootseek[8]; /* offset of start of central directory with respect to * |
| 219 |
* the starting disk number */ |
| 220 |
/* followed by zip64 extensible data sector (of variable size) */ |
| 221 |
} ZZIP_GNUC_PACKED; |
| 222 |
#define zzip_disk64_trailer_headerlength (4U+8U+2U+2U+4U+4U+8U+8U+8U+8U) |
| 223 |
|
| 224 |
/* z_flags */ |
| 225 |
#define ZZIP_IS_ENCRYPTED(p) ((*(zzip_byte_t*)p)&1) |
| 226 |
#define ZZIP_IS_COMPRLEVEL(p) (((*(zzip_byte_t*)p)>>1)&3) |
| 227 |
#define ZZIP_IS_STREAMED(p) (((*(zzip_byte_t*)p)>>3)&1) |
| 228 |
#define ZZIP_IS_PATCHED(p) (((*(zzip_byte_t*)p)>>5)&1) |
| 229 |
|
| 230 |
/* z_compr */ |
| 231 |
#define ZZIP_IS_STORED 0 |
| 232 |
#define ZZIP_IS_SHRUNK 1 |
| 233 |
#define ZZIP_IS_REDUCEDx1 2 |
| 234 |
#define ZZIP_IS_REDUCEDx2 3 |
| 235 |
#define ZZIP_IS_REDUCEDx3 4 |
| 236 |
#define ZZIP_IS_REDUCEDx4 5 |
| 237 |
#define ZZIP_IS_IMPLODED 6 |
| 238 |
#define ZZIP_IS_TOKENIZED 7 |
| 239 |
#define ZZIP_IS_DEFLATED 8 |
| 240 |
#define ZZIP_IS_DEFLATED_BETTER 9 |
| 241 |
#define ZZIP_IS_IMPLODED_BETTER 10 |
| 242 |
|
| 243 |
/* deflated comprlevel */ |
| 244 |
#define ZZIP_DEFLATED_STD_COMPR 0 |
| 245 |
#define ZZIP_DEFLATED_MAX_COMPR 1 |
| 246 |
#define ZZIP_DEFLATED_LOW_COMPR 2 |
| 247 |
#define ZZIP_DEFLATED_MIN_COMPR 3 |
| 248 |
|
| 249 |
# ifdef _MSC_VER |
| 250 |
# pragma pack(pop) |
| 251 |
# endif |
| 252 |
|
| 253 |
#endif /* _ZZIPFORMAT_H */ |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|