summaryrefslogtreecommitdiffstats
path: root/libraries/pack200/src/zip.cpp
blob: 32e8bd5009ca2d3f9059eb8c122b591e377a823c (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
/*
 * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * Note: Lifted from uncrunch.c from jdk sources
 */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <stdint.h>

#include <stdlib.h>
#include <assert.h>

#ifndef _MSC_VER
#include <strings.h>
#endif

#include "defines.h"
#include "bytes.h"
#include "utils.h"

#include "constants.h"
#include "unpack.h"

#include "zip.h"

#include "zlib.h"

inline uint32_t jar::get_crc32(uint32_t c, uchar *ptr, uint32_t len)
{
	return crc32(c, ptr, len);
}

// FIXME: this is bullshit. Do real endianness detection.
#ifdef sparc
#define SWAP_BYTES(a) ((((a) << 8) & 0xff00) | 0x00ff) & (((a) >> 8) | 0xff00)
#else
#define SWAP_BYTES(a) (a)
#endif

#define GET_INT_LO(a) SWAP_BYTES(a & 0xFFFF)

#define GET_INT_HI(a) SWAP_BYTES((a >> 16) & 0xFFFF);

void jar::init(unpacker *u_)
{
	BYTES_OF(*this).clear();
	u = u_;
	u->jarout = this;
}

// Write data to the ZIP output stream.
void jar::write_data(void *buff, int len)
{
	while (len > 0)
	{
		int rc = (int)fwrite(buff, 1, len, jarfp);
		if (rc <= 0)
		{
			fprintf(stderr, "Error: write on output file failed err=%d\n", errno);
			exit(1); // Called only from the native standalone unpacker
		}
		output_file_offset += rc;
		buff = ((char *)buff) + rc;
		len -= rc;
	}
}

void jar::add_to_jar_directory(const char *fname, bool store, int modtime, int len, int clen,
							   uint32_t crc)
{
	uint32_t fname_length = (uint32_t)strlen(fname);
	ushort header[23];
	if (modtime == 0)
		modtime = default_modtime;
	uint32_t dostime = get_dostime(modtime);

	header[0] = (ushort)SWAP_BYTES(0x4B50);
	header[1] = (ushort)SWAP_BYTES(0x0201);
	header[2] = (ushort)SWAP_BYTES(0xA);

	// required version
	header[3] = (ushort)SWAP_BYTES(0xA);

	// flags 02 = maximum  sub-compression flag
	header[4] = (store) ? 0x0 : SWAP_BYTES(0x2);

	// Compression method 8=deflate.
	header[5] = (store) ? 0x0 : SWAP_BYTES(0x08);

	// Last modified date and time.
	header[6] = (ushort)GET_INT_LO(dostime);
	header[7] = (ushort)GET_INT_HI(dostime);

	// CRC
	header[8] = (ushort)GET_INT_LO(crc);
	header[9] = (ushort)GET_INT_HI(crc);

	// Compressed length:
	header[10] = (ushort)GET_INT_LO(clen);
	header[11] = (ushort)GET_INT_HI(clen);

	// Uncompressed length.
	header[12] = (ushort)GET_INT_LO(len);
	header[13] = (ushort)GET_INT_HI(len);

	// Filename length
	header[14] = (ushort)SWAP_BYTES(fname_length);
	// So called "extra field" length.
	header[15] = 0;
	// So called "comment" length.
	header[16] = 0;
	// Disk number start
	header[17] = 0;
	// File flags => binary
	header[18] = 0;
	// More file flags
	header[19] = 0;
	header[20] = 0;
	// Offset within ZIP file.
	header[21] = (ushort)GET_INT_LO(output_file_offset);
	header[22] = (ushort)GET_INT_HI(output_file_offset);

	// Copy the whole thing into the central directory.
	central_directory.append(header, sizeof(header));

	// Copy the fname to the header.
	central_directory.append(fname, fname_length);

	central_directory_count++;
}

void jar::write_jar_header(const char *fname, bool store, int modtime, int len, int clen,
						   uint32_t crc)
{
	uint32_t fname_length = (uint32_t)strlen(fname);
	ushort header[15];
	if (modtime == 0)
		modtime = default_modtime;
	uint32_t dostime = get_dostime(modtime);

	// ZIP LOC magic.
	header[0] = (ushort)SWAP_BYTES(0x4B50);
	header[1] = (ushort)SWAP_BYTES(0x0403);

	// Version
	header[2] = (ushort)SWAP_BYTES(0xA);

	// flags 02 = maximum  sub-compression flag
	header[3] = (store) ? 0x0 : SWAP_BYTES(0x2);

	// Compression method = deflate
	header[4] = (store) ? 0x0 : SWAP_BYTES(0x08);

	// Last modified date and time.
	header[5] = (ushort)GET_INT_LO(dostime);
	header[6] = (ushort)GET_INT_HI(dostime);

	// CRC
	header[7] = (ushort)GET_INT_LO(crc);
	header[8] = (ushort)GET_INT_HI(crc);

	// Compressed length:
	header[9] = (ushort)GET_INT_LO(clen);
	header[10] = (ushort)GET_INT_HI(clen);

	// Uncompressed length.
	header[11] = (ushort)GET_INT_LO(len);
	header[12] = (ushort)GET_INT_HI(len);

	// Filename length
	header[13] = (ushort)SWAP_BYTES(fname_length);
	// So called "extra field" length.
	header[14] = 0;

	// Write the LOC header to the output file.
	write_data(header, (int)sizeof(header));

	// Copy the fname to the header.
	write_data((char *)fname, (int)fname_length);
}

void jar::write_central_directory()
{
	bytes mc;
	mc.set("PACK200");

	ushort header[11];

	// Create the End of Central Directory structure.
	header[0] = (ushort)SWAP_BYTES(0x4B50);
	header[1] = (ushort)SWAP_BYTES(0x0605);
	// disk numbers
	header[2] = 0;
	header[3] = 0;
	// Number of entries in central directory.
	header[4] = (ushort)SWAP_BYTES(central_directory_count);
	header[5] = (ushort)SWAP_BYTES(central_directory_count);
	// Size of the central directory}
	header[6] = (ushort)GET_INT_LO((int)central_directory.size());
	header[7] = (ushort)GET_INT_HI((int)central_directory.size());
	// Offset of central directory within disk.
	header[8] = (ushort)GET_INT_LO(output_file_offset);
	header[9] = (ushort)GET_INT_HI(output_file_offset);
	// zipfile comment length;
	header[10] = (ushort)SWAP_BYTES((int)mc.len);

	// Write the central directory.
	write_data(central_directory.b);

	// Write the End of Central Directory structure.
	write_data(header, (int)sizeof(header));

	// Write the comment.
	write_data(mc);
}

// Public API

// Open a Jar file and initialize.
void jar::openJarFile(const char *fname)
{
	if (!jarfp)
	{
		jarfp = fopen(fname, "wb");
		if (!jarfp)
		{
			fprintf(stderr, "Error: Could not open jar file: %s\n", fname);
			exit(3); // Called only from the native standalone unpacker
		}
	}
}

// Add a ZIP entry and copy the file data
void jar::addJarEntry(const char *fname, bool deflate_hint, int modtime, bytes &head,
					  bytes &tail)
{
	int len = (int)(head.len + tail.len);
	int clen = 0;

	uint32_t crc = get_crc32(0, Z_NULL, 0);
	if (head.len != 0)
		crc = get_crc32(crc, (uchar *)head.ptr, (uint32_t)head.len);
	if (tail.len != 0)
		crc = get_crc32(crc, (uchar *)tail.ptr, (uint32_t)tail.len);

	bool deflate = (deflate_hint && len > 0);

	if (deflate)
	{
		if (deflate_bytes(head, tail) == false)
		{
			deflate = false;
		}
	}
	clen = (int)((deflate) ? deflated.size() : len);
	add_to_jar_directory(fname, !deflate, modtime, len, clen, crc);
	write_jar_header(fname, !deflate, modtime, len, clen, crc);

	if (deflate)
	{
		write_data(deflated.b);
	}
	else
	{
		write_data(head);
		write_data(tail);
	}
}

// Add a ZIP entry for a directory name no data
void jar::addDirectoryToJarFile(const char *dir_name)
{
	bool store = true;
	add_to_jar_directory((const char *)dir_name, store, default_modtime, 0, 0, 0);
	write_jar_header((const char *)dir_name, store, default_modtime, 0, 0, 0);
}

// Write out the central directory and close the jar file.
void jar::closeJarFile(bool central)
{
	if (jarfp)
	{
		fflush(jarfp);
		if (central)
			write_central_directory();
		fflush(jarfp);
		fclose(jarfp);
	}
	reset();
}

/* Convert the date y/n/d and time h:m:s to a four byte DOS date and
 *  time (date in high two bytes, time in low two bytes allowing magnitude
 *  comparison).
 */
inline uint32_t jar::dostime(int y, int n, int d, int h, int m, int s)
{
	return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0)
					: (((uint32_t)y - 1980) << 25) | ((uint32_t)n << 21) | ((uint32_t)d << 16) |
						  ((uint32_t)h << 11) | ((uint32_t)m << 5) | ((uint32_t)s >> 1);
}
/*
#ifdef _REENTRANT // solaris
extern "C" struct tm *gmtime_r(const time_t *, struct tm *);
#else
#define gmtime_r(t, s) gmtime(t)
#endif
*/
/*
 * Return the Unix time in DOS format
 */
uint32_t jar::get_dostime(int modtime)
{
	// see defines.h
	if (modtime != 0 && modtime == modtime_cache)
		return dostime_cache;
	if (modtime != 0 && default_modtime == 0)
		default_modtime = modtime; // catch a reasonable default
	time_t t = modtime;
	struct tm sbuf;
	(void)memset((void *)&sbuf, 0, sizeof(sbuf));
	struct tm *s = gmtime_r(&t, &sbuf);
	modtime_cache = modtime;
	dostime_cache =
		dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday, s->tm_hour, s->tm_min, s->tm_sec);
	// printf("modtime %d => %d\n", modtime_cache, dostime_cache);
	return dostime_cache;
}

/* Returns true on success, and will set the clen to the compressed
   length, the caller should verify if true and clen less than the
   input data
*/
bool jar::deflate_bytes(bytes &head, bytes &tail)
{
	int len = (int)(head.len + tail.len);

	z_stream zs;
	BYTES_OF(zs).clear();

	// NOTE: the window size should always be -MAX_WBITS normally -15.
	// unzip/zipup.c and java/Deflater.c

	int error =
		deflateInit2(&zs, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
	if (error != Z_OK)
	{
		/*
		switch (error)
		{
		case Z_MEM_ERROR:
			PRINTCR((2, "Error: deflate error : Out of memory \n"));
			break;
		case Z_STREAM_ERROR:
			PRINTCR((2, "Error: deflate error : Invalid compression level \n"));
			break;
		case Z_VERSION_ERROR:
			PRINTCR((2, "Error: deflate error : Invalid version\n"));
			break;
		default:
			PRINTCR((2, "Error: Internal deflate error error = %d\n", error));
		}
		*/
		return false;
	}

	deflated.empty();
	zs.next_out = (uchar *)deflated.grow(len + (len / 2));
	zs.avail_out = (int)deflated.size();

	zs.next_in = (uchar *)head.ptr;
	zs.avail_in = (int)head.len;

	bytes *first = &head;
	bytes *last = &tail;
	if (last->len == 0)
	{
		first = nullptr;
		last = &head;
	}
	else if (first->len == 0)
	{
		first = nullptr;
	}

	if (first != nullptr && error == Z_OK)
	{
		zs.next_in = (uchar *)first->ptr;
		zs.avail_in = (int)first->len;
		error = deflate(&zs, Z_NO_FLUSH);
	}
	if (error == Z_OK)
	{
		zs.next_in = (uchar *)last->ptr;
		zs.avail_in = (int)last->len;
		error = deflate(&zs, Z_FINISH);
	}
	if (error == Z_STREAM_END)
	{
		if (len > (int)zs.total_out)
		{
			deflated.b.len = zs.total_out;
			deflateEnd(&zs);
			return true;
		}
		deflateEnd(&zs);
		return false;
	}

	deflateEnd(&zs);
	return false;
}

// Callback for fetching data from a GZIP input stream
static int64_t read_input_via_gzip(unpacker *u, void *buf, int64_t minlen, int64_t maxlen)
{
	assert(minlen <= maxlen); // don't talk nonsense
	int64_t numread = 0;
	char *bufptr = (char *)buf;
	char *inbuf = u->gzin->inbuf;
	size_t inbuflen = sizeof(u->gzin->inbuf);
	unpacker::read_input_fn_t read_gzin_fn = (unpacker::read_input_fn_t)u->gzin->read_input_fn;
	z_stream &zs = *(z_stream *)u->gzin->zstream;
	while (numread < minlen)
	{
		int readlen = (1 << 16); // pretty arbitrary
		if (readlen > (maxlen - numread))
			readlen = (int)(maxlen - numread);
		zs.next_out = (uchar *)bufptr;
		zs.avail_out = readlen;
		if (zs.avail_in == 0)
		{
			zs.avail_in = (int)read_gzin_fn(u, inbuf, 1, inbuflen);
			zs.next_in = (uchar *)inbuf;
		}
		int error = inflate(&zs, Z_NO_FLUSH);
		if (error != Z_OK && error != Z_STREAM_END)
		{
			unpack_abort("error inflating input");
			break;
		}
		int nr = readlen - zs.avail_out;
		numread += nr;
		bufptr += nr;
		assert(numread <= maxlen);
		if (error == Z_STREAM_END)
		{
			enum
			{
				TRAILER_LEN = 8
			};
			// skip 8-byte trailer
			if (zs.avail_in >= TRAILER_LEN)
			{
				zs.avail_in -= TRAILER_LEN;
			}
			else
			{
				// Bug: 5023768,we read past the TRAILER_LEN to see if there is
				// any extraneous data, as we dont support concatenated .gz
				// files just yet.
				int extra = (int)read_gzin_fn(u, inbuf, 1, inbuflen);
				zs.avail_in += extra - TRAILER_LEN;
			}
			// %%% should check final CRC and length here
			// %%% should check for concatenated *.gz files here
			if (zs.avail_in > 0)
				unpack_abort("garbage after end of deflated input stream");
			// pop this filter off:
			u->gzin->free();
			break;
		}
	}

	// fprintf(u->errstrm, "readInputFn(%d,%d) => %d (gunzip)\n",
	//        (int)minlen, (int)maxlen, (int)numread);
	return numread;
}

void gunzip::init(unpacker *u_)
{
	BYTES_OF(*this).clear();
	u = u_;
	assert(u->gzin == nullptr); // once only, please
	read_input_fn = (void *)u->read_input_fn;
	zstream = NEW(z_stream, 1);
	u->gzin = this;
	u->read_input_fn = read_input_via_gzip;
}

void gunzip::start(int magic)
{
	assert((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC);
	int gz_flg = (magic & 0xFF); // keep "flg", discard other 3 bytes
	enum
	{
		FHCRC = (1 << 1),
		FEXTRA = (1 << 2),
		FNAME = (1 << 3),
		FCOMMENT = (1 << 4)
	};
	char gz_mtime[4];
	char gz_xfl[1];
	char gz_os[1];
	char gz_extra_len[2];
	char gz_hcrc[2];
	char gz_ignore;
	// do not save extra, name, comment
	read_fixed_field(gz_mtime, sizeof(gz_mtime));
	read_fixed_field(gz_xfl, sizeof(gz_xfl));
	read_fixed_field(gz_os, sizeof(gz_os));
	if (gz_flg & FEXTRA)
	{
		read_fixed_field(gz_extra_len, sizeof(gz_extra_len));
		int extra_len = gz_extra_len[0] & 0xFF;
		extra_len += (gz_extra_len[1] & 0xFF) << 8;
		for (; extra_len > 0; extra_len--)
		{
			read_fixed_field(&gz_ignore, 1);
		}
	}
	int null_terms = 0;
	if (gz_flg & FNAME)
		null_terms++;
	if (gz_flg & FCOMMENT)
		null_terms++;
	for (; null_terms; null_terms--)
	{
		for (;;)
		{
			gz_ignore = 0;
			read_fixed_field(&gz_ignore, 1);
			if (gz_ignore == 0)
				break;
		}
	}
	if (gz_flg & FHCRC)
		read_fixed_field(gz_hcrc, sizeof(gz_hcrc));

	// now the input stream is ready to read into the inflater
	int error = inflateInit2((z_stream *)zstream, -MAX_WBITS);
	if (error != Z_OK)
	{
		unpack_abort("cannot create input");
	}
}

void gunzip::free()
{
	assert(u->gzin == this);
	u->gzin = nullptr;
	u->read_input_fn = (unpacker::read_input_fn_t) this->read_input_fn;
	inflateEnd((z_stream *)zstream);
	::free(zstream);
	zstream = nullptr;
	::free(this);
}

void gunzip::read_fixed_field(char *buf, size_t buflen)
{
	int64_t nr = ((unpacker::read_input_fn_t)read_input_fn)(u, buf, buflen, buflen);
	if ((size_t)nr != buflen)
		unpack_abort("short stream header");
}