aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/tapefs/zipfs.c
blob: 8725e0c94fa847352dfe3a25daabf1ecf7ee4e40 (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <flate.h>
#include <auth.h>
#include <fcall.h>
#include <ctype.h>
#include "tapefs.h"
#include "zip.h"

#define FORCE_LOWER	1	/* force filenames to lower case */
#define MUNGE_CR	1	/* replace '\r\n' with ' \n' */
#define High64 (1LL<<63)

/*
 * File system for zip archives (read-only)
 */

enum {
	IS_MSDOS = 0,	/* creator OS (interpretation of external flags) */
	IS_RDONLY = 1,	/* file was readonly (external flags) */
	IS_TEXT = 1	/* file was text  (internal flags) */
};

typedef struct Block Block;
struct Block{
	uchar *pos;
	uchar *limit;
};

static Biobuf *bin;
static u32int *crctab;
static ulong crc;

static int findCDir(Biobuf *);
static int header(Biobuf *, ZipHead *);
static int cheader(Biobuf *, ZipHead *);
/* static void trailer(Biobuf *, ZipHead *); */
static char *getname(Biobuf *, int);
static int blwrite(void *, void *, int);
static ulong get4(Biobuf *);
static int get2(Biobuf *);
static int get1(Biobuf *);
static long msdos2time(int, int);

void
populate(char *name)
{
	char *p;
	Fileinf f;
	ZipHead zh;
	int ok, entries;

	crctab = mkcrctab(ZCrcPoly);
	ok = inflateinit();
	if(ok != FlateOk)
		sysfatal("inflateinit failed: %s", flateerr(ok));

	bin = Bopen(name, OREAD);
	if (bin == nil)
		error("Can't open argument file");

	entries = findCDir(bin);
	if(entries < 0)
		sysfatal("empty file");

	while(entries-- > 0){
		memset(&zh, 0, sizeof(zh));
		if(!cheader(bin, &zh))
			break;
		f.addr = zh.off;
		if(zh.iattr & IS_TEXT)
			f.addr |= High64;
		f.mode = (zh.madevers == IS_MSDOS && zh.eattr & IS_RDONLY)? 0444: 0644;
		if (zh.meth == 0 && zh.uncsize == 0){
			p = strchr(zh.file, '\0');
			if(p > zh.file && p[-1] == '/')
				f.mode |= (DMDIR | 0111);
		}
		f.uid = 0;
		f.gid = 0;
		f.size = zh.uncsize;
		f.mdate = msdos2time(zh.modtime, zh.moddate);
		f.name = zh.file + ((zh.file[0] == '/')? 1: 0);
		poppath(f, 1);
		free(zh.file);
	}
	return ;
}

void
dotrunc(Ram *r)
{
	USED(r);
}

void
docreate(Ram *r)
{
	USED(r);
}

char *
doread(Ram *r, vlong off, long cnt)
{
	int i, err;
	Block bs;
	ZipHead zh;
	static Qid oqid;
	static char buf[Maxbuf];
	static uchar *cache = nil;

	if (cnt > Maxbuf)
		sysfatal("file too big (>%d)", Maxbuf);

	if (Bseek(bin, r->addr & 0x7FFFFFFFFFFFFFFFLL, 0) < 0)
		sysfatal("seek failed");

	memset(&zh, 0, sizeof(zh));
	if (!header(bin, &zh))
		sysfatal("cannot get local header");

	switch(zh.meth){
	case 0:
		if (Bseek(bin, off, 1) < 0)
			sysfatal("seek failed");
		if (Bread(bin, buf, cnt) != cnt)
			sysfatal("read failed");
		break;
	case 8:
		if (r->qid.path != oqid.path){
			oqid = r->qid;
			if (cache)
				free(cache);
			cache = emalloc(r->ndata);

			bs.pos = cache;
			bs.limit = cache+r->ndata;
			if ((err = inflate(&bs, blwrite, bin, (int(*)(void*))Bgetc)) != FlateOk)
				sysfatal("inflate failed - %s", flateerr(err));

			if (blockcrc(crctab, crc, cache, r->ndata) != zh.crc)
				fprint(2, "%s - crc failed", r->name);

			if ((r->addr & High64) && MUNGE_CR){
				for (i = 0; i < r->ndata -1; i++)
					if (cache[i] == '\r' && cache[i +1] == '\n')
						cache[i] = ' ';
			}
		}
		memcpy(buf, cache+off, cnt);
		break;
	default:
		sysfatal("%d - unsupported compression method", zh.meth);
		break;
	}

	return buf;
}

void
popdir(Ram *r)
{
	USED(r);
}

void
dowrite(Ram *r, char *buf, long off, long cnt)
{
	USED(r); USED(buf); USED(off); USED(cnt);
}

int
dopermw(Ram *r)
{
	USED(r);
	return 0;
}

/*************************************************/

static int
findCDir(Biobuf *bin)
{
	vlong ecoff;
	long off;
	int entries, zclen;

	ecoff = Bseek(bin, -ZECHeadSize, 2);
	if(ecoff < 0)
		sysfatal("can't seek to header");

	if(get4(bin) != ZECHeader)
		sysfatal("bad magic number on directory");

	get2(bin);
	get2(bin);
	get2(bin);
	entries = get2(bin);
	get4(bin);
	off = get4(bin);
	zclen = get2(bin);
	while(zclen-- > 0)
		get1(bin);

	if(Bseek(bin, off, 0) != off)
		sysfatal("can't seek to contents");

	return entries;
}


static int
header(Biobuf *bin, ZipHead *zh)
{
	ulong v;
	int flen, xlen;

	v = get4(bin);
	if(v != ZHeader){
		if(v == ZCHeader)
			return 0;
		sysfatal("bad magic on local header");
	}
	zh->extvers = get1(bin);
	zh->extos = get1(bin);
	zh->flags = get2(bin);
	zh->meth = get2(bin);
	zh->modtime = get2(bin);
	zh->moddate = get2(bin);
	zh->crc = get4(bin);
	zh->csize = get4(bin);
	zh->uncsize = get4(bin);
	flen = get2(bin);
	xlen = get2(bin);

	zh->file = getname(bin, flen);

	while(xlen-- > 0)
		get1(bin);
	return 1;
}

static int
cheader(Biobuf *bin, ZipHead *zh)
{
	ulong v;
	int flen, xlen, fclen;

	v = get4(bin);
	if(v != ZCHeader){
		if(v == ZECHeader)
			return 0;
		sysfatal("bad magic number in file");
	}
	zh->madevers = get1(bin);
	zh->madeos = get1(bin);
	zh->extvers = get1(bin);
	zh->extos = get1(bin);
	zh->flags = get2(bin);
	zh->meth = get2(bin);
	zh->modtime = get2(bin);
	zh->moddate = get2(bin);
	zh->crc = get4(bin);
	zh->csize = get4(bin);
	zh->uncsize = get4(bin);
	flen = get2(bin);
	xlen = get2(bin);
	fclen = get2(bin);
	get2(bin);		/* disk number start */
	zh->iattr = get2(bin);	/* 1 == is-text-file */
	zh->eattr = get4(bin);	/* 1 == readonly-file */
	zh->off = get4(bin);

	zh->file = getname(bin, flen);

	while(xlen-- > 0)
		get1(bin);

	while(fclen-- > 0)
		get1(bin);

	return 1;
}

static int
blwrite(void *vb, void *buf, int n)
{
	Block *b = vb;
	if(n > b->limit - b->pos)
		n = b->limit - b->pos;
	memmove(b->pos, buf, n);
	b->pos += n;
	return n;
}

/*
static void
trailer(Biobuf *bin, ZipHead *zh)
{
	if(zh->flags & ZTrailInfo){
		zh->crc = get4(bin);
		zh->csize = get4(bin);
		zh->uncsize = get4(bin);
	}
}
*/

static char*
getname(Biobuf *bin, int len)
{
	char *s;
	int i, c;

	s = emalloc(len + 1);
	for(i = 0; i < len; i++){
		c = get1(bin);
		if(FORCE_LOWER)
			c = tolower(c);
		s[i] = c;
	}
	s[i] = '\0';
	return s;
}


static ulong
get4(Biobuf *b)
{
	ulong v;
	int i, c;

	v = 0;
	for(i = 0; i < 4; i++){
		c = Bgetc(b);
		if(c < 0)
			sysfatal("unexpected eof");
		v |= c << (i * 8);
	}
	return v;
}

static int
get2(Biobuf *b)
{
	int i, c, v;

	v = 0;
	for(i = 0; i < 2; i++){
		c = Bgetc(b);
		if(c < 0)
			sysfatal("unexpected eof");
		v |= c << (i * 8);
	}
	return v;
}

static int
get1(Biobuf *b)
{
	int c;

	c = Bgetc(b);
	if(c < 0)
		sysfatal("unexpected eof");
	return c;
}

static long
msdos2time(int time, int date)
{
	Tm tm;

	tm.hour = time >> 11;
	tm.min = (time >> 5) & 63;
	tm.sec = (time & 31) << 1;
	tm.year = 80 + (date >> 9);
	tm.mon = ((date >> 5) & 15) - 1;
	tm.mday = date & 31;
	tm.zone[0] = '\0';
	tm.yday = 0;

	return tm2sec(&tm);
}