File: zunz_private.h

package info (click to toggle)
gretl 2022c-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 59,552 kB
  • sloc: ansic: 409,074; sh: 4,449; makefile: 3,222; cpp: 2,777; xml: 599; perl: 364
file content (287 lines) | stat: -rw-r--r-- 10,291 bytes parent folder | download | duplicates (7)
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
/*
  The code here is based on code by Mark Adler et al. which is
  Copyright (c) 1990-2005 Info-ZIP.  Specifically, it derives from zip
  version 2.31.  Modifications are by Allin Cottrell, March, 2006.
  Please see the included file "LICENSE" which contains the Info-ZIP
  license information.
*/

#ifndef ZUNZ_PRIVATE_H_
#define ZUNZ_PRIVATE_H_

#include "libgretl.h"
#include "version.h"

#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#ifdef S_IFLNK
# define LSTAT   lstat
#else
# define LSTAT   stat
#endif

/* for recording "version made by" in zipfiles */
#define Z_MAJORVER 2
#define Z_MINORVER 3

/* include library's public header */
#include "zipunzip.h"

/* these OS codes are defined in pkzip appnote.txt */
#ifdef WIN32
# define OS_CODE 0xb00
#else
# define OS_CODE 0x300  /* assume unix */
#endif

/* option accessor macros */
#define recurse(o)       (o & ZIP_RECURSE_DIRS)
#define delete_inputs(o) (o & ZIP_DELETE_INPUTS)
#define put_links(o)     (o & ZIP_PUT_LINKS)

/* minimum and maximum match lengths */
#define MIN_MATCH  3
#define MAX_MATCH  258

/* window size = 32K */
#define WSIZE (0x8000)

/* Minimum amount of lookahead, except at the end of the input file.
 * See deflate.c in zip-2.31 for comments about the MIN_MATCH + 1.
 */
#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)

/* Structure carrying extended timestamp information */

typedef struct iztimes_ iztimes;

struct iztimes_ {
    time_t atime;                /* new access time */
    time_t mtime;                /* new modification time */
    time_t ctime;                /* new creation time (!= Unix st.ctime) */
};

/* Lengths of headers after signatures in bytes */
#define LOCHEAD 26
#define CENHEAD 42
#define ENDHEAD 18

/* MSDOS file or directory attributes */
#define MSDOS_DIR_ATTR    0x10

enum {
    MARK_NONE,
    MARK_ZIP,
    MARK_UNZIP,
    MARK_DELETE
};

/* Structures for in-memory file information */

typedef struct zlist_ zlist;

/* See also zipfile structure info in zipfile.c */

struct zlist_ {
    guint16 version_made;         /* zip version by which file compressed */
    guint16 version_extract;      /* zip version required to extract */
    guint16 flags;                /* encrypt, deflate flags */
    guint16 method;               /* compression method */
    guint32 time;                 /* last modified file time, DOS format */
    guint32 crc;                  /* uncompressed crc-32 for file */
    guint32 csize;                /* compressed size */
    guint32 usize;                /* uncompressed size */
    size_t namelen;               /* length of filename */
    size_t extlen;                /* offset of extlen must be >= LOCHEAD */
    size_t cextlen;               /* extlen as in central directory */
    size_t comlen;                /* comment length */
    guint16 dsk;                  /* disk number start */
    guint16 att;                  /* file attributes */
    guint16 lflags;               /* offset of lflags must be >= LOCHEAD */
    guint32 atx;                  /* extended attributes */
    guint32 off;                  /* offset in file */
    gchar *name;                  /* File name in zip file */
    gchar *iname;                 /* Internal file name after cleanup */
    gchar *zname;                 /* External version of internal name */
    char *extra;                  /* Extra field (set only if ext != 0) */
    char *cextra;                 /* Extra in central (set only if cext != 0) */
    char *comment;                /* Comment (set only if com != 0) */
    int mark;                     /* Marker for files to operate on */
    int dosflag;                  /* Set to force MSDOS file attributes */
    zlist *nxt;                   /* Pointer to next header in list */
};

typedef struct flist_ flist;

struct flist_ {
    gchar *name;                  /* Raw internal file name */
    gchar *iname;                 /* Internal file name after cleanup */
    gchar *zname;                 /* External version of internal name */
    flist **lst;                  /* Pointer to link pointing here */
    flist *nxt;                   /* Link to next name */
};

enum {
    ZF_STATE_UNKNOWN,
    ZF_STATE_OLD,
    ZF_STATE_NEW
};

typedef struct zfile_ zfile;

struct zfile_ {
    ZipOption opt;       /* option flags */
    int state;           /* unknown, pre-existing file, or new file */
    char *fname;         /* file name */
    FILE *fp;            /* file pointer */
    int method;          /* compression method */
    int level;           /* compression level */
    int zstart;          /* starting offset of zip structures */
    int zcount;          /* number of files in zip file */
    int zcomlen;         /* length of zip file comment */
    char *zcomment;      /* zip file comment (not zero-terminated) */
    int fcount;          /* count of source files */
    zlist **zsort;       /* list of files sorted by name */
    guint32 tempzn;      /* count of bytes written to output file */
    z_stream strm;       /* stream for deflation/inflation */
    int strm_initted;    /* flag: is strm initialized yet? */
    const char **wanted; /* array of filenames wanted for extraction */
    const char *eprefix; /* path to prepend on extraction */
    char *matches;       /* array for recording matches of wanted files */
};

/* internal file attribute */
#define UNKNOWN (-1)
#define BINARY  0
#define ASCII   1

/* extra field definitions */
#define EF_IZUNIX    0x5855   /* UNIX Extra Field ID ("UX") */
#define EF_IZUNIX2   0x7855   /* Info-ZIP's new Unix ("Ux") */
#define EF_TIME      0x5455   /* universal timestamp ("UT") */
#define EF_OS2EA     0x0009   /* OS/2 Extra Field ID (extended attributes) */
#define EF_ACL       0x4C41   /* ACL Extra Field ID (access control list, "AL") */
#define EF_NTSD      0x4453   /* NT Security Descriptor Extra Field ID, ("SD") */

/* Definitions for extra field handling: */
#define EF_SIZE_MAX  ((unsigned)0xFFFF) /* hard limit of total e.f. length */
#define EB_HEADSIZE       4     /* length of a extra field block header */
#define EB_ID             0     /* offset of block ID in header */
#define EB_LEN            2     /* offset of data length field in header */
#define EB_MEMCMPR_HSIZ   6     /* header length for memcompressed data */
#define EB_DEFLAT_EXTRA  10     /* overhead for 64kByte "undeflatable" data */

#define EB_UX_MINLEN      8     /* minimal "UX" field contains atime, mtime */
#define EB_UX_ATIME       0     /* offset of atime in "UX" extra field data */
#define EB_UX_MTIME       4     /* offset of mtime in "UX" extra field data */

#define EB_UX_FULLSIZE    12    /* full "UX" field (atime, mtime, uid, gid) */
#define EB_UX_UID         8     /* byte offset of UID in "UX" field data */
#define EB_UX_GID         10    /* byte offset of GID in "UX" field data */

#define EB_UT_MINLEN      1     /* minimal UT field contains Flags byte */
#define EB_UT_FLAGS       0     /* byte offset of Flags field */
#define EB_UT_TIME1       1     /* byte offset of 1st time value */
#define EB_UT_FL_MTIME    (1 << 0)      /* mtime present */
#define EB_UT_FL_ATIME    (1 << 1)      /* atime present */
#define EB_UT_FL_CTIME    (1 << 2)      /* ctime present */
#define EB_UT_LEN(n)      (EB_UT_MINLEN + 4 * (n))

#define EB_UX2_MINLEN     4     /* minimal Ux field contains UID/GID */
#define EB_UX2_UID        0     /* byte offset of UID in "Ux" field data */
#define EB_UX2_GID        2     /* byte offset of GID in "Ux" field data */
#define EB_UX2_VALID      (1 << 8)      /* UID/GID present */

/* Error return codes */
#include "ziperr.h"

#define DOSTIME_MINIMUM         ((guint32)0x00210000L)
#define DOSTIME_2038_01_18      ((guint32)0x74320000L)

#define BEST -1                 /* Use best method (deflation or store) */
#define STORE 0                 /* Store method */
#define DEFLATE 8               /* Deflation method */

/* global vars, in main.c  */

extern zlist *zfiles;    /* Pointer to list of files in zip file */
extern flist *found;     /* List of names found */
extern flist **fnxt;     /* Where to put next in found list */

/* end globals */

enum {
    ZIP_DO_CHECK,
    ZIP_DO_NEW,
    ZIP_DO_ZIP,
    ZIP_DO_LIST,
    ZIP_DO_UNZIP,
    ZIP_DO_DELETE
};

/* function prototypes */

/* main.c */
int ziperr (int err, const char *format, ...);
void trace (int level, const char *format, ...);

/* zipwork.c */
int zipup (zfile *zf, zlist *z);
void zlib_deflate_free (zfile *zf);
int decompress_to_file (zfile *zf, zlist *z, long offset);

/* zipfile.c */
int get_ef_ut_ztime (zlist *, iztimes *);
int get_ef_mode (zlist *z);
int delete_input_files (void);
int read_zipfile (zfile *zf, int task);
int put_local_header (zlist *z, FILE *fp);
int put_extended_header (zlist *z, FILE *fp);
int put_central_header (zlist *z, FILE *fp);
int put_end_dir (int nentries, guint32 dirsize, guint32 offset, size_t zcomlen, 
		 const char *comment, FILE *fp);
int zipcopy (zfile *zf, zlist *z, FILE *fp, FILE *fq);

 /* fileio.c */
flist *flist_expel (flist *f, int *fcount);
int newname (const char *name, zfile *zf);

time_t dos2unixtime (guint32 dost);
guint32 dostime (int yr, int mon, int day, int hr, int min, int sec);
guint32 unix2dostime (time_t *);
int is_symlink (guint32 attr);

#ifdef S_IFLNK
# define read_symlink(p,b,n) readlink(p,b,n)
#else /* !S_IFLNK */
# define read_symlink(p,b,n) (0)
#endif /* !S_IFLNK */

int replace_file (char *dest, char *src);
int get_file_attributes (const char *fname);
int fcopy (FILE *f, FILE *g, guint32 n);

/* system.c */
char *internal_to_external (const char *iname);
char *external_to_internal (const char *xname, zfile *zf, GError **gerr);
int add_filenames (const char *fname, zfile *zf);
void time_stamp_file (const char *fname, guint32 dost);
guint32 file_mod_time (const char *fname, guint32 *attr, long *fsize, iztimes *t,
		       zfile *zf);
int set_extra_field (zfile *zf, zlist *z, iztimes *z_utim);

/* filename comparisons */

#ifdef WIN32
# define fnamecmp(a,b) (g_strcasecmp((a),(b)))
/* system.c */
int wanted_namecmp (const char *fname, const char *zname);
#else
# define fnamecmp(a,b) (strcmp((a),(b)))
# define wanted_namecmp(a,b) (strcmp((a),(b)))
#endif

#endif /* ZUNZ_PRIVATE_H_ */