summaryrefslogtreecommitdiff
path: root/thirdparty/libwebsockets/server/fops-zip.c
blob: 2b254f67af6b8695680ccebf674c48661fc8ed36 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Original code used in this source file:
 *
 * https://github.com/PerBothner/DomTerm.git @912add15f3d0aec
 *
 * ./lws-term/io.c
 * ./lws-term/junzip.c
 *
 * Copyright (C) 2017  Per Bothner <per@bothner.com>
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * ( copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *
 * lws rewrite:
 *
 * Copyright (C) 2017  Andy Green <andy@warmcat.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation:
 *  version 2.1 of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 */

#include "private-libwebsockets.h"

#include <zlib.h>

/*
 * This code works with zip format containers which may have files compressed
 * with gzip deflate (type 8) or store uncompressed (type 0).
 *
 * Linux zip produces such zipfiles by default, eg
 *
 *  $ zip ../myzip.zip file1 file2 file3
 */

#define ZIP_COMPRESSION_METHOD_STORE 0
#define ZIP_COMPRESSION_METHOD_DEFLATE 8

typedef struct {
	lws_filepos_t		filename_start;
	uint32_t		crc32;
	uint32_t		comp_size;
	uint32_t		uncomp_size;
	uint32_t		offset;
	uint32_t		mod_time;
	uint16_t		filename_len;
	uint16_t		extra;
	uint16_t		method;
	uint16_t		file_com_len;
} lws_fops_zip_hdr_t;

typedef struct {
	struct lws_fop_fd	fop_fd; /* MUST BE FIRST logical fop_fd into
	 	 	 	 	 * file inside zip: fops_zip fops */
	lws_fop_fd_t		zip_fop_fd; /* logical fop fd on to zip file
	 	 	 	 	     * itself: using platform fops */
	lws_fops_zip_hdr_t	hdr;
	z_stream		inflate;
	lws_filepos_t		content_start;
	lws_filepos_t		exp_uncomp_pos;
	union {
		uint8_t		trailer8[8];
		uint32_t	trailer32[2];
	} u;
	uint8_t			rbuf[128]; /* decompression chunk size */
	int			entry_count;

	unsigned int		decompress:1; /* 0 = direct from file */
	unsigned int		add_gzip_container:1;
} *lws_fops_zip_t;

struct lws_plat_file_ops fops_zip;
#define fop_fd_to_priv(FD) ((lws_fops_zip_t)(FD))

static const uint8_t hd[] = { 31, 139, 8, 0, 0, 0, 0, 0, 0, 3 };

enum {
	ZC_SIGNATURE				= 0,
	ZC_VERSION_MADE_BY 			= 4,
	ZC_VERSION_NEEDED_TO_EXTRACT 		= 6,
	ZC_GENERAL_PURPOSE_BIT_FLAG 		= 8,
	ZC_COMPRESSION_METHOD 			= 10,
	ZC_LAST_MOD_FILE_TIME 			= 12,
	ZC_LAST_MOD_FILE_DATE 			= 14,
	ZC_CRC32 				= 16,
	ZC_COMPRESSED_SIZE 			= 20,
	ZC_UNCOMPRESSED_SIZE 			= 24,
	ZC_FILE_NAME_LENGTH 			= 28,
	ZC_EXTRA_FIELD_LENGTH 			= 30,

	ZC_FILE_COMMENT_LENGTH 			= 32,
	ZC_DISK_NUMBER_START 			= 34,
	ZC_INTERNAL_FILE_ATTRIBUTES 		= 36,
	ZC_EXTERNAL_FILE_ATTRIBUTES 		= 38,
	ZC_REL_OFFSET_LOCAL_HEADER 		= 42,
	ZC_DIRECTORY_LENGTH 			= 46,

	ZE_SIGNATURE_OFFSET 			= 0,
	ZE_DESK_NUMBER 				= 4,
	ZE_CENTRAL_DIRECTORY_DISK_NUMBER 	= 6,
	ZE_NUM_ENTRIES_THIS_DISK 		= 8,
	ZE_NUM_ENTRIES 				= 10,
	ZE_CENTRAL_DIRECTORY_SIZE 		= 12,
	ZE_CENTRAL_DIR_OFFSET 			= 16,
	ZE_ZIP_COMMENT_LENGTH 			= 20,
	ZE_DIRECTORY_LENGTH 			= 22,

	ZL_REL_OFFSET_CONTENT			= 28,
	ZL_HEADER_LENGTH			= 30,

	LWS_FZ_ERR_SEEK_END_RECORD		= 1,
	LWS_FZ_ERR_READ_END_RECORD,
	LWS_FZ_ERR_END_RECORD_MAGIC,
	LWS_FZ_ERR_END_RECORD_SANITY,
	LWS_FZ_ERR_CENTRAL_SEEK,
	LWS_FZ_ERR_CENTRAL_READ,
	LWS_FZ_ERR_CENTRAL_SANITY,
	LWS_FZ_ERR_NAME_TOO_LONG,
	LWS_FZ_ERR_NAME_SEEK,
	LWS_FZ_ERR_NAME_READ,
	LWS_FZ_ERR_CONTENT_SANITY,
	LWS_FZ_ERR_CONTENT_SEEK,
	LWS_FZ_ERR_SCAN_SEEK,
	LWS_FZ_ERR_NOT_FOUND,
	LWS_FZ_ERR_ZLIB_INIT,
	LWS_FZ_ERR_READ_CONTENT,
	LWS_FZ_ERR_SEEK_COMPRESSED,
};

static uint16_t
get_u16(void *p)
{
	const uint8_t *c = (const uint8_t *)p;

	return (uint16_t)((c[0] | (c[1] << 8)));
}

static uint32_t
get_u32(void *p)
{
	const uint8_t *c = (const uint8_t *)p;

	return (uint32_t)((c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24)));
}

int
lws_fops_zip_scan(lws_fops_zip_t priv, const char *name, int len)
{
	lws_filepos_t amount;
	uint8_t buf[96];
	int i;

	if (lws_vfs_file_seek_end(priv->zip_fop_fd, -ZE_DIRECTORY_LENGTH) < 0)
		return LWS_FZ_ERR_SEEK_END_RECORD;

	if (lws_vfs_file_read(priv->zip_fop_fd, &amount, buf,
			      ZE_DIRECTORY_LENGTH))
		return LWS_FZ_ERR_READ_END_RECORD;

	if (amount != ZE_DIRECTORY_LENGTH)
		return LWS_FZ_ERR_READ_END_RECORD;

	/*
	 * We require the zip to have the last record right at the end
	 * Linux zip always does this if no zip comment.
	 */
	if (buf[0] != 'P' || buf[1] != 'K' || buf[2] != 5 || buf[3] != 6)
		return LWS_FZ_ERR_END_RECORD_MAGIC;

	i = get_u16(buf + ZE_NUM_ENTRIES);

	if (get_u16(buf + ZE_DESK_NUMBER) ||
	    get_u16(buf + ZE_CENTRAL_DIRECTORY_DISK_NUMBER) ||
	    i != get_u16(buf + ZE_NUM_ENTRIES_THIS_DISK))
		return LWS_FZ_ERR_END_RECORD_SANITY;

	/* end record is OK... look for our file in the central dir */

	if (lws_vfs_file_seek_set(priv->zip_fop_fd,
				  get_u32(buf + ZE_CENTRAL_DIR_OFFSET)) < 0)
		return LWS_FZ_ERR_CENTRAL_SEEK;

	while (i--) {
		priv->content_start = lws_vfs_tell(priv->zip_fop_fd);

		if (lws_vfs_file_read(priv->zip_fop_fd, &amount, buf,
				      ZC_DIRECTORY_LENGTH))
			return LWS_FZ_ERR_CENTRAL_READ;

		if (amount != ZC_DIRECTORY_LENGTH)
			return LWS_FZ_ERR_CENTRAL_READ;

		if (get_u32(buf + ZC_SIGNATURE) != 0x02014B50)
			return LWS_FZ_ERR_CENTRAL_SANITY;

               lwsl_debug("cstart 0x%lx\n", (unsigned long)priv->content_start);

		priv->hdr.filename_len = get_u16(buf + ZC_FILE_NAME_LENGTH);
		priv->hdr.extra = get_u16(buf + ZC_EXTRA_FIELD_LENGTH);
		priv->hdr.filename_start = lws_vfs_tell(priv->zip_fop_fd);

		priv->hdr.method = get_u16(buf + ZC_COMPRESSION_METHOD);
		priv->hdr.crc32 = get_u32(buf + ZC_CRC32);
		priv->hdr.comp_size = get_u32(buf + ZC_COMPRESSED_SIZE);
		priv->hdr.uncomp_size = get_u32(buf + ZC_UNCOMPRESSED_SIZE);
		priv->hdr.offset = get_u32(buf + ZC_REL_OFFSET_LOCAL_HEADER);
		priv->hdr.mod_time = get_u32(buf + ZC_LAST_MOD_FILE_TIME);
		priv->hdr.file_com_len = get_u16(buf + ZC_FILE_COMMENT_LENGTH);

		if (priv->hdr.filename_len != len)
			goto next;

		if (len >= sizeof(buf) - 1)
			return LWS_FZ_ERR_NAME_TOO_LONG;

		if (priv->zip_fop_fd->fops->LWS_FOP_READ(priv->zip_fop_fd,
							&amount, buf, len))
			return LWS_FZ_ERR_NAME_READ;
		if (amount != len)
			return LWS_FZ_ERR_NAME_READ;

		buf[len] = '\0';
		lwsl_debug("check %s vs %s\n", buf, name);

		if (strcmp((const char *)buf, name))
			goto next;

		/* we found a match */
		if (lws_vfs_file_seek_set(priv->zip_fop_fd, priv->hdr.offset) < 0)
			return LWS_FZ_ERR_NAME_SEEK;
		if (priv->zip_fop_fd->fops->LWS_FOP_READ(priv->zip_fop_fd,
							&amount, buf,
							ZL_HEADER_LENGTH))
			return LWS_FZ_ERR_NAME_READ;
		if (amount != ZL_HEADER_LENGTH)
			return LWS_FZ_ERR_NAME_READ;

		priv->content_start = priv->hdr.offset +
				      ZL_HEADER_LENGTH +
				      priv->hdr.filename_len +
				      get_u16(buf + ZL_REL_OFFSET_CONTENT);

		lwsl_debug("content supposed to start at 0x%lx\n",
                          (unsigned long)priv->content_start);

		if (priv->content_start > priv->zip_fop_fd->len)
			return LWS_FZ_ERR_CONTENT_SANITY;

		if (lws_vfs_file_seek_set(priv->zip_fop_fd,
					  priv->content_start) < 0)
			return LWS_FZ_ERR_CONTENT_SEEK;

		/* we are aligned at the start of the content */

		priv->exp_uncomp_pos = 0;

		return 0;

next:
		if (i && lws_vfs_file_seek_set(priv->zip_fop_fd,
					       priv->content_start +
					       ZC_DIRECTORY_LENGTH +
					       priv->hdr.filename_len +
					       priv->hdr.extra +
					       priv->hdr.file_com_len) < 0)
			return LWS_FZ_ERR_SCAN_SEEK;
	}

	return LWS_FZ_ERR_NOT_FOUND;
}

static int
lws_fops_zip_reset_inflate(lws_fops_zip_t priv)
{
	if (priv->decompress)
		inflateEnd(&priv->inflate);

	priv->inflate.zalloc = Z_NULL;
	priv->inflate.zfree = Z_NULL;
	priv->inflate.opaque = Z_NULL;
	priv->inflate.avail_in = 0;
	priv->inflate.next_in = Z_NULL;

	if (inflateInit2(&priv->inflate, -MAX_WBITS) != Z_OK) {
		lwsl_err("inflate init failed\n");
		return LWS_FZ_ERR_ZLIB_INIT;
	}

	if (lws_vfs_file_seek_set(priv->zip_fop_fd, priv->content_start) < 0)
		return LWS_FZ_ERR_CONTENT_SEEK;

	priv->exp_uncomp_pos = 0;

	return 0;
}

static lws_fop_fd_t
lws_fops_zip_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
		  const char *vpath, lws_fop_flags_t *flags)
{
	lws_fop_flags_t local_flags = 0;
	lws_fops_zip_t priv;
	char rp[192];
	int m;

	/*
	 * vpath points at the / after the fops signature in vfs_path, eg
	 * with a vfs_path "/var/www/docs/manual.zip/index.html", vpath
	 * will come pointing at "/index.html"
	 */

	priv = lws_zalloc(sizeof(*priv), "fops_zip priv");
	if (!priv)
		return NULL;

	priv->fop_fd.fops = &fops_zip;

	m = sizeof(rp) - 1;
	if ((vpath - vfs_path - 1) < m)
		m = vpath - vfs_path - 1;
	strncpy(rp, vfs_path, m);
	rp[m] = '\0';

	/* open the zip file itself using the incoming fops, not fops_zip */

	priv->zip_fop_fd = fops->LWS_FOP_OPEN(fops, rp, NULL, &local_flags);
	if (!priv->zip_fop_fd) {
		lwsl_err("unable to open zip %s\n", rp);
		goto bail1;
	}

	if (*vpath == '/')
		vpath++;

	m = lws_fops_zip_scan(priv, vpath, strlen(vpath));
	if (m) {
		lwsl_err("unable to find record matching '%s' %d\n", vpath, m);
		goto bail2;
	}

	/* the directory metadata tells us modification time, so pass it on */
	priv->fop_fd.mod_time = priv->hdr.mod_time;
	*flags |= LWS_FOP_FLAG_MOD_TIME_VALID | LWS_FOP_FLAG_VIRTUAL;
	priv->fop_fd.flags = *flags;

	/* The zip fop_fd is left pointing at the start of the content.
	 *
	 * 1) Content could be uncompressed (STORE), and we can always serve
	 *    that directly
	 *
	 * 2) Content could be compressed (GZIP), and the client can handle
	 *    receiving GZIP... we can wrap it in a GZIP header and trailer
	 *    and serve the content part directly.  The flag indicating we
	 *    are providing GZIP directly is set so lws will send the right
	 *    headers.
	 *
	 * 3) Content could be compressed (GZIP) but the client can't handle
	 *    receiving GZIP... we can decompress it and serve as it is
	 *    inflated piecemeal.
	 *
	 * 4) Content may be compressed some unknown way... fail
	 *
	 */
	if (priv->hdr.method == ZIP_COMPRESSION_METHOD_STORE) {
		/*
		 * it is stored uncompressed, leave it indicated as
		 * uncompressed, and just serve it from inside the
		 * zip with no gzip container;
		 */

		lwsl_info("direct zip serving (stored)\n");

		priv->fop_fd.len = priv->hdr.uncomp_size;

		return &priv->fop_fd;
	}

	if ((*flags & LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP) &&
	    priv->hdr.method == ZIP_COMPRESSION_METHOD_DEFLATE) {

		/*
		 * We can serve the gzipped file contents directly as gzip
		 * from inside the zip container; client says it is OK.
		 *
		 * To convert to standalone gzip, we have to add a 10-byte
		 * constant header and a variable 8-byte trailer around the
		 * content.
		 *
		 * The 8-byte trailer is prepared now and held in the priv.
		 */

		lwsl_info("direct zip serving (gzipped)\n");

		priv->fop_fd.len = sizeof(hd) + priv->hdr.comp_size +
				   sizeof(priv->u);

		if (lws_is_be()) {
			uint8_t *p = priv->u.trailer8;

			*p++ = (uint8_t)priv->hdr.crc32;
			*p++ = (uint8_t)(priv->hdr.crc32 >> 8);
			*p++ = (uint8_t)(priv->hdr.crc32 >> 16);
			*p++ = (uint8_t)(priv->hdr.crc32 >> 24);
			*p++ = (uint8_t)priv->hdr.uncomp_size;
			*p++ = (uint8_t)(priv->hdr.uncomp_size >> 8);
			*p++ = (uint8_t)(priv->hdr.uncomp_size >> 16);
			*p   = (uint8_t)(priv->hdr.uncomp_size >> 24);
		} else {
			priv->u.trailer32[0] = priv->hdr.crc32;
			priv->u.trailer32[1] = priv->hdr.uncomp_size;
		}

		*flags |= LWS_FOP_FLAG_COMPR_IS_GZIP;
		priv->fop_fd.flags = *flags;
		priv->add_gzip_container = 1;

		return &priv->fop_fd;
	}

	if (priv->hdr.method == ZIP_COMPRESSION_METHOD_DEFLATE) {

		/* we must decompress it to serve it */

		lwsl_info("decompressed zip serving\n");

		priv->fop_fd.len = priv->hdr.uncomp_size;

		if (lws_fops_zip_reset_inflate(priv)) {
			lwsl_err("inflate init failed\n");
			goto bail2;
		}

		priv->decompress = 1;

		return &priv->fop_fd;
	}

	/* we can't handle it ... */

	lwsl_err("zipped file %s compressed in unknown way (%d)\n", vfs_path,
		 priv->hdr.method);

bail2:
	lws_vfs_file_close(&priv->zip_fop_fd);
bail1:
	free(priv);

	return NULL;
}

/* ie, we are closing the fop_fd for the file inside the gzip */

static int
lws_fops_zip_close(lws_fop_fd_t *fd)
{
	lws_fops_zip_t priv = fop_fd_to_priv(*fd);

	if (priv->decompress)
		inflateEnd(&priv->inflate);

	lws_vfs_file_close(&priv->zip_fop_fd); /* close the gzip fop_fd */

	free(priv);
	*fd = NULL;

	return 0;
}

static lws_fileofs_t
lws_fops_zip_seek_cur(lws_fop_fd_t fd, lws_fileofs_t offset_from_cur_pos)
{
	fd->pos += offset_from_cur_pos;

	return fd->pos;
}

static int
lws_fops_zip_read(lws_fop_fd_t fd, lws_filepos_t *amount, uint8_t *buf,
		  lws_filepos_t len)
{
	lws_fops_zip_t priv = fop_fd_to_priv(fd);
	lws_filepos_t ramount, rlen, cur = lws_vfs_tell(fd);
	int ret;

	if (priv->decompress) {

		if (priv->exp_uncomp_pos != fd->pos) {
			/*
			 *  there has been a seek in the uncompressed fop_fd
			 * we have to restart the decompression and loop eating
			 * the decompressed data up to the seek point
			 */
			lwsl_info("seek in decompressed\n");

			lws_fops_zip_reset_inflate(priv);

			while (priv->exp_uncomp_pos != fd->pos) {
				rlen = len;
				if (rlen > fd->pos - priv->exp_uncomp_pos)
					rlen = fd->pos - priv->exp_uncomp_pos;
				if (lws_fops_zip_read(fd, amount, buf, rlen))
					return LWS_FZ_ERR_SEEK_COMPRESSED;
			}
			*amount = 0;
		}

		priv->inflate.avail_out = (unsigned int)len;
		priv->inflate.next_out = buf;

spin:
		if (!priv->inflate.avail_in) {
			rlen = sizeof(priv->rbuf);
			if (rlen > priv->hdr.comp_size -
				   (cur - priv->content_start))
				rlen = priv->hdr.comp_size -
				       (priv->hdr.comp_size -
					priv->content_start);

			if (priv->zip_fop_fd->fops->LWS_FOP_READ(
					priv->zip_fop_fd, &ramount, priv->rbuf,
					rlen))
				return LWS_FZ_ERR_READ_CONTENT;

			cur += ramount;

			priv->inflate.avail_in = (unsigned int)ramount;
			priv->inflate.next_in = priv->rbuf;
		}

		ret = inflate(&priv->inflate, Z_NO_FLUSH);
		if (ret == Z_STREAM_ERROR)
			return ret;

		switch (ret) {
		case Z_NEED_DICT:
			ret = Z_DATA_ERROR;
			/* and fall through */
		case Z_DATA_ERROR:
		case Z_MEM_ERROR:

			return ret;
		}

		if (!priv->inflate.avail_in && priv->inflate.avail_out &&
		     cur != priv->content_start + priv->hdr.comp_size)
			goto spin;

		*amount = len - priv->inflate.avail_out;

		priv->exp_uncomp_pos += *amount;
		fd->pos += *amount;

		return 0;
	}

	if (priv->add_gzip_container) {

		lwsl_info("%s: gzip + container\n", __func__);
		*amount = 0;

		/* place the canned header at the start */

		if (len && fd->pos < sizeof(hd)) {
			rlen = sizeof(hd) - fd->pos;
			if (rlen > len)
				rlen = len;
			/* provide stuff from canned header */
			memcpy(buf, hd + fd->pos, (size_t)rlen);
			fd->pos += rlen;
			buf += rlen;
			len -= rlen;
			*amount += rlen;
		}

		/* serve gzipped data direct from zipfile */

		if (len && fd->pos >= sizeof(hd) &&
		    fd->pos < priv->hdr.comp_size + sizeof(hd)) {

			rlen = priv->hdr.comp_size - (priv->zip_fop_fd->pos -
						      priv->content_start);
			if (rlen > len)
				rlen = len;

			if (rlen &&
			    priv->zip_fop_fd->pos < (priv->hdr.comp_size +
					    	     priv->content_start)) {
				if (lws_vfs_file_read(priv->zip_fop_fd,
						      &ramount, buf, rlen))
					return LWS_FZ_ERR_READ_CONTENT;
				*amount += ramount;
				fd->pos += ramount; // virtual pos
				buf += ramount;
				len -= ramount;
			}
		}

		/* place the prepared trailer at the end */

		if (len && fd->pos >= priv->hdr.comp_size + sizeof(hd) &&
		    fd->pos < priv->hdr.comp_size + sizeof(hd) +
		    	      sizeof(priv->u)) {
			cur = fd->pos - priv->hdr.comp_size - sizeof(hd);
			rlen = sizeof(priv->u) - cur;
			if (rlen > len)
				rlen = len;

			memcpy(buf, priv->u.trailer8 + cur, (size_t)rlen);

			*amount += rlen;
			fd->pos += rlen;
		}

		return 0;
	}

	lwsl_info("%s: store\n", __func__);

	if (len > priv->hdr.uncomp_size - (cur - priv->content_start))
		len = priv->hdr.comp_size - (priv->hdr.comp_size -
					     priv->content_start);

	if (priv->zip_fop_fd->fops->LWS_FOP_READ(priv->zip_fop_fd,
						 amount, buf, len))
		return LWS_FZ_ERR_READ_CONTENT;

	return 0;
}

struct lws_plat_file_ops fops_zip = {
	lws_fops_zip_open,
	lws_fops_zip_close,
	lws_fops_zip_seek_cur,
	lws_fops_zip_read,
	NULL,
	{ { ".zip/", 5 }, { ".jar/", 5 }, { ".war/", 5 } },
	NULL,
};