test_write_format_zip_file_zip64.c revision 358090
1141296Sdas/*-
2141296Sdas * Copyright (c) 2003-2008 Tim Kientzle
32116Sjkh * Copyright (c) 2008 Anselm Strauss
42116Sjkh * All rights reserved.
52116Sjkh *
62116Sjkh * Redistribution and use in source and binary forms, with or without
7141296Sdas * modification, are permitted provided that the following conditions
82116Sjkh * are met:
9141296Sdas * 1. Redistributions of source code must retain the above copyright
102116Sjkh *    notice, this list of conditions and the following disclaimer.
112116Sjkh * 2. Redistributions in binary form must reproduce the above copyright
12141296Sdas *    notice, this list of conditions and the following disclaimer in the
132116Sjkh *    documentation and/or other materials provided with the distribution.
142116Sjkh *
15176451Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16176451Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172116Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182116Sjkh * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
192116Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202116Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21141296Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222116Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232116Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242116Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252116Sjkh */
262116Sjkh
272116Sjkh/*
282116Sjkh * Development supported by Google Summer of Code 2008.
292116Sjkh */
302116Sjkh
312116Sjkh#include "test.h"
322116Sjkh__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/test/test_write_format_zip_file_zip64.c 358090 2020-02-19 01:51:44Z mm $");
332116Sjkh
342116Sjkh/*
352116Sjkh * Detailed byte-for-byte verification of the format of a zip archive
362116Sjkh * with a single file written to it that uses Zip64 extensions.
372116Sjkh */
382116Sjkh
39141296Sdasstatic unsigned long
40141296Sdasbitcrc32(unsigned long c, void *_p, size_t s)
41141296Sdas{
422116Sjkh	/* This is a drop-in replacement for crc32() from zlib.
432116Sjkh	 * Libarchive should be able to correctly generate
442116Sjkh	 * uncompressed zip archives (including correct CRCs) even
45181074Sdas	 * when zlib is unavailable, and this function helps us verify
46181074Sdas	 * that.  Yes, this is very, very slow and unsuitable for
472116Sjkh	 * production use, but it's correct, compact, and works well
482116Sjkh	 * enough for this particular usage.  Libarchive internally
492116Sjkh	 * uses a much more efficient implementation.  */
50181062Sdas	const unsigned char *p = _p;
51181062Sdas	int bitctr;
528870Srgrimes
532116Sjkh	if (p == NULL)
542116Sjkh		return (0);
552116Sjkh
56181062Sdas	for (; s > 0; --s) {
57181062Sdas		c ^= *p++;
582116Sjkh		for (bitctr = 8; bitctr > 0; --bitctr) {
592116Sjkh			if (c & 1) c = (c >> 1);
6097407Salfred			else	   c = (c >> 1) ^ 0xedb88320;
61117912Speter			c ^= 0x80000000;
628870Srgrimes		}
632116Sjkh	}
642116Sjkh	return (c);
652116Sjkh}
662116Sjkh
672116Sjkh/* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
682116Sjkhstatic unsigned i2(const unsigned char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
692116Sjkhstatic unsigned i4(const unsigned char *p) { return (i2(p) | (i2(p + 2) << 16)); }
702116Sjkh/* We're only working with small values here; ignore the 4 high bytes. */
712116Sjkhstatic unsigned i8(const unsigned char *p) { return (i4(p)); }
722116Sjkh
732116SjkhDEFINE_TEST(test_write_format_zip_file_zip64)
74141296Sdas{
752116Sjkh	struct archive *a;
762116Sjkh	struct archive_entry *ae;
772116Sjkh	time_t t = 1234567890;
782116Sjkh	struct tm *tm = localtime(&t);
792116Sjkh	size_t used, buffsize = 1000000;
80141296Sdas	unsigned long crc;
812116Sjkh	int file_perm = 00644;
822116Sjkh	int zip_version = 45;
832116Sjkh	int zip_compression = 8;
842116Sjkh	short file_uid = 10, file_gid = 20;
852116Sjkh	unsigned char *buff, *buffend, *p;
862116Sjkh	unsigned char *central_header, *local_header, *eocd, *eocd_record;
872116Sjkh	unsigned char *extension_start, *extension_end;
88141296Sdas	char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
892116Sjkh	const char *file_name = "file";
902116Sjkh
912116Sjkh#ifndef HAVE_ZLIB_H
922116Sjkh	zip_compression = 0;
932116Sjkh#endif
942116Sjkh
952116Sjkh	buff = malloc(buffsize);
962116Sjkh
972116Sjkh	/* Create a new archive in memory. */
982116Sjkh	assert((a = archive_write_new()) != NULL);
992116Sjkh	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
1002116Sjkh	assertEqualIntA(a, ARCHIVE_OK,
1012116Sjkh	    archive_write_set_options(a, "zip:zip64"));
1022116Sjkh	assertEqualIntA(a, ARCHIVE_OK,
1032116Sjkh	    archive_write_set_options(a, "zip:experimental"));
1042116Sjkh	assertEqualIntA(a, ARCHIVE_OK,
1052116Sjkh	    archive_write_open_memory(a, buff, buffsize, &used));
1062116Sjkh
1072116Sjkh	assert((ae = archive_entry_new()) != NULL);
1082116Sjkh	archive_entry_copy_pathname(ae, file_name);
1092116Sjkh	archive_entry_set_mode(ae, AE_IFREG | file_perm);
1102116Sjkh	archive_entry_set_size(ae, sizeof(file_data));
1112116Sjkh	archive_entry_set_uid(ae, file_uid);
112181204Sdas	archive_entry_set_gid(ae, file_gid);
113181204Sdas	archive_entry_set_mtime(ae, t, 0);
114181204Sdas	assertEqualInt(0, archive_write_header(a, ae));
115181204Sdas	archive_entry_free(ae);
116181204Sdas	assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data)));
1172116Sjkh	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
1182116Sjkh	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
1192116Sjkh	buffend = buff + used;
120181204Sdas	dumpfile("constructed.zip", buff, used);
1212116Sjkh
1222116Sjkh	/* Verify "End of Central Directory" record. */
1232116Sjkh	/* Get address of end-of-central-directory record. */
1242116Sjkh	eocd_record = p = buffend - 22; /* Assumes there is no zip comment field. */
1252116Sjkh	failure("End-of-central-directory begins with PK\\005\\006 signature");
126181074Sdas	assertEqualMem(p, "PK\005\006", 4);
127181074Sdas	failure("This must be disk 0");
128181074Sdas	assertEqualInt(i2(p + 4), 0);
129181074Sdas	failure("Central dir must start on disk 0");
130	assertEqualInt(i2(p + 6), 0);
131	failure("All central dir entries are on this disk");
132	assertEqualInt(i2(p + 8), i2(p + 10));
133	eocd = buff + i4(p + 12) + i4(p + 16);
134	failure("no zip comment");
135	assertEqualInt(i2(p + 20), 0);
136
137	/* Get address of first entry in central directory. */
138	central_header = p = buff + i4(buffend - 6);
139	failure("Central file record at offset %d should begin with"
140	    " PK\\001\\002 signature",
141	    i4(buffend - 10));
142
143	/* Verify file entry in central directory. */
144	assertEqualMem(p, "PK\001\002", 4); /* Signature */
145	assertEqualInt(i2(p + 4), 3 * 256 + zip_version); /* Version made by */
146	assertEqualInt(i2(p + 6), zip_version); /* Version needed to extract */
147	assertEqualInt(i2(p + 8), 8); /* Flags */
148	assertEqualInt(i2(p + 10), zip_compression); /* Compression method */
149	assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
150	assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
151	crc = bitcrc32(0, file_data, sizeof(file_data));
152	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
153	/* assertEqualInt(i4(p + 20), sizeof(file_data)); */ /* Compressed size */
154	assertEqualInt(i4(p + 24), sizeof(file_data)); /* Uncompressed size */
155	assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */
156	/* assertEqualInt(i2(p + 30), 28); */ /* Extra field length: See below */
157	assertEqualInt(i2(p + 32), 0); /* File comment length */
158	assertEqualInt(i2(p + 34), 0); /* Disk number start */
159	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
160	assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
161	assertEqualInt(i4(p + 42), 0); /* Offset of local header */
162	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
163	p = extension_start = central_header + 46 + strlen(file_name);
164	extension_end = extension_start + i2(central_header + 30);
165
166	assertEqualInt(i2(p), 0x5455);  /* 'UT' extension header */
167	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
168	assertEqualInt(p[4], 1); /* 'UT' flags */
169	assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
170	p += 4 + i2(p + 2);
171
172	assertEqualInt(i2(p), 0x7875);  /* 'ux' extension header */
173	assertEqualInt(i2(p + 2), 11); /* 'ux' size */
174	/* TODO: verify 'ux' contents */
175	p += 4 + i2(p + 2);
176
177	/* Note: We don't expect to see zip64 extension in the central
178	 * directory, since the writer knows the actual full size by
179	 * the time it is ready to write the central directory and has
180	 * no reason to insert it then.  Info-Zip seems to do the same
181	 * thing. */
182
183	/* Just in case: Report any extra extensions. */
184	while (p < extension_end) {
185		failure("Unexpected extension 0x%04X", i2(p));
186		assert(0);
187		p += 4 + i2(p + 2);
188	}
189
190	/* Should have run exactly to end of extra data. */
191	assert(p == extension_end);
192
193	assert(p == eocd);
194
195	/* After Central dir, we find Zip64 eocd and Zip64 eocd locator. */
196	assertEqualMem(p, "PK\006\006", 4); /* Zip64 eocd */
197	assertEqualInt(i8(p + 4), 44); /* We're using v1 Zip64 eocd */
198	assertEqualInt(i2(p + 12), 45); /* Written by Version 4.5 */
199	assertEqualInt(i2(p + 14), 45); /* Needs version 4.5 to extract */
200	assertEqualInt(i4(p + 16), 0); /* This is disk #0 */
201	assertEqualInt(i4(p + 20), 0); /* Dir starts on disk #0 */
202	assertEqualInt(i8(p + 24), 1); /* 1 entry on this disk */
203	assertEqualInt(i8(p + 32), 1); /* 1 entry total */
204	assertEqualInt(i8(p + 40), eocd - central_header); /* size of cd */
205	assertEqualInt(i8(p + 48), central_header - buff); /* start of cd */
206	p += 12 + i8(p + 4);
207
208	assertEqualMem(p, "PK\006\007", 4); /* Zip64 eocd locator */
209	assertEqualInt(i4(p + 4), 0); /* Zip64 eocd is on disk #0 */
210	assertEqualInt(i8(p + 8), eocd - buff); /* Offset of Zip64 eocd */
211	assertEqualInt(i4(p + 16), 1); /* 1 disk */
212	p += 20;
213
214	/* Regular EOCD immediately follows Zip64 records. */
215	assert(p == eocd_record);
216
217	/* Verify local header of file entry. */
218	p = local_header = buff;
219	assertEqualMem(p, "PK\003\004", 4); /* Signature */
220	assertEqualInt(i2(p + 4), zip_version); /* Version needed to extract */
221	assertEqualInt(i2(p + 6), 8); /* Flags */
222	assertEqualInt(i2(p + 8), zip_compression); /* Compression method */
223	assertEqualInt(i2(p + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
224	assertEqualInt(i2(p + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
225	assertEqualInt(i4(p + 14), 0); /* CRC-32 */
226	/* assertEqualInt(i4(p + 18), sizeof(file_data)); */ /* Compressed size */
227	/* assertEqualInt(i4(p + 22), sizeof(file_data)); */ /* Uncompressed size not stored because we're using length-at-end. */
228	assertEqualInt(i2(p + 26), strlen(file_name)); /* Pathname length */
229	assertEqualInt(i2(p + 28), 57); /* Extra field length */
230	assertEqualMem(p + 30, file_name, strlen(file_name)); /* Pathname */
231	p = extension_start = local_header + 30 + strlen(file_name);
232	extension_end = extension_start + i2(local_header + 28);
233
234	assertEqualInt(i2(p), 0x5455);  /* 'UT' extension header */
235	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
236	assertEqualInt(p[4], 1); /* 'UT' flags */
237	assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
238	p += 4 + i2(p + 2);
239
240	assertEqualInt(i2(p), 0x7875);  /* 'ux' extension header */
241	assertEqualInt(i2(p + 2), 11); /* 'ux' size */
242	assertEqualInt(p[4], 1); /* 'ux' version */
243	assertEqualInt(p[5], 4); /* 'ux' uid size */
244	assertEqualInt(i4(p + 6), file_uid); /* 'Ux' UID */
245	assertEqualInt(p[10], 4); /* 'ux' gid size */
246	assertEqualInt(i4(p + 11), file_gid); /* 'Ux' GID */
247	p += 4 + i2(p + 2);
248
249	assertEqualInt(i2(p), 0x0001);  /* Zip64 extension header */
250	assertEqualInt(i2(p + 2), 16); /* size */
251	assertEqualInt(i8(p + 4), 8); /* uncompressed file size */
252	/* compressed file size we can't verify here */
253	p += 4 + i2(p + 2);
254
255	assertEqualInt(i2(p), 0x6c78); /* 'xl' experimental extension header */
256	assertEqualInt(i2(p + 2), 9); /* size */
257	assertEqualInt(p[4], 7); /* bitmap of included fields */
258	assertEqualInt(i2(p + 5) >> 8, 3); /* system & version made by */
259	assertEqualInt(i2(p + 7), 0); /* internal file attributes */
260	assertEqualInt(i4(p + 9) >> 16 & 01777, file_perm); /* external file attributes */
261	p += 4 + i2(p + 2);
262
263	/* Just in case: Report any extra extensions. */
264	while (p < extension_end) {
265		failure("Unexpected extension 0x%04X", i2(p));
266		assert(0);
267		p += 4 + i2(p + 2);
268	}
269
270	/* Should have run exactly to end of extra data. */
271	assert(p == extension_end);
272
273	/* Data descriptor should follow compressed data. */
274	while (p < central_header && memcmp(p, "PK\007\010", 4) != 0)
275		++p;
276	assertEqualMem(p, "PK\007\010", 4);
277	assertEqualInt(i4(p + 4), crc); /* CRC-32 */
278	/* assertEqualInt(i8(p + 8), ???); */ /* compressed size */
279	assertEqualInt(i8(p + 16), sizeof(file_data)); /* uncompressed size */
280
281	/* Central directory should immediately follow the only entry. */
282	assert(p + 24 == central_header);
283
284	free(buff);
285}
286