author | Claus Gittinger <cg@exept.de> |
Fri, 12 Feb 1999 15:57:38 +0100 | |
changeset 726 | 5b9f13c73b31 |
parent 723 | 04567da3394f |
child 766 | 4c422664dcc8 |
permissions | -rw-r--r-- |
624 | 1 |
" |
2 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
636 | 3 |
All Rights Reserved |
624 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
622 | 13 |
|
14 |
Object subclass:#ZipArchive |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
15 |
instanceVariableNames:'file mode archiveName firstEntry lastEntry |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
16 |
recentlyExtractedEntries' |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
17 |
classVariableNames:'RecentlyUsedZipArchives FlushBlock ECREC_SIZE LREC_SIZE CREC_SIZE |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
18 |
SIZE_CENTRAL_DIRECTORY TOTAL_ENTRIES_CENTRAL_DIR |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
19 |
C_COMPRESSED_SIZE C_RELATIVE_OFFSET_LOCAL_HEADER |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
20 |
C_FILENAME_LENGTH C_UNCOMPRESSED_SIZE ZipFileFormatErrorSignal |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
21 |
COMPR_STORED COMPR_SHRUNK COMPR_REDUCED1 COMPR_REDUCED2 |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
22 |
COMPR_REDUCED3 COMPR_REDUCED4 COMPR_IMPLODED COMPR_TOKENIZED |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
23 |
COMPR_DEFLATED' |
622 | 24 |
poolDictionaries:'' |
25 |
category:'System-Support-FileFormats' |
|
26 |
! |
|
27 |
||
28 |
Object subclass:#ZipMember |
|
29 |
instanceVariableNames:'next relative_offset_local_header compressed_size |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
30 |
uncompressed_size name crc32 compression_method data' |
622 | 31 |
classVariableNames:'' |
32 |
poolDictionaries:'' |
|
33 |
privateIn:ZipArchive |
|
34 |
! |
|
35 |
||
636 | 36 |
!ZipArchive primitiveDefinitions! |
37 |
%{ |
|
38 |
||
720 | 39 |
|
636 | 40 |
#include <stdio.h> |
41 |
||
42 |
#define uchar unsigned char |
|
43 |
#define ushort unsigned short |
|
44 |
#define ulong unsigned long |
|
45 |
||
46 |
/* |
|
47 |
* inflate definitions |
|
48 |
*/ |
|
49 |
#define PKZIP_BUG_WORKAROUND /* PKZIP 1.93a problem--live with it */ |
|
50 |
#ifndef WSIZE /* default is 32K */ |
|
51 |
# define WSIZE 0x8000 /* window size--must be a power of two, and at least */ |
|
52 |
#endif /* 32K for zip's deflate method */ |
|
53 |
||
54 |
#define NEXTBYTE (*inPtr++) |
|
637 | 55 |
#define XXXFLUSH(n) slide += (n) |
723 | 56 |
#define FLUSH(n) { bcopy(slide, outPtr, (n)); outPtr += (n); } |
57 |
||
58 |
#ifdef DEBUG |
|
59 |
# define Trace(x) if (debugTrace) { fprintf x ; } |
|
636 | 60 |
#else |
61 |
# define Trace(x) /* nothing */ |
|
62 |
#endif |
|
63 |
||
64 |
/* Huffman code lookup table entry--this entry is four bytes for machines |
|
65 |
that have 16-bit pointers (e.g. PC's in the small or medium model). |
|
66 |
Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16 |
|
67 |
means that v is a literal, 16 < e < 32 means that v is a pointer to |
|
68 |
the next table, which codes e - 16 bits, and lastly e == 99 indicates |
|
69 |
an unused code. If a code with e == 99 is looked up, this implies an |
|
70 |
error in the data. */ |
|
71 |
struct huft { |
|
72 |
uchar e; /* number of extra bits or operation */ |
|
73 |
uchar b; /* number of bits in this code or subcode */ |
|
74 |
union { |
|
75 |
ushort n; /* literal, length base, or distance base */ |
|
76 |
struct huft *t; /* pointer to next level of table */ |
|
77 |
} v; |
|
78 |
}; |
|
79 |
||
80 |
%} |
|
81 |
! ! |
|
82 |
||
83 |
!ZipArchive primitiveVariables! |
|
84 |
%{ |
|
85 |
||
723 | 86 |
static int debugTrace = 0; |
87 |
||
636 | 88 |
/* |
89 |
* inflate variables |
|
90 |
*/ |
|
91 |
||
92 |
static unsigned char *inPtr; |
|
93 |
static unsigned char *outPtr; |
|
94 |
static unsigned char *slide; |
|
95 |
||
96 |
static int qflag = 0; |
|
97 |
||
98 |
/* The inflate algorithm uses a sliding 32K byte window on the uncompressed |
|
99 |
stream to find repeated byte strings. This is implemented here as a |
|
100 |
circular buffer. The index is updated simply by incrementing and then |
|
101 |
and'ing with 0x7fff (32K-1). */ |
|
102 |
/* It is left to other modules to supply the 32K area. It is assumed |
|
103 |
to be usable as if it were declared "uchar slide[32768];" or as just |
|
104 |
"uchar *slide;" and then malloc'ed in the latter case. The definition |
|
105 |
must be in unzip.h, included above. */ |
|
106 |
static unsigned wp; /* current position in slide */ |
|
107 |
||
108 |
||
109 |
/* Tables for deflate from PKZIP's appnote.txt. */ |
|
110 |
static unsigned border[] = { /* Order of the bit length code lengths */ |
|
111 |
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
|
112 |
||
113 |
static ushort cplens[] = { /* Copy lengths for literal codes 257..285 */ |
|
114 |
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, |
|
115 |
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; |
|
116 |
/* note: see note #13 above about the 258 in this list. */ |
|
117 |
||
118 |
static ushort cplext[] = { /* Extra bits for literal codes 257..285 */ |
|
119 |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, |
|
120 |
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ |
|
121 |
||
122 |
static ushort cpdist[] = { /* Copy offsets for distance codes 0..29 */ |
|
123 |
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
|
124 |
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, |
|
125 |
8193, 12289, 16385, 24577}; |
|
126 |
static ushort cpdext[] = { /* Extra bits for distance codes */ |
|
127 |
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, |
|
128 |
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, |
|
129 |
12, 12, 13, 13}; |
|
130 |
||
131 |
/* And'ing with mask[n] masks the lower n bits */ |
|
132 |
static ushort mask[] = { |
|
133 |
0x0000, |
|
134 |
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, |
|
135 |
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff |
|
136 |
}; |
|
137 |
||
138 |
||
139 |
/* Macros for inflate() bit peeking and grabbing. |
|
140 |
The usage is: |
|
141 |
||
142 |
NEEDBITS(j) |
|
143 |
x = b & mask[j]; |
|
144 |
DUMPBITS(j) |
|
145 |
||
146 |
where NEEDBITS makes sure that b has at least j bits in it, and |
|
147 |
DUMPBITS removes the bits from b. The macros use the variable k |
|
148 |
for the number of bits in b. Normally, b and k are register |
|
149 |
variables for speed, and are initialized at the begining of a |
|
150 |
routine that uses these macros from a global bit buffer and count. |
|
151 |
||
152 |
In order to not ask for more bits than there are in the compressed |
|
153 |
stream, the Huffman tables are constructed to only ask for just |
|
154 |
enough bits to make up the end-of-block code (value 256). Then no |
|
155 |
bytes need to be "returned" to the buffer at the end of the last |
|
156 |
block. See the huft_build() routine. |
|
157 |
*/ |
|
158 |
||
159 |
static ulong bb; /* bit buffer */ |
|
160 |
static unsigned bk; /* bits in bit buffer */ |
|
161 |
||
637 | 162 |
#define NEEDBITS(n) { while(k<(n)){ b |= ((ulong)NEXTBYTE)<<k; k+=8; } } |
163 |
#define DUMPBITS(n) { b>>=(n); k-=(n); } |
|
636 | 164 |
|
165 |
||
166 |
/* |
|
167 |
Huffman code decoding is performed using a multi-level table lookup. |
|
168 |
The fastest way to decode is to simply build a lookup table whose |
|
169 |
size is determined by the longest code. However, the time it takes |
|
170 |
to build this table can also be a factor if the data being decoded |
|
171 |
is not very long. The most common codes are necessarily the |
|
172 |
shortest codes, so those codes dominate the decoding time, and hence |
|
173 |
the speed. The idea is you can have a shorter table that decodes the |
|
174 |
shorter, more probable codes, and then point to subsidiary tables for |
|
175 |
the longer codes. The time it costs to decode the longer codes is |
|
176 |
then traded against the time it takes to make longer tables. |
|
177 |
||
178 |
This results of this trade are in the variables lbits and dbits |
|
179 |
below. lbits is the number of bits the first level table for literal/ |
|
180 |
length codes can decode in one step, and dbits is the same thing for |
|
181 |
the distance codes. Subsequent tables are also less than or equal to |
|
182 |
those sizes. These values may be adjusted either when all of the |
|
183 |
codes are shorter than that, in which case the longest code length in |
|
184 |
bits is used, or when the shortest code is *longer* than the requested |
|
185 |
table size, in which case the length of the shortest code in bits is |
|
186 |
used. |
|
187 |
||
188 |
There are two different values for the two tables, since they code a |
|
189 |
different number of possibilities each. The literal/length table |
|
190 |
codes 286 possible values, or in a flat code, a little over eight |
|
191 |
bits. The distance table codes 30 possible values, or a little less |
|
192 |
than five bits, flat. The optimum values for speed end up being |
|
193 |
about one bit more than those, so lbits is 8+1 and dbits is 5+1. |
|
194 |
The optimum values may differ though from machine to machine, and |
|
195 |
possibly even between compilers. Your mileage may vary. |
|
196 |
*/ |
|
197 |
||
198 |
||
199 |
static int lbits = 9; /* bits in base literal/length lookup table */ |
|
200 |
static int dbits = 6; /* bits in base distance lookup table */ |
|
201 |
||
202 |
||
203 |
/* If BMAX needs to be larger than 16, then h and x[] should be ulong. */ |
|
204 |
#define BMAX 16 /* maximum bit length of any code (16 for explode) */ |
|
205 |
#define N_MAX 288 /* maximum number of codes in any set */ |
|
206 |
||
207 |
||
208 |
static unsigned hufts; /* track memory usage */ |
|
209 |
||
210 |
%} |
|
211 |
! ! |
|
212 |
||
213 |
!ZipArchive primitiveFunctions! |
|
214 |
%{ |
|
215 |
||
216 |
/* |
|
217 |
* inflate algorithm |
|
218 |
*/ |
|
219 |
||
220 |
/* Free the malloc'ed tables built by huft_build(), which makes a linked |
|
221 |
list of the tables it made, with the links in a dummy first entry of |
|
222 |
each table. */ |
|
223 |
static int |
|
224 |
huft_free(t) |
|
225 |
struct huft *t; /* table to free */ |
|
226 |
{ |
|
227 |
register struct huft *p, *q; |
|
228 |
||
229 |
||
230 |
/* Go through linked list, freeing from the malloced (t[-1]) address. */ |
|
231 |
p = t; |
|
232 |
while (p != (struct huft *)NULL) |
|
233 |
{ |
|
234 |
q = (--p)->v.t; |
|
235 |
free(p); |
|
236 |
p = q; |
|
237 |
} |
|
238 |
return 0; |
|
239 |
} |
|
240 |
||
241 |
||
242 |
||
243 |
||
244 |
/* Given a list of code lengths and a maximum table size, make a set of |
|
245 |
tables to decode that set of codes. Return zero on success, one if |
|
246 |
the given code set is incomplete (the tables are still built in this |
|
247 |
case), two if the input is invalid (all zero length codes or an |
|
248 |
oversubscribed set of lengths), and three if not enough memory. |
|
249 |
The code with value 256 is special, and the tables are constructed |
|
250 |
so that no bits beyond that code are fetched when that code is |
|
251 |
decoded. */ |
|
252 |
static int |
|
253 |
huft_build(b, n, s, d, e, t, m) |
|
254 |
unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ |
|
255 |
unsigned n; /* number of codes (assumed <= N_MAX) */ |
|
256 |
unsigned s; /* number of simple-valued codes (0..s-1) */ |
|
257 |
ushort *d; /* list of base values for non-simple codes */ |
|
258 |
ushort *e; /* list of extra bits for non-simple codes */ |
|
259 |
struct huft **t; /* result: starting table */ |
|
260 |
int *m; /* maximum lookup bits, returns actual */ |
|
261 |
{ |
|
262 |
unsigned a; /* counter for codes of length k */ |
|
263 |
unsigned c[BMAX+1]; /* bit length count table */ |
|
264 |
unsigned el; /* length of EOB code (value 256) */ |
|
265 |
unsigned f; /* i repeats in table every f entries */ |
|
266 |
int g; /* maximum code length */ |
|
267 |
int h; /* table level */ |
|
268 |
register unsigned i; /* counter, current code */ |
|
269 |
register unsigned j; /* counter */ |
|
270 |
register int k; /* number of bits in current code */ |
|
271 |
int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */ |
|
272 |
int *l = lx+1; /* stack of bits per table */ |
|
273 |
register unsigned *p; /* pointer into c[], b[], or v[] */ |
|
274 |
register struct huft *q; /* points to current table */ |
|
275 |
struct huft r; /* table entry for structure assignment */ |
|
276 |
struct huft *u[BMAX]; /* table stack */ |
|
277 |
static unsigned v[N_MAX]; /* values in order of bit length */ |
|
278 |
register int w; /* bits before this table == (l * h) */ |
|
279 |
unsigned x[BMAX+1]; /* bit offsets, then code stack */ |
|
280 |
unsigned *xp; /* pointer into x */ |
|
281 |
int y; /* number of dummy codes added */ |
|
282 |
unsigned z; /* number of entries in current table */ |
|
283 |
||
284 |
||
285 |
/* Generate counts for each bit length */ |
|
286 |
el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */ |
|
287 |
bzero((char *)c, sizeof(c)); |
|
288 |
p = b; i = n; |
|
289 |
do { |
|
290 |
c[*p]++; p++; /* assume all entries <= BMAX */ |
|
291 |
} while (--i); |
|
292 |
if (c[0] == n) /* null input--all zero length codes */ |
|
293 |
{ |
|
294 |
*t = (struct huft *)NULL; |
|
295 |
*m = 0; |
|
296 |
return 0; |
|
297 |
} |
|
298 |
||
299 |
||
300 |
/* Find minimum and maximum length, bound *m by those */ |
|
301 |
for (j = 1; j <= BMAX; j++) |
|
302 |
if (c[j]) |
|
303 |
break; |
|
304 |
k = j; /* minimum code length */ |
|
305 |
if ((unsigned)*m < j) |
|
306 |
*m = j; |
|
307 |
for (i = BMAX; i; i--) |
|
308 |
if (c[i]) |
|
309 |
break; |
|
310 |
g = i; /* maximum code length */ |
|
311 |
if ((unsigned)*m > i) |
|
312 |
*m = i; |
|
313 |
||
314 |
||
315 |
/* Adjust last length count to fill out codes, if needed */ |
|
316 |
for (y = 1 << j; j < i; j++, y <<= 1) |
|
317 |
if ((y -= c[j]) < 0) |
|
318 |
return 2; /* bad input: more codes than bits */ |
|
319 |
if ((y -= c[i]) < 0) |
|
320 |
return 2; |
|
321 |
c[i] += y; |
|
322 |
||
323 |
||
324 |
/* Generate starting offsets into the value table for each length */ |
|
325 |
x[1] = j = 0; |
|
326 |
p = c + 1; xp = x + 2; |
|
327 |
while (--i) { /* note that i == g from above */ |
|
328 |
*xp++ = (j += *p++); |
|
329 |
} |
|
330 |
||
331 |
||
332 |
/* Make a table of values in order of bit lengths */ |
|
333 |
p = b; i = 0; |
|
334 |
do { |
|
335 |
if ((j = *p++) != 0) |
|
336 |
v[x[j]++] = i; |
|
337 |
} while (++i < n); |
|
338 |
||
339 |
||
340 |
/* Generate the Huffman codes and for each, make the table entries */ |
|
341 |
x[0] = i = 0; /* first Huffman code is zero */ |
|
342 |
p = v; /* grab values in bit order */ |
|
343 |
h = -1; /* no tables yet--level -1 */ |
|
344 |
w = l[-1] = 0; /* no bits decoded yet */ |
|
345 |
u[0] = (struct huft *)NULL; /* just to keep compilers happy */ |
|
346 |
q = (struct huft *)NULL; /* ditto */ |
|
347 |
z = 0; /* ditto */ |
|
348 |
||
349 |
/* go through the bit lengths (k already is bits in shortest code) */ |
|
350 |
for (; k <= g; k++) |
|
351 |
{ |
|
352 |
a = c[k]; |
|
353 |
while (a--) |
|
354 |
{ |
|
355 |
/* here i is the Huffman code of length k bits for value *p */ |
|
356 |
/* make tables up to required level */ |
|
357 |
while (k > w + l[h]) |
|
358 |
{ |
|
359 |
w += l[h++]; /* add bits already decoded */ |
|
360 |
||
361 |
/* compute minimum size table less than or equal to *m bits */ |
|
362 |
z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */ |
|
363 |
if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ |
|
364 |
{ /* too few codes for k-w bit table */ |
|
365 |
f -= a + 1; /* deduct codes from patterns left */ |
|
366 |
xp = c + k; |
|
367 |
while (++j < z) /* try smaller tables up to z bits */ |
|
368 |
{ |
|
369 |
if ((f <<= 1) <= *++xp) |
|
370 |
break; /* enough codes to use up j bits */ |
|
371 |
f -= *xp; /* else deduct codes from patterns */ |
|
372 |
} |
|
373 |
} |
|
374 |
if ((unsigned)w + j > el && (unsigned)w < el) |
|
375 |
j = el - w; /* make EOB code end at table */ |
|
376 |
z = 1 << j; /* table entries for j-bit table */ |
|
377 |
l[h] = j; /* set table size in stack */ |
|
378 |
||
379 |
/* allocate and link in new table */ |
|
380 |
if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == |
|
381 |
(struct huft *)NULL) |
|
382 |
{ |
|
383 |
if (h) |
|
384 |
huft_free(u[0]); |
|
385 |
return 3; /* not enough memory */ |
|
386 |
} |
|
387 |
hufts += z + 1; /* track memory usage */ |
|
388 |
*t = q + 1; /* link to list for huft_free() */ |
|
389 |
*(t = &(q->v.t)) = (struct huft *)NULL; |
|
390 |
u[h] = ++q; /* table starts after link */ |
|
391 |
||
392 |
/* connect to last table, if there is one */ |
|
393 |
if (h) |
|
394 |
{ |
|
395 |
x[h] = i; /* save pattern for backing up */ |
|
396 |
r.b = (uchar)l[h-1]; /* bits to dump before this table */ |
|
397 |
r.e = (uchar)(16 + j); /* bits in this table */ |
|
398 |
r.v.t = q; /* pointer to this table */ |
|
399 |
j = (i & ((1 << w) - 1)) >> (w - l[h-1]); |
|
400 |
u[h-1][j] = r; /* connect to last table */ |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
/* set up table entry in r */ |
|
405 |
r.b = (uchar)(k - w); |
|
406 |
if (p >= v + n) |
|
407 |
r.e = 99; /* out of values--invalid code */ |
|
408 |
else if (*p < s) |
|
409 |
{ |
|
410 |
r.e = (uchar)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ |
|
411 |
r.v.n = *p++; /* simple code is just the value */ |
|
412 |
} |
|
413 |
else |
|
414 |
{ |
|
415 |
r.e = (uchar)e[*p - s]; /* non-simple--look up in lists */ |
|
416 |
r.v.n = d[*p++ - s]; |
|
417 |
} |
|
418 |
||
419 |
/* fill code-like entries with r */ |
|
420 |
f = 1 << (k - w); |
|
421 |
for (j = i >> w; j < z; j += f) |
|
422 |
q[j] = r; |
|
423 |
||
424 |
/* backwards increment the k-bit code i */ |
|
425 |
for (j = 1 << (k - 1); i & j; j >>= 1) |
|
426 |
i ^= j; |
|
427 |
i ^= j; |
|
428 |
||
429 |
/* backup over finished tables */ |
|
430 |
while ((i & ((1 << w) - 1)) != x[h]) |
|
431 |
w -= l[--h]; /* don't need to update q */ |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
||
436 |
/* return actual size of base table */ |
|
437 |
*m = l[0]; |
|
438 |
||
439 |
||
440 |
/* Return true (1) if we were given an incomplete table */ |
|
441 |
return y != 0 && g != 1; |
|
442 |
} |
|
443 |
||
444 |
||
445 |
||
446 |
#ifdef ASM_INFLATECODES |
|
447 |
# define inflate_codes(tl,td,bl,bd) flate_codes(tl,td,bl,bd,(uchar *)slide) |
|
448 |
int flate_codes OF((struct huft *, struct huft *, int, int, uchar *)); |
|
449 |
||
450 |
#else |
|
451 |
||
452 |
/* inflate (decompress) the codes in a deflated (compressed) block. |
|
453 |
Return an error code or zero if it all goes ok. */ |
|
454 |
static int |
|
455 |
inflate_codes(tl, td, bl, bd) |
|
456 |
struct huft *tl, *td; /* literal/length and distance decoder tables */ |
|
457 |
int bl, bd; /* number of bits decoded by tl[] and td[] */ |
|
458 |
{ |
|
459 |
register unsigned e; /* table entry flag/number of extra bits */ |
|
460 |
unsigned n, d; /* length and index for copy */ |
|
461 |
unsigned w; /* current window position */ |
|
462 |
struct huft *t; /* pointer to table entry */ |
|
463 |
unsigned ml, md; /* masks for bl and bd bits */ |
|
464 |
register ulong b; /* bit buffer */ |
|
465 |
register unsigned k; /* number of bits in bit buffer */ |
|
466 |
||
467 |
||
468 |
/* make local copies of globals */ |
|
469 |
b = bb; /* initialize bit buffer */ |
|
470 |
k = bk; |
|
471 |
w = wp; /* initialize window position */ |
|
472 |
||
473 |
||
474 |
/* inflate the coded data */ |
|
475 |
ml = mask[bl]; /* precompute masks for speed */ |
|
476 |
md = mask[bd]; |
|
477 |
while (1) /* do until end of block */ |
|
478 |
{ |
|
479 |
NEEDBITS((unsigned)bl) |
|
480 |
if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) |
|
481 |
do { |
|
482 |
if (e == 99) |
|
483 |
return 1; |
|
484 |
DUMPBITS(t->b) |
|
485 |
e -= 16; |
|
486 |
NEEDBITS(e) |
|
487 |
} while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16); |
|
488 |
DUMPBITS(t->b) |
|
489 |
if (e == 16) /* then it's a literal */ |
|
490 |
{ |
|
491 |
slide[w++] = (uchar)t->v.n; |
|
492 |
if (w == WSIZE) |
|
493 |
{ |
|
494 |
FLUSH(w); |
|
495 |
w = 0; |
|
496 |
} |
|
497 |
} |
|
498 |
else /* it's an EOB or a length */ |
|
499 |
{ |
|
500 |
/* exit if end of block */ |
|
501 |
if (e == 15) |
|
502 |
break; |
|
503 |
||
504 |
/* get length of block to copy */ |
|
505 |
NEEDBITS(e) |
|
506 |
n = t->v.n + ((unsigned)b & mask[e]); |
|
507 |
DUMPBITS(e); |
|
508 |
||
509 |
/* decode distance of block to copy */ |
|
510 |
NEEDBITS((unsigned)bd) |
|
511 |
if ((e = (t = td + ((unsigned)b & md))->e) > 16) |
|
512 |
do { |
|
513 |
if (e == 99) |
|
514 |
return 1; |
|
515 |
DUMPBITS(t->b) |
|
516 |
e -= 16; |
|
517 |
NEEDBITS(e) |
|
518 |
} while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16); |
|
519 |
DUMPBITS(t->b) |
|
520 |
NEEDBITS(e) |
|
521 |
d = w - t->v.n - ((unsigned)b & mask[e]); |
|
522 |
DUMPBITS(e) |
|
523 |
||
524 |
/* do the copy */ |
|
525 |
do { |
|
526 |
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); |
|
527 |
#ifndef NOMEMCPY |
|
528 |
if (w - d >= e) /* (this test assumes unsigned comparison) */ |
|
529 |
{ |
|
530 |
# ifdef USE_MEMCPY |
|
531 |
memcpy(slide + w, slide + d, e); |
|
532 |
# else |
|
533 |
bcopy(slide + d, slide + w, e); |
|
534 |
# endif |
|
535 |
w += e; |
|
536 |
d += e; |
|
537 |
} |
|
538 |
else /* do it slow to avoid memcpy() overlap */ |
|
539 |
#endif /* !NOMEMCPY */ |
|
540 |
do { |
|
541 |
slide[w++] = slide[d++]; |
|
542 |
} while (--e); |
|
543 |
if (w == WSIZE) |
|
544 |
{ |
|
545 |
FLUSH(w); |
|
546 |
w = 0; |
|
547 |
} |
|
548 |
} while (n); |
|
549 |
} |
|
550 |
} |
|
551 |
||
552 |
||
553 |
/* restore the globals from the locals */ |
|
554 |
wp = w; /* restore global window pointer */ |
|
555 |
bb = b; /* restore global bit buffer */ |
|
556 |
bk = k; |
|
557 |
||
558 |
||
559 |
/* done */ |
|
560 |
return 0; |
|
561 |
} |
|
562 |
||
563 |
#endif /* ASM_INFLATECODES */ |
|
564 |
||
565 |
||
566 |
||
567 |
/* "decompress" an inflated type 0 (stored) block. */ |
|
568 |
static int |
|
569 |
inflate_stored() |
|
570 |
{ |
|
571 |
unsigned n; /* number of bytes in block */ |
|
572 |
unsigned w; /* current window position */ |
|
573 |
register ulong b; /* bit buffer */ |
|
574 |
register unsigned k; /* number of bits in bit buffer */ |
|
575 |
||
576 |
||
577 |
/* make local copies of globals */ |
|
578 |
Trace((stderr, "stored block\n")); |
|
579 |
b = bb; /* initialize bit buffer */ |
|
580 |
k = bk; |
|
581 |
w = wp; /* initialize window position */ |
|
582 |
||
583 |
||
584 |
/* go to byte boundary */ |
|
585 |
n = k & 7; |
|
586 |
DUMPBITS(n); |
|
587 |
||
588 |
||
589 |
/* get the length and its complement */ |
|
590 |
NEEDBITS(16) |
|
591 |
n = ((unsigned)b & 0xffff); |
|
592 |
DUMPBITS(16) |
|
593 |
NEEDBITS(16) |
|
594 |
if (n != (unsigned)((~b) & 0xffff)) |
|
595 |
return 1; /* error in compressed data */ |
|
596 |
DUMPBITS(16) |
|
597 |
||
598 |
||
599 |
/* read and output the compressed data */ |
|
600 |
while (n--) |
|
601 |
{ |
|
602 |
NEEDBITS(8) |
|
603 |
slide[w++] = (uchar)b; |
|
604 |
if (w == WSIZE) |
|
605 |
{ |
|
606 |
FLUSH(w); |
|
607 |
w = 0; |
|
608 |
} |
|
609 |
DUMPBITS(8) |
|
610 |
} |
|
611 |
||
612 |
||
613 |
/* restore the globals from the locals */ |
|
614 |
wp = w; /* restore global window pointer */ |
|
615 |
bb = b; /* restore global bit buffer */ |
|
616 |
bk = k; |
|
617 |
return 0; |
|
618 |
} |
|
619 |
||
620 |
||
621 |
/* Globals for literal tables (built once) */ |
|
622 |
static struct huft *fixed_tl = (struct huft *)NULL; |
|
623 |
static struct huft *fixed_td = (struct huft *)NULL; |
|
624 |
static int fixed_bl, fixed_bd; |
|
625 |
||
626 |
/* decompress an inflated type 1 (fixed Huffman codes) block. We should |
|
627 |
either replace this with a custom decoder, or at least precompute the |
|
628 |
Huffman tables. */ |
|
629 |
static int |
|
630 |
inflate_fixed() |
|
631 |
{ |
|
632 |
/* if first time, set up tables for fixed blocks */ |
|
633 |
Trace((stderr, "fixed block\n")); |
|
634 |
if (fixed_tl == (struct huft *)NULL) |
|
635 |
{ |
|
636 |
int i; /* temporary variable */ |
|
637 |
static unsigned l[288]; /* length list for huft_build */ |
|
638 |
||
639 |
/* literal table */ |
|
640 |
for (i = 0; i < 144; i++) |
|
641 |
l[i] = 8; |
|
642 |
for (; i < 256; i++) |
|
643 |
l[i] = 9; |
|
644 |
for (; i < 280; i++) |
|
645 |
l[i] = 7; |
|
646 |
for (; i < 288; i++) /* make a complete, but wrong code set */ |
|
647 |
l[i] = 8; |
|
648 |
fixed_bl = 7; |
|
649 |
if ((i = huft_build(l, 288, 257, cplens, cplext, |
|
650 |
&fixed_tl, &fixed_bl)) != 0) |
|
651 |
{ |
|
652 |
Trace((stderr, "incomplete code set 1\n")); |
|
653 |
fixed_tl = (struct huft *)NULL; |
|
654 |
return i; |
|
655 |
} |
|
656 |
||
657 |
/* distance table */ |
|
658 |
for (i = 0; i < 30; i++) /* make an incomplete code set */ |
|
659 |
l[i] = 5; |
|
660 |
fixed_bd = 5; |
|
661 |
if ((i = huft_build(l, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd)) > 1) |
|
662 |
{ |
|
663 |
Trace((stderr, "incomplete code set 2\n")); |
|
664 |
huft_free(fixed_tl); |
|
665 |
fixed_tl = (struct huft *)NULL; |
|
666 |
return i; |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
||
671 |
/* decompress until an end-of-block code */ |
|
672 |
return inflate_codes(fixed_tl, fixed_td, fixed_bl, fixed_bd) != 0; |
|
673 |
} |
|
674 |
||
675 |
||
676 |
||
677 |
/* decompress an inflated type 2 (dynamic Huffman codes) block. */ |
|
678 |
static int |
|
679 |
inflate_dynamic() |
|
680 |
{ |
|
681 |
int i; /* temporary variables */ |
|
682 |
unsigned j; |
|
683 |
unsigned l; /* last length */ |
|
684 |
unsigned m; /* mask for bit lengths table */ |
|
685 |
unsigned n; /* number of lengths to get */ |
|
686 |
struct huft *tl; /* literal/length code table */ |
|
687 |
struct huft *td; /* distance code table */ |
|
688 |
int bl; /* lookup bits for tl */ |
|
689 |
int bd; /* lookup bits for td */ |
|
690 |
unsigned nb; /* number of bit length codes */ |
|
691 |
unsigned nl; /* number of literal/length codes */ |
|
692 |
unsigned nd; /* number of distance codes */ |
|
693 |
#ifdef PKZIP_BUG_WORKAROUND |
|
694 |
static unsigned ll[288+32]; /* literal/length and distance code lengths */ |
|
695 |
#else |
|
696 |
static unsigned ll[286+30]; /* literal/length and distance code lengths */ |
|
697 |
#endif |
|
698 |
register ulong b; /* bit buffer */ |
|
699 |
register unsigned k; /* number of bits in bit buffer */ |
|
700 |
||
701 |
||
702 |
/* make local bit buffer */ |
|
703 |
Trace((stderr, "dynamic block\n")); |
|
704 |
b = bb; |
|
705 |
k = bk; |
|
706 |
||
707 |
||
708 |
/* read in table lengths */ |
|
709 |
NEEDBITS(5) |
|
710 |
nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ |
|
711 |
DUMPBITS(5) |
|
712 |
NEEDBITS(5) |
|
713 |
nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ |
|
714 |
DUMPBITS(5) |
|
715 |
NEEDBITS(4) |
|
716 |
nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ |
|
717 |
DUMPBITS(4) |
|
718 |
#ifdef PKZIP_BUG_WORKAROUND |
|
719 |
if (nl > 288 || nd > 32) |
|
720 |
#else |
|
721 |
if (nl > 286 || nd > 30) |
|
722 |
#endif |
|
723 |
{ |
|
724 |
Trace((stderr, "bad length\n")); |
|
725 |
return 1; /* bad lengths */ |
|
726 |
} |
|
727 |
||
728 |
||
729 |
/* read in bit-length-code lengths */ |
|
730 |
for (j = 0; j < nb; j++) |
|
731 |
{ |
|
732 |
NEEDBITS(3) |
|
733 |
ll[border[j]] = (unsigned)b & 7; |
|
734 |
DUMPBITS(3) |
|
735 |
} |
|
736 |
for (; j < 19; j++) |
|
737 |
ll[border[j]] = 0; |
|
738 |
||
739 |
||
740 |
/* build decoding table for trees--single level, 7 bit lookup */ |
|
741 |
bl = 7; |
|
742 |
if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) |
|
743 |
{ |
|
744 |
if (i == 1) |
|
745 |
huft_free(tl); |
|
746 |
Trace((stderr, "incomplete code set 3\n")); |
|
747 |
return i; /* incomplete code set */ |
|
748 |
} |
|
749 |
||
750 |
||
751 |
/* read in literal and distance code lengths */ |
|
752 |
n = nl + nd; |
|
753 |
m = mask[bl]; |
|
754 |
i = l = 0; |
|
755 |
while ((unsigned)i < n) |
|
756 |
{ |
|
757 |
NEEDBITS((unsigned)bl) |
|
758 |
j = (td = tl + ((unsigned)b & m))->b; |
|
759 |
DUMPBITS(j) |
|
760 |
j = td->v.n; |
|
761 |
if (j < 16) /* length of code in bits (0..15) */ |
|
762 |
ll[i++] = l = j; /* save last length in l */ |
|
763 |
else if (j == 16) /* repeat last length 3 to 6 times */ |
|
764 |
{ |
|
765 |
NEEDBITS(2) |
|
766 |
j = 3 + ((unsigned)b & 3); |
|
767 |
DUMPBITS(2) |
|
768 |
if ((unsigned)i + j > n) |
|
769 |
return 1; |
|
770 |
while (j--) |
|
771 |
ll[i++] = l; |
|
772 |
} |
|
773 |
else if (j == 17) /* 3 to 10 zero length codes */ |
|
774 |
{ |
|
775 |
NEEDBITS(3) |
|
776 |
j = 3 + ((unsigned)b & 7); |
|
777 |
DUMPBITS(3) |
|
778 |
if ((unsigned)i + j > n) |
|
779 |
return 1; |
|
780 |
while (j--) |
|
781 |
ll[i++] = 0; |
|
782 |
l = 0; |
|
783 |
} |
|
784 |
else /* j == 18: 11 to 138 zero length codes */ |
|
785 |
{ |
|
786 |
NEEDBITS(7) |
|
787 |
j = 11 + ((unsigned)b & 0x7f); |
|
788 |
DUMPBITS(7) |
|
789 |
if ((unsigned)i + j > n) |
|
790 |
return 1; |
|
791 |
while (j--) |
|
792 |
ll[i++] = 0; |
|
793 |
l = 0; |
|
794 |
} |
|
795 |
} |
|
796 |
||
797 |
||
798 |
/* free decoding table for trees */ |
|
799 |
huft_free(tl); |
|
800 |
||
801 |
||
802 |
/* restore the global bit buffer */ |
|
803 |
bb = b; |
|
804 |
bk = k; |
|
805 |
||
806 |
||
807 |
/* build the decoding tables for literal/length and distance codes */ |
|
808 |
bl = lbits; |
|
809 |
if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) |
|
810 |
{ |
|
811 |
Trace((stderr, "incomplete code set 4\n")); |
|
812 |
if (i == 1 && !qflag) { |
|
813 |
Trace((stderr, "incomplete l-tree\n")); |
|
814 |
huft_free(tl); |
|
815 |
} |
|
816 |
return i; /* incomplete code set */ |
|
817 |
} |
|
818 |
bd = dbits; |
|
819 |
if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) |
|
820 |
{ |
|
821 |
Trace((stderr, "huft_build err\n")); |
|
822 |
if (i == 1 && !qflag) { |
|
823 |
Trace((stderr, "incomplete d-tree\n")); |
|
824 |
#ifdef PKZIP_BUG_WORKAROUND |
|
825 |
i = 0; |
|
826 |
} |
|
827 |
#else |
|
828 |
huft_free(td); |
|
829 |
} |
|
830 |
huft_free(tl); |
|
831 |
return i; /* incomplete code set */ |
|
832 |
#endif |
|
833 |
} |
|
834 |
||
835 |
||
836 |
/* decompress until an end-of-block code */ |
|
837 |
if (inflate_codes(tl, td, bl, bd)) { |
|
838 |
Trace((stderr, "inflate_codes error\n")); |
|
839 |
return 1; |
|
840 |
} |
|
841 |
||
842 |
||
843 |
/* free the decoding tables, return */ |
|
844 |
huft_free(tl); |
|
845 |
huft_free(td); |
|
846 |
Trace((stderr, "block ok\n")); |
|
847 |
return 0; |
|
848 |
} |
|
849 |
||
850 |
||
851 |
||
852 |
/* decompress an inflated block */ |
|
853 |
static int |
|
637 | 854 |
inflate_block(endPtr) |
855 |
int *endPtr; /* last block flag */ |
|
636 | 856 |
{ |
857 |
unsigned t; /* block type */ |
|
858 |
register ulong b; /* bit buffer */ |
|
859 |
register unsigned k; /* number of bits in bit buffer */ |
|
860 |
||
861 |
||
637 | 862 |
Trace((stderr, "inflate_block\n")); |
863 |
||
636 | 864 |
/* make local bit buffer */ |
865 |
b = bb; |
|
866 |
k = bk; |
|
867 |
||
868 |
||
869 |
/* read in last block bit */ |
|
870 |
NEEDBITS(1) |
|
637 | 871 |
*endPtr = (int)b & 1; |
872 |
Trace((stderr, " end = %d\n", (int)b & 1)); |
|
873 |
||
636 | 874 |
DUMPBITS(1) |
875 |
||
876 |
||
877 |
/* read in block type */ |
|
878 |
NEEDBITS(2) |
|
879 |
t = (unsigned)b & 3; |
|
880 |
DUMPBITS(2) |
|
637 | 881 |
Trace((stderr, " type = %d\n", t)); |
636 | 882 |
|
883 |
||
884 |
/* restore the global bit buffer */ |
|
885 |
bb = b; |
|
886 |
bk = k; |
|
887 |
||
888 |
||
889 |
/* inflate that block type */ |
|
637 | 890 |
if (t == 2) { |
636 | 891 |
return inflate_dynamic(); |
637 | 892 |
} |
893 |
if (t == 0) { |
|
636 | 894 |
return inflate_stored(); |
637 | 895 |
} |
896 |
if (t == 1) { |
|
636 | 897 |
return inflate_fixed(); |
637 | 898 |
} |
636 | 899 |
|
900 |
Trace((stderr, "bad block type\n")); |
|
901 |
/* bad block type */ |
|
902 |
return 2; |
|
903 |
} |
|
904 |
||
905 |
||
906 |
||
907 |
/* decompress an inflated entry */ |
|
908 |
static int |
|
909 |
inflate() |
|
910 |
{ |
|
637 | 911 |
int endFlag; /* last block flag */ |
636 | 912 |
int r; /* result code */ |
913 |
unsigned h; /* maximum struct huft's malloc'ed */ |
|
914 |
||
915 |
/* initialize window, bit buffer */ |
|
916 |
wp = 0; |
|
917 |
bk = 0; |
|
918 |
bb = 0; |
|
919 |
||
637 | 920 |
endFlag = 0; |
636 | 921 |
|
922 |
/* decompress until the last block */ |
|
923 |
h = 0; |
|
924 |
do { |
|
925 |
hufts = 0; |
|
637 | 926 |
if ((r = inflate_block(&endFlag)) != 0) { |
927 |
Trace((stderr, "inflate_block -> %d\n", r)); |
|
636 | 928 |
return r; |
637 | 929 |
} |
636 | 930 |
if (hufts > h) |
931 |
h = hufts; |
|
637 | 932 |
} while (!endFlag); |
636 | 933 |
|
934 |
||
935 |
/* flush out slide */ |
|
936 |
FLUSH(wp); |
|
937 |
||
938 |
||
939 |
/* return success */ |
|
940 |
Trace((stderr, "%u bytes in Huffman tables (%d/entry)\n", |
|
941 |
h * sizeof(struct huft), sizeof(struct huft))); |
|
942 |
return 0; |
|
943 |
} |
|
944 |
||
945 |
||
946 |
||
947 |
static int |
|
948 |
inflate_free() |
|
949 |
{ |
|
950 |
if (fixed_td != (struct huft *)NULL) |
|
951 |
{ |
|
952 |
huft_free(fixed_td); |
|
953 |
fixed_td = (struct huft *)NULL; |
|
954 |
} |
|
955 |
||
956 |
if (fixed_tl != (struct huft *)NULL) |
|
957 |
{ |
|
958 |
huft_free(fixed_tl); |
|
959 |
fixed_tl = (struct huft *)NULL; |
|
960 |
} |
|
961 |
return 0; |
|
962 |
} |
|
963 |
||
964 |
||
965 |
static int |
|
966 |
stx_inflate(in, out) |
|
967 |
char *in, *out; |
|
968 |
{ |
|
969 |
int rslt; |
|
970 |
||
971 |
inPtr = in; |
|
972 |
outPtr = out; |
|
640 | 973 |
slide = (char *)malloc(WSIZE+2); |
637 | 974 |
if (! slide) return 1; |
636 | 975 |
|
976 |
rslt = inflate(); |
|
977 |
inflate_free(); |
|
637 | 978 |
free(slide); |
636 | 979 |
return rslt; |
980 |
} |
|
981 |
||
982 |
%} |
|
983 |
! ! |
|
984 |
||
622 | 985 |
!ZipArchive class methodsFor:'documentation'! |
986 |
||
624 | 987 |
copyright |
988 |
" |
|
989 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
636 | 990 |
All Rights Reserved |
624 | 991 |
|
992 |
This software is furnished under a license and may be used |
|
993 |
only in accordance with the terms of that license and with the |
|
994 |
inclusion of the above copyright notice. This software may not |
|
995 |
be provided or otherwise made available to, or used by, any |
|
996 |
other person. No title to or ownership of the software is |
|
997 |
hereby transferred. |
|
998 |
" |
|
999 |
||
1000 |
! |
|
622 | 1001 |
|
624 | 1002 |
documentation |
1003 |
" |
|
1004 |
provides access to a zip archive. |
|
1005 |
Caveat: only uncompressed archives are supported (for now). |
|
651 | 1006 |
|
1007 |
[author:] |
|
723 | 1008 |
Claus Gittinger |
624 | 1009 |
" |
1010 |
! |
|
1011 |
||
1012 |
examples |
|
1013 |
" |
|
636 | 1014 |
[exBegin] |
624 | 1015 |
|zip bytes| |
1016 |
||
1017 |
zip := ZipArchive oldFileNamed:'foo.zip'. |
|
1018 |
bytes := zip extract:'bar'. |
|
636 | 1019 |
[exEnd] |
1020 |
||
1021 |
[exBegin] |
|
1022 |
|zip bytes| |
|
1023 |
||
1024 |
zip := ZipArchive oldFileNamed:'source/stx/libbasic2.zip'. |
|
1025 |
zip entries do:[:entry | |
|
1026 |
Transcript showCR:entry |
|
1027 |
]. |
|
1028 |
[exEnd] |
|
1029 |
||
1030 |
[exBegin] |
|
1031 |
|zip bytes| |
|
1032 |
||
1033 |
zip := ZipArchive oldFileNamed:'source/stx/libbasic2.zip'. |
|
1034 |
bytes := zip extract:'TwoByteStr.st'. |
|
1035 |
Transcript showCR:(bytes asString). |
|
1036 |
[exEnd] |
|
624 | 1037 |
" |
622 | 1038 |
! ! |
1039 |
||
723 | 1040 |
!ZipArchive class methodsFor:'debugging'! |
1041 |
||
1042 |
debugTrace:aBoolean |
|
1043 |
%{ |
|
1044 |
if (aBoolean == true) { |
|
1045 |
debugTrace = 1; |
|
1046 |
} else { |
|
1047 |
debugTrace = 0; |
|
1048 |
} |
|
1049 |
%} |
|
1050 |
! ! |
|
1051 |
||
622 | 1052 |
!ZipArchive class methodsFor:'instance creation'! |
1053 |
||
1054 |
newFileNamed:name |
|
1055 |
^ self new name:name mode:#write |
|
1056 |
||
1057 |
"Created: / 29.3.1998 / 17:46:16 / cg" |
|
1058 |
! |
|
1059 |
||
1060 |
oldFileNamed:name |
|
635 | 1061 |
|zar f fn| |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1062 |
|
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1063 |
RecentlyUsedZipArchives isNil ifTrue:[ |
723 | 1064 |
RecentlyUsedZipArchives := OrderedCollection new |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1065 |
]. |
635 | 1066 |
f := name asFilename. |
1067 |
fn := f pathName. |
|
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1068 |
RecentlyUsedZipArchives keysAndValuesDo:[:i :z | |
723 | 1069 |
z name = fn ifTrue:[ |
1070 |
RecentlyUsedZipArchives removeIndex:i. |
|
1071 |
RecentlyUsedZipArchives addLast:z. |
|
1072 |
self installFlushBlock. |
|
1073 |
^ z |
|
1074 |
]. |
|
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1075 |
]. |
635 | 1076 |
zar := self new name:fn mode:#read. |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1077 |
RecentlyUsedZipArchives add:zar. |
701
90faf30c20eb
increased number of cached zipFiles
Claus Gittinger <cg@exept.de>
parents:
700
diff
changeset
|
1078 |
[RecentlyUsedZipArchives size > 15] whileTrue:[ |
723 | 1079 |
RecentlyUsedZipArchives removeFirst |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1080 |
]. |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1081 |
self installFlushBlock. |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1082 |
^ zar |
622 | 1083 |
|
1084 |
"Created: / 29.3.1998 / 17:46:09 / cg" |
|
701
90faf30c20eb
increased number of cached zipFiles
Claus Gittinger <cg@exept.de>
parents:
700
diff
changeset
|
1085 |
"Modified: / 20.10.1998 / 00:30:02 / cg" |
622 | 1086 |
! ! |
1087 |
||
1088 |
!ZipArchive class methodsFor:'class initialization'! |
|
1089 |
||
1090 |
initialize |
|
1091 |
ECREC_SIZE := 18. |
|
1092 |
LREC_SIZE := 26. |
|
1093 |
CREC_SIZE := 42. |
|
1094 |
||
1095 |
TOTAL_ENTRIES_CENTRAL_DIR := 10. |
|
1096 |
SIZE_CENTRAL_DIRECTORY := 12. |
|
1097 |
||
1098 |
C_COMPRESSED_SIZE := 16. |
|
1099 |
C_UNCOMPRESSED_SIZE := 20. |
|
1100 |
C_FILENAME_LENGTH := 24. |
|
1101 |
C_RELATIVE_OFFSET_LOCAL_HEADER := 38. |
|
1102 |
||
1103 |
"/ compression methods |
|
1104 |
COMPR_STORED := 0. |
|
1105 |
COMPR_SHRUNK := 1. |
|
1106 |
COMPR_REDUCED1 := 2. |
|
1107 |
COMPR_REDUCED2 := 3. |
|
1108 |
COMPR_REDUCED3 := 4. |
|
1109 |
COMPR_REDUCED4 := 5. |
|
1110 |
COMPR_IMPLODED := 6. |
|
1111 |
COMPR_TOKENIZED := 7. |
|
1112 |
COMPR_DEFLATED := 8. |
|
1113 |
||
1114 |
ZipFileFormatErrorSignal := Signal new. |
|
1115 |
||
1116 |
" |
|
1117 |
self initialize |
|
1118 |
" |
|
1119 |
||
1120 |
"Modified: / 29.3.1998 / 20:17:18 / cg" |
|
1121 |
! ! |
|
1122 |
||
629
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1123 |
!ZipArchive class methodsFor:'cleanup'! |
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1124 |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1125 |
installFlushBlock |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1126 |
"forget about cached zipArchives" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1127 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1128 |
FlushBlock isNil ifTrue:[ |
723 | 1129 |
FlushBlock := [ RecentlyUsedZipArchives := nil. FlushBlock := nil. ]. |
700
b1c6920cfef6
keep more zipArchive-dirs cached.
Claus Gittinger <cg@exept.de>
parents:
694
diff
changeset
|
1130 |
] ifFalse:[ |
723 | 1131 |
Processor removeTimedBlock:FlushBlock. |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1132 |
]. |
641 | 1133 |
Processor addTimedBlock:FlushBlock for:nil afterSeconds:60. |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1134 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1135 |
" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1136 |
self installFlushBlock |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1137 |
" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1138 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1139 |
"Created: / 9.4.1998 / 13:17:07 / cg" |
700
b1c6920cfef6
keep more zipArchive-dirs cached.
Claus Gittinger <cg@exept.de>
parents:
694
diff
changeset
|
1140 |
"Modified: / 19.10.1998 / 21:02:22 / cg" |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1141 |
! |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1142 |
|
629
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1143 |
lowSpaceCleanup |
635 | 1144 |
"forget about cached zipArchives" |
1145 |
||
629
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1146 |
RecentlyUsedZipArchives := nil |
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1147 |
|
635 | 1148 |
" |
1149 |
self lowSpaceCleanup |
|
1150 |
" |
|
1151 |
||
1152 |
"Modified: / 7.4.1998 / 17:58:57 / cg" |
|
629
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1153 |
! ! |
33fcaf60afa3
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
1154 |
|
622 | 1155 |
!ZipArchive class methodsFor:'constants'! |
1156 |
||
1157 |
LREC_SIZE |
|
1158 |
^ LREC_SIZE |
|
1159 |
||
1160 |
"Created: / 29.3.1998 / 19:11:20 / cg" |
|
1161 |
! ! |
|
1162 |
||
1163 |
!ZipArchive methodsFor:'accessing'! |
|
1164 |
||
1165 |
entries |
|
1166 |
"return a collection of fileName entries" |
|
1167 |
||
1168 |
|names| |
|
1169 |
||
1170 |
names := OrderedCollection new. |
|
1171 |
||
1172 |
self zipMembersDo:[:zipd | |
|
636 | 1173 |
names add:(zipd name) |
622 | 1174 |
]. |
1175 |
^ names |
|
1176 |
||
1177 |
" |
|
1178 |
(ZipArchive oldFileNamed:'/usr/lib/java/lib/classes.zip') entries |
|
1179 |
" |
|
1180 |
||
1181 |
"Modified: / 29.3.1998 / 20:08:38 / cg" |
|
1182 |
! |
|
1183 |
||
1184 |
extract:fileName |
|
636 | 1185 |
"extract a filename entry as a byteArray" |
1186 |
||
719 | 1187 |
|zmemb rawContents data oldEntry| |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1188 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1189 |
recentlyExtractedEntries isNil ifTrue:[ |
723 | 1190 |
recentlyExtractedEntries := OrderedCollection new |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1191 |
]. |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1192 |
recentlyExtractedEntries keysAndValuesDo:[:index :entry | |
723 | 1193 |
entry name = fileName ifTrue:[ |
1194 |
recentlyExtractedEntries removeIndex:index. |
|
1195 |
recentlyExtractedEntries addLast:entry. |
|
1196 |
data := entry data. |
|
1197 |
data notNil ifTrue:[ |
|
1198 |
^ data |
|
1199 |
] |
|
1200 |
] |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1201 |
]. |
622 | 1202 |
|
1203 |
zmemb := self findMember:fileName. |
|
625 | 1204 |
zmemb isNil ifTrue:[^ nil]. |
1205 |
||
1206 |
self openFile. |
|
622 | 1207 |
file position:(zmemb fileStart + 1). |
1208 |
rawContents := file nextBytes:(zmemb compressed_size). |
|
625 | 1209 |
self closeFile. |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1210 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1211 |
data := self |
723 | 1212 |
decode:rawContents |
1213 |
method:(zmemb compression_method) |
|
1214 |
size:(zmemb uncompressed_size). |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1215 |
|
719 | 1216 |
data size < (32*1024) ifTrue:[ |
1217 |
"/ zmemb data:data. |
|
723 | 1218 |
recentlyExtractedEntries addLast:zmemb. |
1219 |
[recentlyExtractedEntries size > 5] whileTrue:[ |
|
1220 |
oldEntry := recentlyExtractedEntries removeFirst. |
|
1221 |
oldEntry data:nil. |
|
1222 |
]. |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1223 |
]. |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1224 |
^ data. |
622 | 1225 |
|
1226 |
" |
|
1227 |
(ZipArchive oldFileNamed:'/usr/lib/java/lib/classes.zip') extract:'java/io/UTFDataFormatException.class' |
|
1228 |
" |
|
1229 |
||
719 | 1230 |
"Modified: / 10.1.1999 / 17:43:25 / cg" |
622 | 1231 |
! |
1232 |
||
1233 |
members |
|
1234 |
"return a collection of members" |
|
1235 |
||
1236 |
|members| |
|
1237 |
||
1238 |
members := OrderedCollection new. |
|
1239 |
||
1240 |
self zipMembersDo:[:zipd | |
|
636 | 1241 |
members add:zipd |
622 | 1242 |
]. |
1243 |
^ members |
|
1244 |
||
1245 |
" |
|
1246 |
(ZipArchive oldFileNamed:'/usr/lib/java/lib/classes.zip') members |
|
1247 |
" |
|
1248 |
||
1249 |
"Created: / 29.3.1998 / 20:09:27 / cg" |
|
1250 |
"Modified: / 29.3.1998 / 20:10:21 / cg" |
|
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1251 |
! |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1252 |
|
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1253 |
name |
636 | 1254 |
"return the (file-)name of this zipArchive" |
1255 |
||
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1256 |
^ archiveName |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1257 |
|
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1258 |
"Created: / 6.4.1998 / 10:47:11 / cg" |
622 | 1259 |
! ! |
1260 |
||
1261 |
!ZipArchive methodsFor:'private'! |
|
1262 |
||
625 | 1263 |
closeFile |
717 | 1264 |
file notNil ifTrue:[ |
723 | 1265 |
file close. |
1266 |
file := nil. |
|
717 | 1267 |
] |
625 | 1268 |
|
1269 |
"Created: / 30.3.1998 / 18:18:10 / cg" |
|
717 | 1270 |
"Modified: / 29.12.1998 / 23:08:27 / cg" |
625 | 1271 |
! |
1272 |
||
624 | 1273 |
name:nm mode:m |
625 | 1274 |
archiveName := nm asFilename name. |
624 | 1275 |
mode := m. |
1276 |
||
622 | 1277 |
mode == #read ifTrue:[ |
636 | 1278 |
self openFile. |
1279 |
self readDirectory. |
|
1280 |
self closeFile. |
|
622 | 1281 |
] ifFalse:[ |
636 | 1282 |
"/ self openFile. |
622 | 1283 |
] |
1284 |
||
1285 |
" |
|
1286 |
ZipArchive oldFileNamed:'/usr/lib/java/lib/classes.zip' |
|
1287 |
(ZipArchive oldFileNamed:'/usr/lib/java/lib/classes.zip') entries |
|
1288 |
(ZipArchive oldFileNamed:'/usr/lib/java/lib/classes.zip') extract:'java/io/UTFDataFormatException.class' |
|
1289 |
" |
|
1290 |
||
625 | 1291 |
"Modified: / 30.3.1998 / 18:19:48 / cg" |
1292 |
! |
|
1293 |
||
1294 |
openFile |
|
1295 |
file isNil ifTrue:[ |
|
636 | 1296 |
mode == #read ifTrue:[ |
1297 |
file := archiveName asFilename readStream binary. |
|
1298 |
] ifFalse:[ |
|
1299 |
file := archiveName asFilename writeStream binary |
|
1300 |
] |
|
625 | 1301 |
]. |
1302 |
||
1303 |
"Created: / 30.3.1998 / 18:18:48 / cg" |
|
1304 |
"Modified: / 30.3.1998 / 18:19:26 / cg" |
|
622 | 1305 |
! ! |
1306 |
||
1307 |
!ZipArchive methodsFor:'private - decompression'! |
|
1308 |
||
636 | 1309 |
decode:rawBytes method:compressionMethod size:uncompressedSize |
1310 |
|outBytes| |
|
1311 |
||
622 | 1312 |
compressionMethod == COMPR_STORED ifTrue:[ |
636 | 1313 |
"/ |
1314 |
"/ uncompressed |
|
1315 |
"/ |
|
1316 |
^ rawBytes |
|
622 | 1317 |
]. |
1318 |
||
636 | 1319 |
compressionMethod == COMPR_DEFLATED ifTrue:[ |
1320 |
"/ |
|
1321 |
"/ deflate/inflate algorithm |
|
1322 |
"/ |
|
1323 |
outBytes := ByteArray new:uncompressedSize. |
|
1324 |
^ self inflate:rawBytes to:outBytes |
|
1325 |
]. |
|
1326 |
||
1327 |
"/ |
|
1328 |
"/ the other algorithms are not (yet) supported |
|
1329 |
"/ |
|
622 | 1330 |
compressionMethod == COMPR_SHRUNK ifTrue:[ |
636 | 1331 |
self error:'unsupported compression method: SHRUNK'. |
1332 |
^ nil |
|
622 | 1333 |
]. |
1334 |
compressionMethod == COMPR_REDUCED1 ifTrue:[ |
|
636 | 1335 |
self error:'unsupported compression method: REDUCED1'. |
1336 |
^ nil |
|
622 | 1337 |
]. |
1338 |
compressionMethod == COMPR_REDUCED2 ifTrue:[ |
|
636 | 1339 |
self error:'unsupported compression method: REDUCED2'. |
1340 |
^ nil |
|
622 | 1341 |
]. |
1342 |
compressionMethod == COMPR_REDUCED3 ifTrue:[ |
|
636 | 1343 |
self error:'unsupported compression method: REDUCED3'. |
1344 |
^ nil |
|
622 | 1345 |
]. |
1346 |
compressionMethod == COMPR_REDUCED4 ifTrue:[ |
|
636 | 1347 |
self error:'unsupported compression method: REDUCED4'. |
1348 |
^ nil |
|
622 | 1349 |
]. |
1350 |
compressionMethod == COMPR_IMPLODED ifTrue:[ |
|
636 | 1351 |
self error:'unsupported compression method: IMPLODED'. |
1352 |
^ nil |
|
622 | 1353 |
]. |
1354 |
compressionMethod == COMPR_TOKENIZED ifTrue:[ |
|
636 | 1355 |
self error:'unsupported compression method: TOKENIZED'. |
1356 |
^ nil |
|
622 | 1357 |
]. |
1358 |
||
1359 |
self error:'unsupported compression method'. |
|
1360 |
^ nil |
|
1361 |
||
1362 |
"Created: / 29.3.1998 / 20:14:45 / cg" |
|
636 | 1363 |
"Modified: / 8.4.1998 / 10:31:34 / cg" |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1364 |
! |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1365 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1366 |
inflate:inBytes to:outBytes |
723 | 1367 |
|inflateReturnCode| |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1368 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1369 |
%{ /* STACK:32768 */ |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1370 |
if (__isByteArray(inBytes) |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1371 |
&& __isByteArray(outBytes)) { |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1372 |
char *in, *out; |
723 | 1373 |
int rc; |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1374 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1375 |
in = __ByteArrayInstPtr(inBytes)->ba_element; |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1376 |
out = __ByteArrayInstPtr(outBytes)->ba_element; |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1377 |
|
723 | 1378 |
if ((rc = stx_inflate(in, out)) == 0) { |
1379 |
RETURN (outBytes); |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1380 |
} |
723 | 1381 |
inflateReturnCode = __MKSMALLINT(rc); |
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1382 |
} |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1383 |
%}. |
723 | 1384 |
inflateReturnCode notNil ifTrue:[ |
1385 |
"/ bad blockType 2 |
|
1386 |
self error:'inflate error: ' , inflateReturnCode printString |
|
1387 |
]. |
|
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1388 |
^ nil. |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1389 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1390 |
"Created: / 8.4.1998 / 10:31:27 / cg" |
622 | 1391 |
! ! |
1392 |
||
1393 |
!ZipArchive methodsFor:'private - directory stuff'! |
|
1394 |
||
1395 |
addMember |
|
624 | 1396 |
"add a zipMember" |
1397 |
||
622 | 1398 |
|zmemb | |
1399 |
||
691
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1400 |
self addMember:(zmemb := ZipMember new). |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1401 |
^ zmemb. |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1402 |
|
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1403 |
"Created: / 29.3.1998 / 18:22:25 / cg" |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1404 |
"Modified: / 9.9.1998 / 20:33:32 / cg" |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1405 |
! |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1406 |
|
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1407 |
addMember:zmemb |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1408 |
"add a zipMember" |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1409 |
|
622 | 1410 |
(firstEntry == nil) ifTrue:[ |
723 | 1411 |
firstEntry := zmemb |
622 | 1412 |
] ifFalse:[ |
723 | 1413 |
lastEntry next:zmemb. |
622 | 1414 |
]. |
1415 |
lastEntry := zmemb. |
|
1416 |
^ zmemb. |
|
1417 |
||
624 | 1418 |
"Modified: / 30.3.1998 / 17:13:20 / cg" |
691
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1419 |
"Created: / 9.9.1998 / 20:33:06 / cg" |
622 | 1420 |
! |
1421 |
||
1422 |
findMember:name |
|
624 | 1423 |
"find a zipMember by name" |
1424 |
||
622 | 1425 |
self zipMembersDo:[:zipd | |
636 | 1426 |
(zipd name = name) ifTrue:[^ zipd]. |
622 | 1427 |
]. |
1428 |
^ nil |
|
1429 |
||
624 | 1430 |
"Modified: / 30.3.1998 / 17:13:30 / cg" |
622 | 1431 |
! |
1432 |
||
1433 |
readDirectory |
|
624 | 1434 |
"read the zip directory into a linked-list of zipMembers" |
1435 |
||
681
58fcd6034921
search for, if PK-magic is not found at expected file position.
Claus Gittinger <cg@exept.de>
parents:
651
diff
changeset
|
1436 |
|size count_in dir_size foundPK pos0| |
622 | 1437 |
|
1438 |
size := file fileSize. |
|
1439 |
(size == 0) ifTrue:[ |
|
723 | 1440 |
count_in := 0. |
1441 |
^ self |
|
622 | 1442 |
]. |
1443 |
||
1444 |
(size < (ECREC_SIZE+4)) ifTrue:[ |
|
723 | 1445 |
^ ZipFileFormatErrorSignal raiseWith:'zipfile too short'. |
622 | 1446 |
]. |
1447 |
||
681
58fcd6034921
search for, if PK-magic is not found at expected file position.
Claus Gittinger <cg@exept.de>
parents:
651
diff
changeset
|
1448 |
file position:(pos0 := size - ECREC_SIZE - 4 + 1). |
625 | 1449 |
((file next ~~ ($P asciiValue)) |
1450 |
or:[file next ~~ ($K asciiValue) |
|
1451 |
or:[file next ~~ 8r005 |
|
1452 |
or:[file next ~~ 8r006]]]) ifTrue:[ |
|
723 | 1453 |
"/ search for PK ... |
1454 |
file position:1. "/(pos0 - 100). |
|
1455 |
[file atEnd not and:[foundPK not]] whileTrue:[ |
|
1456 |
(file next == ($P asciiValue) |
|
1457 |
and:[file next == ($K asciiValue) |
|
1458 |
and:[file next == 8r005 |
|
1459 |
and:[file next == 8r006]]]) ifTrue:[ |
|
1460 |
foundPK := true. |
|
1461 |
pos0 := file position - 4. |
|
1462 |
] |
|
1463 |
]. |
|
1464 |
foundPK ifTrue:[ |
|
1465 |
'ZipArchive [warning]: funny format; resynchronized'. |
|
1466 |
] ifFalse:[ |
|
1467 |
^ ZipFileFormatErrorSignal raiseWith:'not a valid zipfile'. |
|
1468 |
] |
|
622 | 1469 |
]. |
1470 |
||
1471 |
file skip: (TOTAL_ENTRIES_CENTRAL_DIR - 4). |
|
1472 |
||
700
b1c6920cfef6
keep more zipArchive-dirs cached.
Claus Gittinger <cg@exept.de>
parents:
694
diff
changeset
|
1473 |
count_in := file nextUnsignedShortMSB:false. "/ Get TOTAL_ENTRIES_CENTRAL_DIR |
624 | 1474 |
dir_size := file nextLongMSB:false. "/ Get SIZE_CENTRAL_DIRECTORY |
681
58fcd6034921
search for, if PK-magic is not found at expected file position.
Claus Gittinger <cg@exept.de>
parents:
651
diff
changeset
|
1475 |
file position:(pos0 - dir_size ). |
622 | 1476 |
|
691
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1477 |
file signalAtEnd:true. |
5c5cf54ead71
handle errors more gracefully
Claus Gittinger <cg@exept.de>
parents:
681
diff
changeset
|
1478 |
|
622 | 1479 |
0 to:(count_in-1) do:[:i | |
723 | 1480 |
|zipd filename_length s| |
622 | 1481 |
|
723 | 1482 |
zipd := ZipMember new. |
1483 |
file skip:(4+C_COMPRESSED_SIZE-4-2-2-2). |
|
1484 |
zipd compression_method:(file nextUnsignedShortMSB:false). "/ Get compression method |
|
1485 |
(file nextUnsignedShortMSB:false) isNil ifTrue:[ "/ skip last_mod_file_time |
|
1486 |
self warn:'file format error or short file'. |
|
1487 |
^ self. |
|
1488 |
]. |
|
1489 |
(file nextUnsignedShortMSB:false). "/ skip last_mod_file_date |
|
1490 |
zipd crc32:(file nextLongMSB:false). "/ Get crc32 |
|
1491 |
zipd compressed_size:(file nextLongMSB:false). "/ Get C_COMPRESSED_SIZE |
|
1492 |
zipd uncompressed_size:(file nextLongMSB:false). "/ Get C_UNCOMPRESSED_SIZE |
|
1493 |
filename_length := (file nextUnsignedShortMSB:false). "/ Get C_FILENAME_LENGTH |
|
1494 |
file skip:(C_RELATIVE_OFFSET_LOCAL_HEADER-(C_FILENAME_LENGTH+2)). |
|
1495 |
zipd relative_offset_local_header:(file nextLongMSB:false). |
|
1496 |
zipd name:(s := String new:filename_length). |
|
1497 |
file nextBytes:filename_length into:s. |
|
700
b1c6920cfef6
keep more zipArchive-dirs cached.
Claus Gittinger <cg@exept.de>
parents:
694
diff
changeset
|
1498 |
|
723 | 1499 |
self addMember:zipd. |
622 | 1500 |
] |
1501 |
||
700
b1c6920cfef6
keep more zipArchive-dirs cached.
Claus Gittinger <cg@exept.de>
parents:
694
diff
changeset
|
1502 |
"Modified: / 19.10.1998 / 21:27:32 / cg" |
622 | 1503 |
! |
1504 |
||
1505 |
zipMembersDo:aBlock |
|
624 | 1506 |
"evaluate aBlock for all zipMembers" |
1507 |
||
622 | 1508 |
|zipd| |
1509 |
||
1510 |
zipd := firstEntry. |
|
1511 |
[zipd notNil] whileTrue:[ |
|
636 | 1512 |
aBlock value:zipd. |
1513 |
zipd := zipd next |
|
622 | 1514 |
]. |
1515 |
||
1516 |
"Created: / 29.3.1998 / 19:15:15 / cg" |
|
624 | 1517 |
"Modified: / 30.3.1998 / 17:13:47 / cg" |
622 | 1518 |
! ! |
1519 |
||
651 | 1520 |
!ZipArchive::ZipMember class methodsFor:'documentation'! |
1521 |
||
1522 |
documentation |
|
1523 |
" |
|
1524 |
keeps some information for a single entry in a zipFile. |
|
1525 |
" |
|
1526 |
||
1527 |
||
1528 |
! ! |
|
1529 |
||
622 | 1530 |
!ZipArchive::ZipMember methodsFor:'accessing'! |
1531 |
||
1532 |
compressed_size |
|
1533 |
"return the value of the instance variable 'compressed_size' (automatically generated)" |
|
1534 |
||
1535 |
^ compressed_size |
|
1536 |
||
1537 |
"Created: / 29.3.1998 / 18:28:03 / cg" |
|
1538 |
! |
|
1539 |
||
1540 |
compressed_size:something |
|
1541 |
"set the value of the instance variable 'compressed_size' (automatically generated)" |
|
1542 |
||
1543 |
compressed_size := something. |
|
1544 |
||
1545 |
"Created: / 29.3.1998 / 18:28:03 / cg" |
|
1546 |
! |
|
1547 |
||
1548 |
compression_method |
|
1549 |
"return the value of the instance variable 'compression_method' (automatically generated)" |
|
1550 |
||
1551 |
^ compression_method |
|
1552 |
||
1553 |
"Created: / 29.3.1998 / 20:02:57 / cg" |
|
1554 |
! |
|
1555 |
||
1556 |
compression_method:something |
|
1557 |
"set the value of the instance variable 'compression_method' (automatically generated)" |
|
1558 |
||
1559 |
compression_method := something. |
|
1560 |
||
1561 |
"Created: / 29.3.1998 / 20:02:57 / cg" |
|
1562 |
! |
|
1563 |
||
1564 |
crc32 |
|
1565 |
"return the value of the instance variable 'crc32' (automatically generated)" |
|
1566 |
||
1567 |
^ crc32 |
|
1568 |
||
1569 |
"Created: / 29.3.1998 / 20:03:00 / cg" |
|
1570 |
! |
|
1571 |
||
1572 |
crc32:something |
|
1573 |
"set the value of the instance variable 'crc32' (automatically generated)" |
|
1574 |
||
1575 |
crc32 := something. |
|
1576 |
||
1577 |
"Created: / 29.3.1998 / 20:03:00 / cg" |
|
1578 |
! |
|
1579 |
||
638
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1580 |
data |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1581 |
"return the value of the instance variable 'data' (automatically generated)" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1582 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1583 |
^ data |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1584 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1585 |
"Created: / 9.4.1998 / 13:05:03 / cg" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1586 |
! |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1587 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1588 |
data:something |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1589 |
"set the value of the instance variable 'data' (automatically generated)" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1590 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1591 |
data := something. |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1592 |
|
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1593 |
"Created: / 9.4.1998 / 13:05:03 / cg" |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1594 |
! |
ff61e852f5c3
flush cached data after some time (1 minute)
Claus Gittinger <cg@exept.de>
parents:
637
diff
changeset
|
1595 |
|
622 | 1596 |
name |
1597 |
"return the value of the instance variable 'name' (automatically generated)" |
|
1598 |
||
1599 |
^ name |
|
1600 |
||
1601 |
"Created: / 29.3.1998 / 18:29:22 / cg" |
|
1602 |
! |
|
1603 |
||
1604 |
name:something |
|
1605 |
"set the value of the instance variable 'name' (automatically generated)" |
|
1606 |
||
1607 |
name := something. |
|
1608 |
||
1609 |
"Created: / 29.3.1998 / 18:29:22 / cg" |
|
1610 |
! |
|
1611 |
||
1612 |
next |
|
1613 |
"return the value of the instance variable 'next' (automatically generated)" |
|
1614 |
||
1615 |
^ next |
|
1616 |
||
1617 |
"Created: / 29.3.1998 / 18:29:42 / cg" |
|
1618 |
! |
|
1619 |
||
1620 |
next:something |
|
1621 |
"set the value of the instance variable 'next' (automatically generated)" |
|
1622 |
||
1623 |
next := something. |
|
1624 |
||
1625 |
"Created: / 29.3.1998 / 18:29:42 / cg" |
|
1626 |
! |
|
1627 |
||
1628 |
relative_offset_local_header |
|
1629 |
"return the value of the instance variable 'relative_offset_local_header' (automatically generated)" |
|
1630 |
||
1631 |
^ relative_offset_local_header |
|
1632 |
||
1633 |
"Created: / 29.3.1998 / 18:28:40 / cg" |
|
1634 |
! |
|
1635 |
||
1636 |
relative_offset_local_header:something |
|
1637 |
"set the value of the instance variable 'relative_offset_local_header' (automatically generated)" |
|
1638 |
||
1639 |
relative_offset_local_header := something. |
|
1640 |
||
1641 |
"Created: / 29.3.1998 / 18:28:40 / cg" |
|
1642 |
! |
|
1643 |
||
1644 |
uncompressed_size |
|
1645 |
"return the value of the instance variable 'uncompressed_size' (automatically generated)" |
|
1646 |
||
1647 |
^ uncompressed_size |
|
1648 |
||
1649 |
"Created: / 29.3.1998 / 18:28:21 / cg" |
|
1650 |
! |
|
1651 |
||
1652 |
uncompressed_size:something |
|
1653 |
"set the value of the instance variable 'uncompressed_size' (automatically generated)" |
|
1654 |
||
1655 |
uncompressed_size := something. |
|
1656 |
||
1657 |
"Created: / 29.3.1998 / 18:28:21 / cg" |
|
1658 |
! ! |
|
1659 |
||
1660 |
!ZipArchive::ZipMember methodsFor:'printing & storing'! |
|
1661 |
||
1662 |
displayString |
|
634 | 1663 |
^ 'ZipMember(' , (name ? '*nil*') , ')' |
622 | 1664 |
|
1665 |
"Created: / 29.3.1998 / 20:10:07 / cg" |
|
634 | 1666 |
"Modified: / 2.4.1998 / 15:10:08 / cg" |
622 | 1667 |
! ! |
1668 |
||
1669 |
!ZipArchive::ZipMember methodsFor:'queries'! |
|
1670 |
||
1671 |
fileStart |
|
1672 |
^ relative_offset_local_header + ZipArchive LREC_SIZE + 4 + name size |
|
1673 |
||
1674 |
"Created: / 29.3.1998 / 19:10:57 / cg" |
|
1675 |
! ! |
|
1676 |
||
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1677 |
!ZipArchive class methodsFor:'documentation'! |
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1678 |
|
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1679 |
version |
726
5b9f13c73b31
renamed __new() to __STX___new()
Claus Gittinger <cg@exept.de>
parents:
723
diff
changeset
|
1680 |
^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.25 1999-02-12 14:57:38 cg Exp $' |
628
27102a6bd3f7
remember recently visited zipArchives for faster access
Claus Gittinger <cg@exept.de>
parents:
625
diff
changeset
|
1681 |
! ! |
622 | 1682 |
ZipArchive initialize! |