xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip_compression_zstd.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-SPDX-License-Identifier: BSD-2-Clause
2  * Copyright (c) 2024 ARJANEN Loïc Jean David
3  * All rights reserved.
4  */
5 
6 #include "test.h"
7 #ifdef HAVE_ZSTD_H
8 #include <zstd.h>
9 
10 /* File data */
11 static const char file_name[] = "file";
12 static const char file_data1[] = {'~', 'Z', '`', '^', 'Y', 'X', 'N', 'W', 'V', 'G', 'H', 'I', 'J'};
13 static const char file_data2[] = {'U', 'T', 'S', 'M', 'R', 'Q', 'P', 'O', 'K', 'L'};
14 static const int file_perm = 00644;
15 static const short file_uid = 10;
16 static const short file_gid = 20;
17 
18 /* Folder data */
19 static const char folder_name[] = "folder/";
20 static const int folder_perm = 00755;
21 static const short folder_uid = 30;
22 static const short folder_gid = 40;
23 
24 static time_t now;
25 
verify_write_zstd(struct archive * a)26 static void verify_write_zstd(struct archive *a)
27 {
28 	struct archive_entry *entry;
29 
30 	/* Write entries. */
31 
32 	/* Regular file */
33 	assert((entry = archive_entry_new()) != NULL);
34 	archive_entry_set_pathname(entry, file_name);
35 	archive_entry_set_mode(entry, S_IFREG | 0644);
36 	archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2));
37 	archive_entry_set_uid(entry, file_uid);
38 	archive_entry_set_gid(entry, file_gid);
39 	archive_entry_set_mtime(entry, now, 0);
40 	archive_entry_set_atime(entry, now + 3, 0);
41 	assertEqualIntA(a, 0, archive_write_header(a, entry));
42 	assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
43 	assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
44 	archive_entry_free(entry);
45 
46 	/* Folder */
47 	assert((entry = archive_entry_new()) != NULL);
48 	archive_entry_set_pathname(entry, folder_name);
49 	archive_entry_set_mode(entry, S_IFDIR | folder_perm);
50 	archive_entry_set_size(entry, 0);
51 	archive_entry_set_uid(entry, folder_uid);
52 	archive_entry_set_gid(entry, folder_gid);
53 	archive_entry_set_mtime(entry, now, 0);
54 	archive_entry_set_ctime(entry, now + 5, 0);
55 	assertEqualIntA(a, 0, archive_write_header(a, entry));
56 	archive_entry_free(entry);
57 }
58 
verify_zstd_contents(const char * buff,size_t used)59 static void verify_zstd_contents(const char *buff, size_t used)
60 {
61 	const char *buffend;
62 	struct archive* zip_archive;
63 	struct archive_entry *ae;
64 	char filedata[sizeof(file_data1) + sizeof(file_data2)];
65 	/* Misc variables */
66 	unsigned long crc;
67 	struct tm *tm;
68 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
69 	struct tm tmbuf;
70 #endif
71 	/* p is the pointer to walk over the central directory,
72 	 * q walks over the local headers, the data and the data descriptors. */
73 	const char *p, *q, *local_header, *extra_start;
74 
75 #if defined(HAVE_LOCALTIME_S)
76 	tm = localtime_s(&tmbuf, &now) ? NULL : &tmbuf;
77 #elif defined(HAVE_LOCALTIME_R)
78 	tm = localtime_r(&now, &tmbuf);
79 #else
80 	tm = localtime(&now);
81 #endif
82 
83 	/* Open archive from memory, we'll need it for checking the file
84 	 * value */
85 	assert((zip_archive = archive_read_new()) != NULL);
86 	assertEqualIntA(zip_archive, ARCHIVE_OK, archive_read_support_format_zip(zip_archive));
87 	assertEqualIntA(zip_archive, ARCHIVE_OK, archive_read_support_filter_all(zip_archive));
88 	assertEqualIntA(zip_archive, ARCHIVE_OK, archive_read_open_memory(zip_archive, buff, used));
89 
90 	/* Remember the end of the archive in memory. */
91 	buffend = buff + used;
92 
93 	/* Verify "End of Central Directory" record. */
94 	/* Get address of end-of-central-directory record. */
95 	p = buffend - 22; /* Assumes there is no zip comment field. */
96 	failure("End-of-central-directory begins with PK\\005\\006 signature");
97 	assertEqualMem(p, "PK\005\006", 4);
98 	failure("This must be disk 0");
99 	assertEqualInt(i2le(p + 4), 0);
100 	failure("Central dir must start on disk 0");
101 	assertEqualInt(i2le(p + 6), 0);
102 	failure("All central dir entries are on this disk");
103 	assertEqualInt(i2le(p + 8), i2le(p + 10));
104 	failure("CD start (%u) + CD length (%u) should == archive size - 22",
105 	    i4le(p + 12), i4le(p + 16));
106 	assertEqualInt(i4le(p + 12) + i4le(p + 16), used - 22);
107 	failure("no zip comment");
108 	assertEqualInt(i2le(p + 20), 0);
109 
110 	/* Get address of first entry in central directory. */
111 	p = buff + i4le(buffend - 6);
112 	failure("Central file record at offset %u should begin with"
113 	    " PK\\001\\002 signature",
114 	    i4le(buffend - 10));
115 
116 	/* Verify file entry in central directory, except compressed size (offset 20). */
117 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
118 	assertEqualInt(i2le(p + 4), 3 * 256 + 63); /* Version made by */
119 	assertEqualInt(i2le(p + 6), 63); /* Version needed to extract */
120 	assertEqualInt(i2le(p + 8), 8); /* Flags */
121 	assertEqualInt(i2le(p + 10), 93); /* Compression method */
122 	assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
123 	assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
124 	crc = bitcrc32(0, file_data1, sizeof(file_data1));
125 	crc = bitcrc32(crc, file_data2, sizeof(file_data2));
126 	assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
127 	assertEqualInt(i4le(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
128 	assertEqualInt(i2le(p + 28), strlen(file_name)); /* Pathname length */
129 	assertEqualInt(i2le(p + 30), 24); /* Extra field length */
130 	assertEqualInt(i2le(p + 32), 0); /* File comment length */
131 	assertEqualInt(i2le(p + 34), 0); /* Disk number start */
132 	assertEqualInt(i2le(p + 36), 0); /* Internal file attrs */
133 	assertEqualInt(i4le(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
134 	assertEqualInt(i4le(p + 42), 0); /* Offset of local header */
135 	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
136 	p = p + 46 + strlen(file_name);
137 
138 	assertEqualInt(i2le(p), 0x7875); /* 'ux' extension header */
139 	assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
140 /* TODO */
141 	p = p + 4 + i2le(p + 2);
142 
143 	assertEqualInt(i2le(p), 0x5455); /* 'UT' extension header */
144 	assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
145 	assertEqualInt(p[4], 1); /* 'UT' flags */
146 	assertEqualInt(i4le(p + 5), now); /* 'UT' mtime */
147 	p = p + 4 + i2le(p + 2);
148 
149 	/* Verify local header of file entry. */
150 	local_header = q = buff;
151 	assertEqualMem(q, "PK\003\004", 4); /* Signature */
152 	assertEqualInt(i2le(q + 4), 63); /* Version needed to extract */
153 	assertEqualInt(i2le(q + 6), 8); /* Flags: bit 3 = length-at-end (required because CRC32 is unknown) */
154 	assertEqualInt(i2le(q + 8), 93); /* Compression method */
155 	assertEqualInt(i2le(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
156 	assertEqualInt(i2le(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
157 	assertEqualInt(i4le(q + 14), 0); /* CRC-32 */
158 	assertEqualInt(i4le(q + 18), 0); /* Compressed size, must be zero because of length-at-end */
159 	assertEqualInt(i4le(q + 22), 0); /* Uncompressed size, must be zero because of length-at-end */
160 	assertEqualInt(i2le(q + 26), strlen(file_name)); /* Pathname length */
161 	assertEqualInt(i2le(q + 28), 41); /* Extra field length */
162 	assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */
163 	extra_start = q = q + 30 + strlen(file_name);
164 
165 	assertEqualInt(i2le(q), 0x7875); /* 'ux' extension header */
166 	assertEqualInt(i2le(q + 2), 11); /* 'ux' size */
167 	assertEqualInt(q[4], 1); /* 'ux' version */
168 	assertEqualInt(q[5], 4); /* 'ux' uid size */
169 	assertEqualInt(i4le(q + 6), file_uid); /* 'Ux' UID */
170 	assertEqualInt(q[10], 4); /* 'ux' gid size */
171 	assertEqualInt(i4le(q + 11), file_gid); /* 'Ux' GID */
172 	q = q + 4 + i2le(q + 2);
173 
174 	assertEqualInt(i2le(q), 0x5455); /* 'UT' extension header */
175 	assertEqualInt(i2le(q + 2), 9); /* 'UT' size */
176 	assertEqualInt(q[4], 3); /* 'UT' flags */
177 	assertEqualInt(i4le(q + 5), now); /* 'UT' mtime */
178 	assertEqualInt(i4le(q + 9), now + 3); /* 'UT' atime */
179 	q = q + 4 + i2le(q + 2);
180 
181 	assertEqualInt(i2le(q), 0x6c78); /* 'xl' experimental extension header */
182 	assertEqualInt(i2le(q + 2), 9); /* size */
183 	assertEqualInt(q[4], 7); /* Bitmap of fields included. */
184 	assertEqualInt(i2le(q + 5) >> 8, 3); /* system & version made by */
185 	assertEqualInt(i2le(q + 7), 0); /* internal file attributes */
186 	assertEqualInt(i4le(q + 9) >> 16 & 01777, file_perm); /* external file attributes */
187 	q = q + 4 + i2le(q + 2);
188 
189 	assert(q == extra_start + i2le(local_header + 28));
190 	q = extra_start + i2le(local_header + 28);
191 
192 	/* Verify data of file entry, using our own zip reader to test. */
193 	assertEqualIntA(zip_archive, ARCHIVE_OK, archive_read_next_header(zip_archive, &ae));
194 	assertEqualString("file", archive_entry_pathname(ae));
195 	assertEqualIntA(zip_archive, sizeof(filedata), archive_read_data(zip_archive, filedata, sizeof(filedata)));
196 	assertEqualMem(filedata, file_data1, sizeof(file_data1));
197 	assertEqualMem(filedata + sizeof(file_data1), file_data2,
198 		sizeof(file_data2));
199 
200 	/* Skip data of file entry in q */
201 	while (q < buffend - 3) {
202 		if (memcmp(q, "PK\007\010", 4) == 0) {
203 			break;
204 		}
205 		q++;
206 	}
207 
208 	/* Verify data descriptor of file entry, except compressed size (offset 8). */
209 	assertEqualMem(q, "PK\007\010", 4); /* Signature */
210 	assertEqualInt(i4le(q + 4), crc); /* CRC-32 */
211 	assertEqualInt(i4le(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
212 	q = q + 16;
213 
214 	/* Verify folder entry in central directory. */
215 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
216 	assertEqualInt(i2le(p + 4), 3 * 256 + 20); /* Version made by */
217 	assertEqualInt(i2le(p + 6), 20); /* Version needed to extract */
218 	assertEqualInt(i2le(p + 8), 0); /* Flags */
219 	assertEqualInt(i2le(p + 10), 0); /* Compression method */
220 	assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
221 	assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
222 	crc = 0;
223 	assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
224 	assertEqualInt(i4le(p + 20), 0); /* Compressed size */
225 	assertEqualInt(i4le(p + 24), 0); /* Uncompressed size */
226 	assertEqualInt(i2le(p + 28), strlen(folder_name)); /* Pathname length */
227 	assertEqualInt(i2le(p + 30), 24); /* Extra field length */
228 	assertEqualInt(i2le(p + 32), 0); /* File comment length */
229 	assertEqualInt(i2le(p + 34), 0); /* Disk number start */
230 	assertEqualInt(i2le(p + 36), 0); /* Internal file attrs */
231 	assertEqualInt(i4le(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
232 	assertEqualInt(i4le(p + 42), q - buff); /* Offset of local header */
233 	assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
234 	p = p + 46 + strlen(folder_name);
235 
236 	assertEqualInt(i2le(p), 0x7875); /* 'ux' extension header */
237 	assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
238 	assertEqualInt(p[4], 1); /* 'ux' version */
239 	assertEqualInt(p[5], 4); /* 'ux' uid size */
240 	assertEqualInt(i4le(p + 6), folder_uid); /* 'ux' UID */
241 	assertEqualInt(p[10], 4); /* 'ux' gid size */
242 	assertEqualInt(i4le(p + 11), folder_gid); /* 'ux' GID */
243 	p = p + 4 + i2le(p + 2);
244 
245 	assertEqualInt(i2le(p), 0x5455); /* 'UT' extension header */
246 	assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
247 	assertEqualInt(p[4], 1); /* 'UT' flags */
248 	assertEqualInt(i4le(p + 5), now); /* 'UT' mtime */
249 	p = p + 4 + i2le(p + 2);
250 
251 	/* Verify local header of folder entry. */
252 	local_header = q;
253 	assertEqualMem(q, "PK\003\004", 4); /* Signature */
254 	assertEqualInt(i2le(q + 4), 20); /* Version needed to extract */
255 	assertEqualInt(i2le(q + 6), 0); /* Flags */
256 	assertEqualInt(i2le(q + 8), 0); /* Compression method */
257 	assertEqualInt(i2le(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
258 	assertEqualInt(i2le(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
259 	assertEqualInt(i4le(q + 14), 0); /* CRC-32 */
260 	assertEqualInt(i4le(q + 18), 0); /* Compressed size */
261 	assertEqualInt(i4le(q + 22), 0); /* Uncompressed size */
262 	assertEqualInt(i2le(q + 26), strlen(folder_name)); /* Pathname length */
263 	assertEqualInt(i2le(q + 28), 41); /* Extra field length */
264 	assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
265 	extra_start = q = q + 30 + strlen(folder_name);
266 
267 	assertEqualInt(i2le(q), 0x7875); /* 'ux' extension header */
268 	assertEqualInt(i2le(q + 2), 11); /* 'ux' size */
269 	assertEqualInt(q[4], 1); /* 'ux' version */
270 	assertEqualInt(q[5], 4); /* 'ux' uid size */
271 	assertEqualInt(i4le(q + 6), folder_uid); /* 'ux' UID */
272 	assertEqualInt(q[10], 4); /* 'ux' gid size */
273 	assertEqualInt(i4le(q + 11), folder_gid); /* 'ux' GID */
274 	q = q + 4 + i2le(q + 2);
275 
276 	assertEqualInt(i2le(q), 0x5455); /* 'UT' extension header */
277 	assertEqualInt(i2le(q + 2), 9); /* 'UT' size */
278 	assertEqualInt(q[4], 5); /* 'UT' flags */
279 	assertEqualInt(i4le(q + 5), now); /* 'UT' mtime */
280 	assertEqualInt(i4le(q + 9), now + 5); /* 'UT' atime */
281 	q = q + 4 + i2le(q + 2);
282 
283 	assertEqualInt(i2le(q), 0x6c78); /* 'xl' experimental extension header */
284 	assertEqualInt(i2le(q + 2), 9); /* size */
285 	assertEqualInt(q[4], 7); /* bitmap of fields */
286 	assertEqualInt(i2le(q + 5) >> 8, 3); /* system & version made by */
287 	assertEqualInt(i2le(q + 7), 0); /* internal file attributes */
288 	assertEqualInt(i4le(q + 9) >> 16 & 01777, folder_perm); /* external file attributes */
289 	q = q + 4 + i2le(q + 2);
290 
291 	assert(q == extra_start + i2le(local_header + 28));
292 	q = extra_start + i2le(local_header + 28);
293 
294 	/* There should not be any data in the folder entry,
295 	 * so the first central directory entry should be next: */
296 	assertEqualMem(q, "PK\001\002", 4); /* Signature */
297 
298 	/* Close archive, in case. */
299 	archive_read_free(zip_archive);
300 }
301 
302 #endif /* HAVE_ZSTD_H */
DEFINE_TEST(test_write_format_zip_compression_zstd)303 DEFINE_TEST(test_write_format_zip_compression_zstd)
304 {
305 #ifndef HAVE_ZSTD_H
306 	skipping("zstd is not fully supported on this platform");
307 #else /* HAVE_ZSTD_H */
308 	/* Buffer data */
309 	struct archive *a;
310 	char buff[100000];
311 	size_t used;
312 
313 	/* Time data */
314 	now = time(NULL);
315 
316 	/* Create new ZIP archive in memory without padding. */
317 	/* Use the setter function to use ZSTD compression. */
318 	assert((a = archive_write_new()) != NULL);
319 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
320 	assertEqualIntA(a, ARCHIVE_OK, archive_write_zip_set_compression_zstd(a));
321 	assertEqualIntA(a, ARCHIVE_OK,
322 	    archive_write_set_options(a, "zip:experimental"));
323 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
324 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
325 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
326 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
327 
328 	verify_write_zstd(a);
329 
330 	/* Close the archive . */
331 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
332 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
333 	dumpfile("constructed.zip", buff, used);
334 
335 	verify_zstd_contents(buff, used);
336 
337 	/* Create new ZIP archive in memory without padding. */
338 	/* Use compression-level=1 to check that compression levels are
339 	 * somewhat supported as well as threads=2 to check the multi-threaded
340 	 * encoder, if available. */
341 	assert((a = archive_write_new()) != NULL);
342 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
343 	assertEqualIntA(a, ARCHIVE_OK,
344 	    archive_write_set_options(a, "zip:compression=zstd"));
345 	assertEqualIntA(a, ARCHIVE_OK,
346 	    archive_write_set_options(a, "zip:compression-level=1"));
347 	assertEqualIntA(a, ARCHIVE_OK,
348 	    archive_write_set_options(a, "zip:threads=2"));
349 	assertEqualIntA(a, ARCHIVE_OK,
350 	    archive_write_set_options(a, "zip:experimental"));
351 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
352 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
353 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
354 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
355 
356 	verify_write_zstd(a);
357 
358 	/* Close the archive. */
359 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
360 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
361 	dumpfile("constructed.zip", buff, used);
362 
363 	verify_zstd_contents(buff, used);
364 #endif /* HAVE_ZSTD_H */
365 }
366 
367