xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip_stream.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * Copyright (c) 2003-2023 Tim Kientzle
3  * Copyright (c) 2008 Anselm Strauss
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "test.h"
28 
29 /*
30  * Detailed byte-for-byte verification of the format of a zip archive
31  * written in streaming mode WITHOUT Zip64 extensions enabled.
32  */
33 
34 static unsigned long
bitcrc32(unsigned long c,void * _p,size_t s)35 bitcrc32(unsigned long c, void *_p, size_t s)
36 {
37 	/* This is a drop-in replacement for crc32() from zlib.
38 	 * Libarchive should be able to correctly generate
39 	 * uncompressed zip archives (including correct CRCs) even
40 	 * when zlib is unavailable, and this function helps us verify
41 	 * that.  Yes, this is very, very slow and unsuitable for
42 	 * production use, but it's correct, compact, and works well
43 	 * enough for this particular usage.  Libarchive internally
44 	 * uses a much more efficient implementation.  */
45 	const unsigned char *p = _p;
46 	int bitctr;
47 
48 	if (p == NULL)
49 		return (0);
50 
51 	for (; s > 0; --s) {
52 		c ^= *p++;
53 		for (bitctr = 8; bitctr > 0; --bitctr) {
54 			if (c & 1) c = (c >> 1);
55 			else	   c = (c >> 1) ^ 0xedb88320;
56 			c ^= 0x80000000;
57 		}
58 	}
59 	return (c);
60 }
61 
62 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
i2(const unsigned char * p)63 static unsigned i2(const unsigned char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
i4(const unsigned char * p)64 static unsigned i4(const unsigned char *p) { return (i2(p) | (i2(p + 2) << 16)); }
65 
DEFINE_TEST(test_write_format_zip_stream)66 DEFINE_TEST(test_write_format_zip_stream)
67 {
68 	struct archive *a;
69 	struct archive_entry *ae;
70 	size_t used, buffsize = 1000000;
71 	unsigned long crc;
72 	unsigned long compressed_size = 0;
73 	int file_perm = 00644;
74 #ifdef HAVE_ZLIB_H
75 	int zip_version = 20;
76 #else
77 	int zip_version = 10;
78 #endif
79 	int zip_compression = 8;
80 	short file_uid = 10, file_gid = 20;
81 	unsigned char *buff, *buffend, *p;
82 	unsigned char *central_header, *local_header, *eocd, *eocd_record;
83 	unsigned char *extension_start, *extension_end;
84 	unsigned char *data_start, *data_end;
85 	char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
86 	const char *file_name = "file";
87 
88 #ifndef HAVE_ZLIB_H
89 	zip_version = 10;
90 	zip_compression = 0;
91 #endif
92 
93 	buff = malloc(buffsize);
94 
95 	/* Create a new archive in memory. */
96 	assert((a = archive_write_new()) != NULL);
97 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
98 	assertEqualIntA(a, ARCHIVE_OK,
99 	    archive_write_set_options(a, "zip:!zip64"));
100 	assertEqualIntA(a, ARCHIVE_OK,
101 	    archive_write_open_memory(a, buff, buffsize, &used));
102 
103 	assert((ae = archive_entry_new()) != NULL);
104 	archive_entry_copy_pathname(ae, file_name);
105 	archive_entry_set_mode(ae, AE_IFREG | file_perm);
106 	archive_entry_set_uid(ae, file_uid);
107 	archive_entry_set_gid(ae, file_gid);
108 	archive_entry_set_mtime(ae, 0, 0);
109 	assertEqualInt(0, archive_write_header(a, ae));
110 	archive_entry_free(ae);
111 	assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data)));
112 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
113 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
114 	buffend = buff + used;
115 	dumpfile("constructed.zip", buff, used);
116 
117 	/* Verify "End of Central Directory" record. */
118 	/* Get address of end-of-central-directory record. */
119 	eocd_record = p = buffend - 22; /* Assumes there is no zip comment field. */
120 	failure("End-of-central-directory begins with PK\\005\\006 signature");
121 	assertEqualMem(p, "PK\005\006", 4);
122 	failure("This must be disk 0");
123 	assertEqualInt(i2(p + 4), 0);
124 	failure("Central dir must start on disk 0");
125 	assertEqualInt(i2(p + 6), 0);
126 	failure("All central dir entries are on this disk");
127 	assertEqualInt(i2(p + 8), i2(p + 10));
128 	eocd = buff + i4(p + 12) + i4(p + 16);
129 	failure("no zip comment");
130 	assertEqualInt(i2(p + 20), 0);
131 
132 	/* Get address of first entry in central directory. */
133 	central_header = p = buff + i4(buffend - 6);
134 	failure("Central file record at offset %d should begin with"
135 	    " PK\\001\\002 signature",
136 	    i4(buffend - 10));
137 
138 	/* Verify file entry in central directory. */
139 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
140 	assertEqualInt(i2(p + 4), 3 * 256 + zip_version); /* Version made by */
141 	assertEqualInt(i2(p + 6), zip_version); /* Version needed to extract */
142 	assertEqualInt(i2(p + 8), 8); /* Flags */
143 	assertEqualInt(i2(p + 10), zip_compression); /* Compression method */
144 	assertEqualInt(i2(p + 12), 0); /* File time */
145 	assertEqualInt(i2(p + 14), 33); /* File date */
146 	crc = bitcrc32(0, file_data, sizeof(file_data));
147 	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
148 	compressed_size = i4(p + 20);  /* Compressed size */
149 	assertEqualInt(i4(p + 24), sizeof(file_data)); /* Uncompressed size */
150 	assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */
151 	/* assertEqualInt(i2(p + 30), 28); */ /* Extra field length: See below */
152 	assertEqualInt(i2(p + 32), 0); /* File comment length */
153 	assertEqualInt(i2(p + 34), 0); /* Disk number start */
154 	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
155 	assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
156 	assertEqualInt(i4(p + 42), 0); /* Offset of local header */
157 	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
158 	p = extension_start = central_header + 46 + strlen(file_name);
159 	extension_end = extension_start + i2(central_header + 30);
160 
161 	assertEqualInt(i2(p), 0x7875);  /* 'ux' extension header */
162 	assertEqualInt(i2(p + 2), 11); /* 'ux' size */
163 	assertEqualInt(p[4], 1); /* 'ux' version */
164 	assertEqualInt(p[5], 4); /* 'ux' uid size */
165 	assertEqualInt(i4(p + 6), file_uid); /* 'Ux' UID */
166 	assertEqualInt(p[10], 4); /* 'ux' gid size */
167 	assertEqualInt(i4(p + 11), file_gid); /* 'Ux' GID */
168 	p += 4 + i2(p + 2);
169 
170 	assertEqualInt(i2(p), 0x5455);  /* 'UT' extension header */
171 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
172 	assertEqualInt(p[4], 1); /* 'UT' flags */
173 	assertEqualInt(i4(p + 5), 0); /* 'UT' mtime */
174 	p += 4 + i2(p + 2);
175 
176 	/* Note: We don't expect to see zip64 extension in the central
177 	 * directory, since the writer knows the actual full size by
178 	 * the time it is ready to write the central directory and has
179 	 * no reason to insert it then.  Info-Zip seems to do the same
180 	 * thing. */
181 
182 	/* Just in case: Report any extra extensions. */
183 	while (p < extension_end) {
184 		failure("Unexpected extension 0x%04X", i2(p));
185 		assert(0);
186 		p += 4 + i2(p + 2);
187 	}
188 
189 	/* Should have run exactly to end of extra data. */
190 	assert(p == extension_end);
191 
192 	assert(p == eocd);
193 	assert(p == eocd_record);
194 
195 	/* Verify local header of file entry. */
196 	p = local_header = buff;
197 	assertEqualMem(p, "PK\003\004", 4); /* Signature */
198 	assertEqualInt(i2(p + 4), zip_version); /* Version needed to extract */
199 	assertEqualInt(i2(p + 6), 8); /* Flags */
200 	assertEqualInt(i2(p + 8), zip_compression); /* Compression method */
201 	assertEqualInt(i2(p + 10), 0); /* File time */
202 	assertEqualInt(i2(p + 12), 33); /* File date */
203 	assertEqualInt(i4(p + 14), 0); /* CRC-32 */
204 	assertEqualInt(i4(p + 18), 0); /* Compressed size */
205 	assertEqualInt(i4(p + 22), 0); /* Uncompressed size */
206 	assertEqualInt(i2(p + 26), strlen(file_name)); /* Pathname length */
207 	assertEqualInt(i2(p + 28), 24); /* Extra field length */
208 	assertEqualMem(p + 30, file_name, strlen(file_name)); /* Pathname */
209 	p = extension_start = local_header + 30 + strlen(file_name);
210 	extension_end = extension_start + i2(local_header + 28);
211 
212 	assertEqualInt(i2(p), 0x7875);  /* 'ux' extension header */
213 	assertEqualInt(i2(p + 2), 11); /* 'ux' size */
214 	assertEqualInt(p[4], 1); /* 'ux' version */
215 	assertEqualInt(p[5], 4); /* 'ux' uid size */
216 	assertEqualInt(i4(p + 6), file_uid); /* 'Ux' UID */
217 	assertEqualInt(p[10], 4); /* 'ux' gid size */
218 	assertEqualInt(i4(p + 11), file_gid); /* 'Ux' GID */
219 	p += 4 + i2(p + 2);
220 
221 	assertEqualInt(i2(p), 0x5455);  /* 'UT' extension header */
222 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
223 	assertEqualInt(p[4], 1); /* 'UT' flags */
224 	assertEqualInt(i4(p + 5), 0); /* 'UT' mtime */
225 	p += 4 + i2(p + 2);
226 
227 	/* Just in case: Report any extra extensions. */
228 	while (p < extension_end) {
229 		failure("Unexpected extension 0x%04X", i2(p));
230 		assert(0);
231 		p += 4 + i2(p + 2);
232 	}
233 
234 	/* Should have run exactly to end of extra data. */
235 	assert(p == extension_end);
236 	data_start = p;
237 
238 	/* Data descriptor should follow compressed data. */
239 	while (p < central_header && memcmp(p, "PK\007\010", 4) != 0)
240 		++p;
241 	data_end = p;
242 	assertEqualInt(data_end - data_start, compressed_size);
243 	assertEqualMem(p, "PK\007\010", 4);
244 	assertEqualInt(i4(p + 4), crc); /* CRC-32 */
245 	assertEqualInt(i4(p + 8), compressed_size); /* compressed size */
246 	assertEqualInt(i4(p + 12), sizeof(file_data)); /* uncompressed size */
247 
248 	/* Central directory should immediately follow the data descriptor. */
249 	assert(p + 16 == central_header);
250 
251 	free(buff);
252 }
253