1 /*-
2  * Copyright (c) 2021 Jia Cheong Tan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
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 /* File data */
30 static const char file_name[] = "file";
31 static const char file_data1[] = {'a', 'b', 'c', 'd', 'e'};
32 static const char file_data2[] = {'f', 'g', 'h', 'i', 'j'};
33 static const int file_perm = 00644;
34 static const short file_uid = 10;
35 static const short file_gid = 20;
36 
37 /* Folder data */
38 static const char folder_name[] = "folder/";
39 static const int folder_perm = 00755;
40 static const short folder_uid = 30;
41 static const short folder_gid = 40;
42 
43 #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1 << 3)
44 
45 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
46 static unsigned i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
47 static unsigned i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
48 
49 static unsigned long
50 bitcrc32(unsigned long c, const void *_p, size_t s)
51 {
52 	/* This is a drop-in replacement for crc32() from zlib.
53 	 * Libarchive should be able to correctly generate
54 	 * uncompressed zip archives (including correct CRCs) even
55 	 * when zlib is unavailable, and this function helps us verify
56 	 * that.  Yes, this is very, very slow and unsuitable for
57 	 * production use, but it's correct, compact, and works well
58 	 * enough for this particular usage.  Libarchive internally
59 	 * uses a much more efficient implementation.  */
60 	const unsigned char *p = _p;
61 	int bitctr;
62 
63 	if (p == NULL)
64 		return (0);
65 
66 	for (; s > 0; --s)
67 	{
68 		c ^= *p++;
69 		for (bitctr = 8; bitctr > 0; --bitctr)
70 		{
71 			if (c & 1)
72 				c = (c >> 1);
73 			else
74 				c = (c >> 1) ^ 0xedb88320;
75 			c ^= 0x80000000;
76 		}
77 	}
78 	return (c);
79 }
80 
81 static void write_archive(struct archive *a)
82 {
83 	struct archive_entry *entry = archive_entry_new();
84 	assert(entry != NULL);
85 
86 	/* Does not set size for file entry */
87 	archive_entry_set_pathname(entry, file_name);
88 	archive_entry_set_mode(entry, S_IFREG | 0644);
89 	archive_entry_set_uid(entry, file_uid);
90 	archive_entry_set_gid(entry, file_gid);
91 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
92 	assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
93 	assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
94 	archive_entry_free(entry);
95 
96 	/* Folder */
97 	assert((entry = archive_entry_new()) != NULL);
98 	archive_entry_set_pathname(entry, folder_name);
99 	archive_entry_set_mode(entry, S_IFDIR | folder_perm);
100 	archive_entry_set_size(entry, 0);
101 	archive_entry_set_uid(entry, folder_uid);
102 	archive_entry_set_gid(entry, folder_gid);
103 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
104 	archive_entry_free(entry);
105 }
106 
107 static void verify_contents(const char *zip_buff, size_t size)
108 {
109 	unsigned long crc = bitcrc32(0, file_data1, sizeof(file_data1));
110 	crc = bitcrc32(crc, file_data2, sizeof(file_data2));
111 
112 	const char *zip_end = zip_buff + size;
113 	/* Since there are no comments, the end of central directory
114     *  is 22 bytes from the end of content */
115 	const char *end_of_central_dir = zip_end - 22;
116 	/* Check for end of central directory signature */
117 	assertEqualMem(end_of_central_dir, "PK\x5\x6", 4);
118 	/* Check for number of disk */
119 	assertEqualInt(i2(end_of_central_dir + 4), 0);
120 	/* Check for disk where central directory starts */
121 	assertEqualInt(i2(end_of_central_dir + 6), 0);
122 	/* Check for number of central directory records on disk */
123 	assertEqualInt(i2(end_of_central_dir + 8), 2);
124 	/* Check for total number of central directory records */
125 	assertEqualInt(i2(end_of_central_dir + 10), 2);
126 	/* Check for size of central directory and offset
127     *  The size + offset must equal the end of the central directory */
128 	assertEqualInt(i4(end_of_central_dir + 12) + i4(end_of_central_dir + 16), end_of_central_dir - zip_buff);
129 	/* Check for empty comment length */
130 	assertEqualInt(i2(end_of_central_dir + 20), 0);
131 
132 	/* Get address of central directory */
133 	const char *central_directory = zip_buff + i4(end_of_central_dir + 16);
134 
135 	/* Check for entry in central directory signature */
136 	assertEqualMem(central_directory, "PK\x1\x2", 4);
137 	/* Check for version used to write entry */
138 	assertEqualInt(i2(central_directory + 4), 3 * 256 + 10);
139 	/* Check for version needed to extract entry */
140 	assertEqualInt(i2(central_directory + 6), 10);
141 	/* Check flags */
142 	assertEqualInt(i2(central_directory + 8), ZIP_ENTRY_FLAG_LENGTH_AT_END);
143 	/* Check compression method */
144 	assertEqualInt(i2(central_directory + 10), 0);
145 	/* Check crc value */
146 	assertEqualInt(i4(central_directory + 16), crc);
147 	/* Check compressed size*/
148 	assertEqualInt(i4(central_directory + 20), sizeof(file_data1) + sizeof(file_data2));
149 	/* Check uncompressed size */
150 	assertEqualInt(i4(central_directory + 24), sizeof(file_data1) + sizeof(file_data2));
151 	/* Check file name length */
152 	assertEqualInt(i2(central_directory + 28), strlen(file_name));
153 	/* Check extra field length */
154 	assertEqualInt(i2(central_directory + 30), 15);
155 	/* Check file comment length */
156 	assertEqualInt(i2(central_directory + 32), 0);
157 	/* Check disk number where file starts */
158 	assertEqualInt(i2(central_directory + 34), 0);
159 	/* Check internal file attrs */
160 	assertEqualInt(i2(central_directory + 36), 0);
161 	/* Check external file attrs */
162 	assertEqualInt(i4(central_directory + 38) >> 16 & 01777, file_perm);
163 	/* Check offset of local header */
164 	assertEqualInt(i4(central_directory + 42), 0);
165 	/* Check for file name contents */
166 	assertEqualMem(central_directory + 46, file_name, strlen(file_name));
167 
168 	/* Get address of local file entry */
169 	const char *local_file_header = zip_buff;
170 
171 	/* Check local file header signature */
172 	assertEqualMem(local_file_header, "PK\x3\x4", 4);
173 	/* Check version needed to extract */
174 	assertEqualInt(i2(local_file_header + 4), 10);
175 	/* Check flags */
176 	assertEqualInt(i2(local_file_header + 6), 8);
177 	/* Check compression method */
178 	assertEqualInt(i2(local_file_header + 8), 0);
179 	/* Check crc */
180 	assertEqualInt(i4(local_file_header + 14), 0);
181 	/* Check compressed size
182     *  0 because it was unknown at time of writing */
183 	assertEqualInt(i4(local_file_header + 18), 0);
184 	/* Check uncompressed size
185     *  0 because it was unknown at time of writing */
186 	assertEqualInt(i4(local_file_header + 22), 0);
187 	/* Check pathname length */
188 	assertEqualInt(i2(local_file_header + 26), strlen(file_name));
189 	/* Check extra field length */
190 	assertEqualInt(i2(local_file_header + 28), 15);
191 	/* Check path name match */
192 	assertEqualMem(local_file_header + 30, file_name, strlen(file_name));
193 
194 	/* Start of data */
195 	const char *data = local_file_header + i2(local_file_header + 28) + strlen(file_name) + 30;
196 	/* Check for file data match */
197 	assertEqualMem(data, file_data1, sizeof(file_data1));
198 	assertEqualMem(data + sizeof(file_data1), file_data2, sizeof(file_data2));
199 
200 	/* Start of data descriptor */
201 	const char *data_descriptor = data + sizeof(file_data1) + sizeof(file_data2);
202 	/* Check data descriptor signature */
203 	assertEqualMem(data_descriptor, "PK\x7\x8", 4);
204 	/* Check crc value */
205 	assertEqualInt(i4(data_descriptor + 4), crc);
206 	/* Check compressed size */
207 	assertEqualInt(i4(data_descriptor + 8), sizeof(file_data1) + sizeof(file_data2));
208 	/* Chcek uncompresed size */
209 	assertEqualInt(i4(data_descriptor + 12), sizeof(file_data1) + sizeof(file_data2));
210 
211 	/* Get folder entry in central directory */
212 	const char *central_directory_folder_entry = central_directory + 46 + i2(local_file_header + 28) + strlen(file_name);
213 
214 	/* Get start of folder entry */
215 	const char *local_folder_header = data_descriptor + 16;
216 
217 	/* Check for entry in central directory signature */
218 	assertEqualMem(central_directory_folder_entry, "PK\x1\x2", 4);
219 	/* Check version made by */
220 	assertEqualInt(i2(central_directory_folder_entry + 4), 3 * 256 + 20);
221 	/* Check version needed to extract */
222 	assertEqualInt(i2(central_directory_folder_entry + 6), 20);
223 	/* Check flags */
224 	assertEqualInt(i2(central_directory_folder_entry + 8), 0);
225 	/* Check compression method */
226 	assertEqualInt(i2(central_directory_folder_entry + 10), 0);
227 	/* Check crc */
228 	assertEqualInt(i2(central_directory_folder_entry + 16), 0);
229 	/* Check compressed size */
230 	assertEqualInt(i4(central_directory_folder_entry + 20), 0);
231 	/* Check uncompressed size */
232 	assertEqualInt(i4(central_directory_folder_entry + 24), 0);
233 	/* Check path name length */
234 	assertEqualInt(i2(central_directory_folder_entry + 28), strlen(folder_name));
235 	/* Check extra field length */
236 	assertEqualInt(i2(central_directory_folder_entry + 30), 15);
237 	/* Check file comment length */
238 	assertEqualInt(i2(central_directory_folder_entry + 32), 0);
239 	/* Check disk number start */
240 	assertEqualInt(i2(central_directory_folder_entry + 34), 0);
241 	/* Check internal file attrs */
242 	assertEqualInt(i2(central_directory_folder_entry + 36), 0);
243 	/* Check external file attrs */
244 	assertEqualInt(i4(central_directory_folder_entry + 38) >> 16 & 01777, folder_perm);
245 	/* Check offset of local header*/
246 	assertEqualInt(i4(central_directory_folder_entry + 42), local_folder_header - zip_buff);
247 	/* Check path name */
248 	assertEqualMem(central_directory_folder_entry + 46, folder_name, strlen(folder_name));
249 
250 	/* Check local header */
251 	assertEqualMem(local_folder_header, "PK\x3\x4", 4);
252 	/* Check version to extract */
253 	assertEqualInt(i2(local_folder_header + 4), 20);
254 	/* Check flags */
255 	assertEqualInt(i2(local_folder_header + 6), 0);
256 	/* Check compression method */
257 	assertEqualInt(i2(local_folder_header + 8), 0);
258 	/* Check crc */
259 	assertEqualInt(i4(local_folder_header + 14), 0);
260 	/* Check compressed size */
261 	assertEqualInt(i2(local_folder_header + 18), 0);
262 	/* Check uncompressed size */
263 	assertEqualInt(i4(local_folder_header + 22), 0);
264 	/* Check path name length */
265 	assertEqualInt(i2(local_folder_header + 26), strlen(folder_name));
266 	/* Check extra field length */
267 	assertEqualInt(i2(local_folder_header + 28), 15);
268 	/* Check path name */
269 	assertEqualMem(local_folder_header + 30, folder_name, strlen(folder_name));
270 
271 	const char *post_local_folder = local_folder_header + 30 + i2(local_folder_header + 28) + strlen(folder_name);
272 	assertEqualMem(post_local_folder, central_directory, 4);
273 }
274 
275 DEFINE_TEST(test_write_format_zip_size_unset)
276 {
277 	struct archive *a;
278 	char zip_buffer[100000];
279 	size_t size;
280 
281 	/* Use compression=store to disable compression. */
282 	assert((a = archive_write_new()) != NULL);
283 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
284 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:compression=store"));
285 	/* Disable zip64 explicitly since it is automatically enabled if no size is set */
286 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64="));
287 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
288 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
289 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
290 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, zip_buffer, sizeof(zip_buffer), &size));
291 
292 	write_archive(a);
293 
294 	/* Close the archive . */
295 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
296 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
297 	dumpfile("constructed_size_unset.zip", zip_buffer, size);
298 
299 	verify_contents(zip_buffer, size);
300 
301 	/* Use compression-level=0 to disable compression. */
302 	assert((a = archive_write_new()) != NULL);
303 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
304 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:compression-level=0"));
305 	/* Disable zip64 explicitly since it is automatically enabled if no size is set */
306 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64="));
307 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
308 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
309 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
310 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, zip_buffer, sizeof(zip_buffer), &size));
311 
312 	write_archive(a);
313 
314 	/* Close the archive . */
315 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
316 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
317 	dumpfile("constructed_size_unset.zip", zip_buffer, size);
318 
319 	verify_contents(zip_buffer, size);
320 }
321