11 #define FORCE_LOWER 1 /* force filenames to lower case */
12 #define MUNGE_CR 1 /* replace '\r\n' with ' \n' */
13 #define High64 (1LL<<63)
16 * File system for zip archives (read-only)
20 IS_MSDOS = 0, /* creator OS (interpretation of external flags) */
21 IS_RDONLY = 1, /* file was readonly (external flags) */
22 IS_TEXT = 1 /* file was text (internal flags) */
25 typedef struct Block Block;
35 static int findCDir(Biobuf *);
36 static int header(Biobuf *, ZipHead *);
37 static int cheader(Biobuf *, ZipHead *);
38 /* static void trailer(Biobuf *, ZipHead *); */
39 static char *getname(Biobuf *, int);
40 static int blwrite(void *, void *, int);
41 static ulong get4(Biobuf *);
42 static int get2(Biobuf *);
43 static int get1(Biobuf *);
44 static long msdos2time(int, int);
54 crctab = mkcrctab(ZCrcPoly);
57 sysfatal("inflateinit failed: %s", flateerr(ok));
59 bin = Bopen(name, OREAD);
61 error("Can't open argument file");
63 entries = findCDir(bin);
65 sysfatal("empty file");
68 memset(&zh, 0, sizeof(zh));
69 if(!cheader(bin, &zh))
72 if(zh.iattr & IS_TEXT)
74 f.mode = (zh.madevers == IS_MSDOS && zh.eattr & IS_RDONLY)? 0444: 0644;
75 if (zh.meth == 0 && zh.uncsize == 0){
76 p = strchr(zh.file, '\0');
77 if(p > zh.file && p[-1] == '/')
78 f.mode |= (DMDIR | 0111);
83 f.mdate = msdos2time(zh.modtime, zh.moddate);
84 f.name = zh.file + ((zh.file[0] == '/')? 1: 0);
104 doread(Ram *r, vlong off, long cnt)
110 static char buf[Maxbuf];
111 static uchar *cache = nil;
114 sysfatal("file too big (>%d)", Maxbuf);
116 if (Bseek(bin, r->addr & 0x7FFFFFFFFFFFFFFFLL, 0) < 0)
117 sysfatal("seek failed");
119 memset(&zh, 0, sizeof(zh));
120 if (!header(bin, &zh))
121 sysfatal("cannot get local header");
125 if (Bseek(bin, off, 1) < 0)
126 sysfatal("seek failed");
127 if (Bread(bin, buf, cnt) != cnt)
128 sysfatal("read failed");
131 if (r->qid.path != oqid.path){
135 cache = emalloc(r->ndata);
138 bs.limit = cache+r->ndata;
139 if ((err = inflate(&bs, blwrite, bin, (int(*)(void*))Bgetc)) != FlateOk)
140 sysfatal("inflate failed - %s", flateerr(err));
142 if (blockcrc(crctab, crc, cache, r->ndata) != zh.crc)
143 fprint(2, "%s - crc failed", r->name);
145 if ((r->addr & High64) && MUNGE_CR){
146 for (i = 0; i < r->ndata -1; i++)
147 if (cache[i] == '\r' && cache[i +1] == '\n')
151 memcpy(buf, cache+off, cnt);
154 sysfatal("%d - unsupported compression method", zh.meth);
168 dowrite(Ram *r, char *buf, long off, long cnt)
170 USED(r); USED(buf); USED(off); USED(cnt);
180 /*************************************************/
183 findCDir(Biobuf *bin)
189 ecoff = Bseek(bin, -ZECHeadSize, 2);
191 sysfatal("can't seek to header");
193 if(get4(bin) != ZECHeader)
194 sysfatal("bad magic number on directory");
206 if(Bseek(bin, off, 0) != off)
207 sysfatal("can't seek to contents");
214 header(Biobuf *bin, ZipHead *zh)
223 sysfatal("bad magic on local header");
225 zh->extvers = get1(bin);
226 zh->extos = get1(bin);
227 zh->flags = get2(bin);
228 zh->meth = get2(bin);
229 zh->modtime = get2(bin);
230 zh->moddate = get2(bin);
232 zh->csize = get4(bin);
233 zh->uncsize = get4(bin);
237 zh->file = getname(bin, flen);
245 cheader(Biobuf *bin, ZipHead *zh)
248 int flen, xlen, fclen;
254 sysfatal("bad magic number in file");
256 zh->madevers = get1(bin);
257 zh->madeos = get1(bin);
258 zh->extvers = get1(bin);
259 zh->extos = get1(bin);
260 zh->flags = get2(bin);
261 zh->meth = get2(bin);
262 zh->modtime = get2(bin);
263 zh->moddate = get2(bin);
265 zh->csize = get4(bin);
266 zh->uncsize = get4(bin);
270 get2(bin); /* disk number start */
271 zh->iattr = get2(bin); /* 1 == is-text-file */
272 zh->eattr = get4(bin); /* 1 == readonly-file */
275 zh->file = getname(bin, flen);
287 blwrite(void *vb, void *buf, int n)
290 if(n > b->limit - b->pos)
291 n = b->limit - b->pos;
292 memmove(b->pos, buf, n);
299 trailer(Biobuf *bin, ZipHead *zh)
301 if(zh->flags & ZTrailInfo){
303 zh->csize = get4(bin);
304 zh->uncsize = get4(bin);
310 getname(Biobuf *bin, int len)
315 s = emalloc(len + 1);
316 for(i = 0; i < len; i++){
334 for(i = 0; i < 4; i++){
337 sysfatal("unexpected eof");
349 for(i = 0; i < 2; i++){
352 sysfatal("unexpected eof");
365 sysfatal("unexpected eof");
370 msdos2time(int time, int date)
374 tm.hour = time >> 11;
375 tm.min = (time >> 5) & 63;
376 tm.sec = (time & 31) << 1;
377 tm.year = 80 + (date >> 9);
378 tm.mon = ((date >> 5) & 15) - 1;