xref: /freebsd/contrib/libarchive/libarchive/test/test_read_format_7zip.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2011 Michihiro NAKAJIMA
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 #if HAVE_LZMA_H
28 #include <lzma.h>
29 #endif
30 
31 #if defined(_WIN32) && !defined(__CYGWIN__)
32 #define	close		_close
33 #define	open		_open
34 #endif
35 
36 #define __LIBARCHIVE_BUILD
37 
38 /*
39  * Extract a non-encoded file.
40  * The header of the 7z archive files is not encoded.
41  */
42 static void
test_copy(int use_open_fd)43 test_copy(int use_open_fd)
44 {
45 	const char *refname = "test_read_format_7zip_copy.7z";
46 	struct archive_entry *ae;
47 	struct archive *a;
48 	char buff[128];
49 	int fd = -1;
50 
51 	extract_reference_file(refname);
52 	assert((a = archive_read_new()) != NULL);
53 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
54 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
55 	if (use_open_fd) {
56 		fd = open(refname, O_RDONLY | O_BINARY);
57 		assertEqualIntA(a, ARCHIVE_OK,
58 		    archive_read_open_fd(a, fd, 10240));
59 	} else {
60 		assertEqualIntA(a, ARCHIVE_OK,
61 		    archive_read_open_filename(a, refname, 10240));
62 	}
63 
64 	/* Verify regular file1. */
65 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
66 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
67 	assertEqualString("file1", archive_entry_pathname(ae));
68 	assertEqualInt(86401, archive_entry_mtime(ae));
69 	assertEqualInt(60, archive_entry_size(ae));
70 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
71 	assert(archive_read_has_encrypted_entries(a) > ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
72 	assertEqualInt(60, archive_read_data(a, buff, sizeof(buff)));
73 	assertEqualMem(buff, "    ", 4);
74 
75 	assertEqualInt(1, archive_file_count(a));
76 
77 	/* End of archive. */
78 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
79 
80 	/* Verify archive format. */
81 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
82 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
83 
84 	/* Close the archive. */
85 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
86 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
87 	if (fd != -1)
88 		close(fd);
89 }
90 
91 /*
92  * An archive file has no entry.
93  */
94 static void
test_empty_archive(void)95 test_empty_archive(void)
96 {
97 	const char *refname = "test_read_format_7zip_empty_archive.7z";
98 	struct archive_entry *ae;
99 	struct archive *a;
100 
101 	extract_reference_file(refname);
102 	assert((a = archive_read_new()) != NULL);
103 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
104 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
105 	assertEqualIntA(a, ARCHIVE_OK,
106 	    archive_read_open_filename(a, refname, 10240));
107 
108 	/* End of archive. */
109 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
110 
111 	assertEqualInt(0, archive_file_count(a));
112 
113 	/* Verify archive format. */
114 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
115 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
116 
117 	/* Close the archive. */
118 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
119 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
120 }
121 
122 /*
123  * An archive file has one empty file. It means there is no content
124  * in the archive file except for a header.
125  */
126 static void
test_empty_file(void)127 test_empty_file(void)
128 {
129 	const char *refname = "test_read_format_7zip_empty_file.7z";
130 	struct archive_entry *ae;
131 	struct archive *a;
132 
133 	extract_reference_file(refname);
134 	assert((a = archive_read_new()) != NULL);
135 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
136 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
137 	assertEqualIntA(a, ARCHIVE_OK,
138 	    archive_read_open_filename(a, refname, 10240));
139 
140 	/* Verify regular empty. */
141 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
142 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
143 	assertEqualString("empty", archive_entry_pathname(ae));
144 	assertEqualInt(86401, archive_entry_mtime(ae));
145 	assertEqualInt(0, archive_entry_size(ae));
146 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
147 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
148 
149 	assertEqualInt(1, archive_file_count(a));
150 
151 	/* End of archive. */
152 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
153 
154 	/* Verify archive format. */
155 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
156 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
157 
158 	/* Close the archive. */
159 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
160 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
161 }
162 
163 /*
164  * Extract an encoded file.
165  * The header of the 7z archive files is not encoded.
166  */
167 static void
test_plain_header(const char * refname)168 test_plain_header(const char *refname)
169 {
170 	struct archive_entry *ae;
171 	struct archive *a;
172 	char buff[128];
173 
174 	extract_reference_file(refname);
175 	assert((a = archive_read_new()) != NULL);
176 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
177 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
178 	assertEqualIntA(a, ARCHIVE_OK,
179 	    archive_read_open_filename(a, refname, 10240));
180 
181 	/* Verify regular file1. */
182 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
183 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
184 	assertEqualString("file1", archive_entry_pathname(ae));
185 	assertEqualInt(1322058763, archive_entry_mtime(ae));
186 	assertEqualInt(2844, archive_entry_size(ae));
187 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
188 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
189 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
190 	assertEqualMem(buff, "The libarchive distribution ", 28);
191 
192 	assertEqualInt(1, archive_file_count(a));
193 
194 	/* End of archive. */
195 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
196 
197 	/* Verify archive format. */
198 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
199 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
200 
201 	/* Close the archive. */
202 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
203 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
204 }
205 
206 /*
207  * Extract multi files.
208  * The header of the 7z archive files is encoded with LZMA.
209  */
210 static void
test_extract_all_files(const char * refname)211 test_extract_all_files(const char *refname)
212 {
213 	struct archive_entry *ae;
214 	struct archive *a;
215 	char buff[128];
216 
217 	extract_reference_file(refname);
218 	assert((a = archive_read_new()) != NULL);
219 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
220 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
221 	assertEqualIntA(a, ARCHIVE_OK,
222 	    archive_read_open_filename(a, refname, 10240));
223 
224 	/* Verify regular file1. */
225 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
226 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
227 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
228 	assertEqualInt(86401, archive_entry_mtime(ae));
229 	assertEqualInt(13, archive_entry_size(ae));
230 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
231 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
232 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
233 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
234 
235 	/* Verify regular file2. */
236 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
237 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
238 	assertEqualString("file2", archive_entry_pathname(ae));
239 	assertEqualInt(86401, archive_entry_mtime(ae));
240 	assertEqualInt(26, archive_entry_size(ae));
241 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
242 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
243 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
244 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
245 
246 	/* Verify regular file3. */
247 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
248 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
249 	assertEqualString("file3", archive_entry_pathname(ae));
250 	assertEqualInt(86401, archive_entry_mtime(ae));
251 	assertEqualInt(39, archive_entry_size(ae));
252 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
253 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
254 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
255 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
256 
257 	/* Verify regular file4. */
258 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
259 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
260 	assertEqualString("file4", archive_entry_pathname(ae));
261 	assertEqualInt(86401, archive_entry_mtime(ae));
262 	assertEqualInt(52, archive_entry_size(ae));
263 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
264 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
265 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
266 	assertEqualMem(buff,
267 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
268 
269 	/* Verify directory dir1. */
270 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
271 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
272 	assertEqualString("dir1/", archive_entry_pathname(ae));
273 	assertEqualInt(2764801, archive_entry_mtime(ae));
274 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
275 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
276 
277 	assertEqualInt(5, archive_file_count(a));
278 
279 	/* End of archive. */
280 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
281 
282 	/* Verify archive format. */
283 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
284 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
285 
286 	/* Close the archive. */
287 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
288 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
289 }
290 
291 /*
292  * Extract multi files.
293  * Like test_extract_all_files, but with zstandard compression.
294  */
295 static void
test_extract_all_files_zstd(const char * refname)296 test_extract_all_files_zstd(const char *refname)
297 {
298 	struct archive_entry *ae;
299 	struct archive *a;
300 	char buff[128];
301 
302 	extract_reference_file(refname);
303 	assert((a = archive_read_new()) != NULL);
304 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
305 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
306 	assertEqualIntA(a, ARCHIVE_OK,
307 	    archive_read_open_filename(a, refname, 10240));
308 
309 	/* Verify directory dir1. Note that this comes before the dir1/file1 entry in recent versions of 7-Zip. */
310 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
311 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
312 	assertEqualString("dir1/", archive_entry_pathname(ae));
313 	assertEqualInt(2764801, archive_entry_mtime(ae));
314 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
315 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
316 
317 	/* Verify regular file1. */
318 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
319 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
320 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
321 	assertEqualInt(86401, archive_entry_mtime(ae));
322 	assertEqualInt(13, archive_entry_size(ae));
323 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
324 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
325 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
326 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
327 
328 	/* Verify regular file2. */
329 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
330 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
331 	assertEqualString("file2", archive_entry_pathname(ae));
332 	assertEqualInt(86401, archive_entry_mtime(ae));
333 	assertEqualInt(26, archive_entry_size(ae));
334 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
335 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
336 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
337 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
338 
339 	/* Verify regular file3. */
340 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
341 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
342 	assertEqualString("file3", archive_entry_pathname(ae));
343 	assertEqualInt(86401, archive_entry_mtime(ae));
344 	assertEqualInt(39, archive_entry_size(ae));
345 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
346 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
347 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
348 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
349 
350 	/* Verify regular file4. */
351 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
352 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
353 	assertEqualString("file4", archive_entry_pathname(ae));
354 	assertEqualInt(86401, archive_entry_mtime(ae));
355 	assertEqualInt(52, archive_entry_size(ae));
356 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
357 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
358 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
359 	assertEqualMem(buff,
360 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
361 
362 	assertEqualInt(5, archive_file_count(a));
363 
364 	/* End of archive. */
365 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
366 
367 	/* Verify archive format. */
368 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
369 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
370 
371 	/* Close the archive. */
372 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
373 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
374 }
375 
376 /*
377  * Extract file from an archives using ZSTD compression with and without BCJ.
378  */
379 static void
test_extract_file_zstd_bcj_nobjc(const char * refname)380 test_extract_file_zstd_bcj_nobjc(const char *refname)
381 {
382 	struct archive_entry *ae;
383 	struct archive *a;
384 	char buff[4096];
385 	uint32_t computed_crc = 0;
386 	uint32_t expected_crc = 0xbd66eebc;
387 
388 	extract_reference_file(refname);
389 	assert((a = archive_read_new()) != NULL);
390 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
391 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
392 	assertEqualIntA(a, ARCHIVE_OK,
393 	    archive_read_open_filename(a, refname, 10240));
394 
395 	/* Verify regular file: hw. */
396 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
397 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
398 	assertEqualString("hw", archive_entry_pathname(ae));
399 	assertEqualInt(1685913368, archive_entry_mtime(ae));
400 	assertEqualInt(15952, archive_entry_size(ae));
401 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
402 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
403 
404 	for (;;) {
405 		la_ssize_t bytes_read = archive_read_data(a, buff, sizeof(buff));
406 		assert(bytes_read >= 0);
407 		if (bytes_read == 0) break;
408 		computed_crc = bitcrc32(computed_crc, buff, bytes_read);
409 	}
410 	assertEqualInt(computed_crc, expected_crc);
411 
412 	assertEqualInt(1, archive_file_count(a));
413 
414 	/* End of archive. */
415 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
416 
417 	/* Verify archive format. */
418 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
419 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
420 
421 	/* Close the archive. */
422 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
423 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
424 }
425 
426 /*
427  * Extract last file.
428  * The header of the 7z archive files is encoded with LZMA.
429  */
430 static void
test_extract_last_file(const char * refname)431 test_extract_last_file(const char *refname)
432 {
433 	struct archive_entry *ae;
434 	struct archive *a;
435 	char buff[128];
436 
437 	extract_reference_file(refname);
438 	assert((a = archive_read_new()) != NULL);
439 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
440 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
441 	assertEqualIntA(a, ARCHIVE_OK,
442 	    archive_read_open_filename(a, refname, 10240));
443 
444 	/* Verify regular file1. */
445 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
446 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
447 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
448 	assertEqualInt(86401, archive_entry_mtime(ae));
449 	assertEqualInt(13, archive_entry_size(ae));
450 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
451 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
452 
453 	/* Verify regular file2. */
454 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
455 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
456 	assertEqualString("file2", archive_entry_pathname(ae));
457 	assertEqualInt(86401, archive_entry_mtime(ae));
458 	assertEqualInt(26, archive_entry_size(ae));
459 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
460 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
461 
462 	/* Verify regular file3. */
463 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
464 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
465 	assertEqualString("file3", archive_entry_pathname(ae));
466 	assertEqualInt(86401, archive_entry_mtime(ae));
467 	assertEqualInt(39, archive_entry_size(ae));
468 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
469 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
470 
471 	/* Verify regular file4. */
472 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
473 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
474 	assertEqualString("file4", archive_entry_pathname(ae));
475 	assertEqualInt(86401, archive_entry_mtime(ae));
476 	assertEqualInt(52, archive_entry_size(ae));
477 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
478 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
479 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
480 	assertEqualMem(buff,
481 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
482 
483 	/* Verify directory dir1. */
484 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
485 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
486 	assertEqualString("dir1/", archive_entry_pathname(ae));
487 	assertEqualInt(2764801, archive_entry_mtime(ae));
488 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
489 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
490 
491 	assertEqualInt(5, archive_file_count(a));
492 
493 	/* End of archive. */
494 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
495 
496 	/* Verify archive format. */
497 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
498 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
499 
500 	/* Close the archive. */
501 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
502 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
503 }
504 
505 /*
506  * Extract a mixed archive file which has both LZMA and LZMA2 encoded files.
507  *  LZMA: file1, file2, file3, file4
508  *  LZMA2: zfile1, zfile2, zfile3, zfile4
509  */
510 static void
test_extract_all_files2(const char * refname)511 test_extract_all_files2(const char *refname)
512 {
513 	struct archive_entry *ae;
514 	struct archive *a;
515 	char buff[128];
516 
517 	extract_reference_file(refname);
518 	assert((a = archive_read_new()) != NULL);
519 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
520 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
521 	assertEqualIntA(a, ARCHIVE_OK,
522 	    archive_read_open_filename(a, refname, 10240));
523 
524 	/* Verify regular file1. */
525 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
526 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
527 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
528 	assertEqualInt(86401, archive_entry_mtime(ae));
529 	assertEqualInt(13, archive_entry_size(ae));
530 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
531 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
532 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
533 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
534 
535 	/* Verify regular file2. */
536 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
537 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
538 	assertEqualString("file2", archive_entry_pathname(ae));
539 	assertEqualInt(86401, archive_entry_mtime(ae));
540 	assertEqualInt(26, archive_entry_size(ae));
541 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
542 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
543 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
544 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
545 
546 	/* Verify regular file3. */
547 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
548 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
549 	assertEqualString("file3", archive_entry_pathname(ae));
550 	assertEqualInt(86401, archive_entry_mtime(ae));
551 	assertEqualInt(39, archive_entry_size(ae));
552 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
553 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
554 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
555 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
556 
557 	/* Verify regular file4. */
558 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
559 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
560 	assertEqualString("file4", archive_entry_pathname(ae));
561 	assertEqualInt(86401, archive_entry_mtime(ae));
562 	assertEqualInt(52, archive_entry_size(ae));
563 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
564 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
565 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
566 	assertEqualMem(buff,
567 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
568 
569 	/* Verify regular zfile1. */
570 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
571 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
572 	assertEqualString("dir1/zfile1", archive_entry_pathname(ae));
573 	assertEqualInt(5184001, archive_entry_mtime(ae));
574 	assertEqualInt(13, archive_entry_size(ae));
575 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
576 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
577 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
578 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
579 
580 	/* Verify regular zfile2. */
581 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
582 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
583 	assertEqualString("zfile2", archive_entry_pathname(ae));
584 	assertEqualInt(5184001, archive_entry_mtime(ae));
585 	assertEqualInt(26, archive_entry_size(ae));
586 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
587 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
588 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
589 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
590 
591 	/* Verify regular zfile3. */
592 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
593 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
594 	assertEqualString("zfile3", archive_entry_pathname(ae));
595 	assertEqualInt(5184001, archive_entry_mtime(ae));
596 	assertEqualInt(39, archive_entry_size(ae));
597 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
598 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
599 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
600 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
601 
602 	/* Verify regular zfile4. */
603 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
604 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
605 	assertEqualString("zfile4", archive_entry_pathname(ae));
606 	assertEqualInt(5184001, archive_entry_mtime(ae));
607 	assertEqualInt(52, archive_entry_size(ae));
608 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
609 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
610 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
611 	assertEqualMem(buff,
612 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
613 
614 	/* Verify directory dir1. */
615 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
616 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
617 	assertEqualString("dir1/", archive_entry_pathname(ae));
618 	assertEqualInt(2764801, archive_entry_mtime(ae));
619 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
620 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
621 
622 	assertEqualInt(9, archive_file_count(a));
623 
624 	/* End of archive. */
625 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
626 
627 	/* Verify archive format. */
628 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
629 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
630 
631 	/* Close the archive. */
632 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
633 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
634 }
635 
636 /*
637  * Extract a file compressed with DELTA + LZMA[12].
638  */
639 static void
test_delta_lzma(const char * refname)640 test_delta_lzma(const char *refname)
641 {
642 	struct archive_entry *ae;
643 	struct archive *a;
644 	size_t remaining;
645 	ssize_t bytes;
646 	char buff[1024];
647 
648 	extract_reference_file(refname);
649 	assert((a = archive_read_new()) != NULL);
650 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
651 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
652 	assertEqualIntA(a, ARCHIVE_OK,
653 	    archive_read_open_filename(a, refname, 10240));
654 
655 	/* Verify regular file1. */
656 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
657 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
658 	assertEqualString("file1", archive_entry_pathname(ae));
659 	assertEqualInt(172802, archive_entry_mtime(ae));
660 	assertEqualInt(27627, archive_entry_size(ae));
661 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
662 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
663 	remaining = (size_t)archive_entry_size(ae);
664 	while (remaining) {
665 		if (remaining < sizeof(buff))
666 			assertEqualInt(remaining,
667 			    bytes = archive_read_data(a, buff, sizeof(buff)));
668 		else
669 			assertEqualInt(sizeof(buff),
670 			    bytes = archive_read_data(a, buff, sizeof(buff)));
671 		if (bytes > 0)
672 			remaining -= bytes;
673 		else
674 			break;
675 	}
676 	assertEqualInt(0, remaining);
677 
678 	assertEqualInt(1, archive_file_count(a));
679 
680 	/* End of archive. */
681 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
682 
683 	/* Verify archive format. */
684 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
685 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
686 
687 	/* Close the archive. */
688 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
689 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
690 }
691 
692 /*
693  * Extract a file compressed with BCJ + LZMA2.
694  */
695 static void
test_bcj(const char * refname)696 test_bcj(const char *refname)
697 {
698 	struct archive_entry *ae;
699 	struct archive *a;
700 	size_t remaining;
701 	ssize_t bytes;
702 	char buff[1024];
703 
704 	extract_reference_file(refname);
705 	assert((a = archive_read_new()) != NULL);
706 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
707 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
708 	assertEqualIntA(a, ARCHIVE_OK,
709 	    archive_read_open_filename(a, refname, 10240));
710 
711 	/* Verify regular x86exe. */
712 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
713 	assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae) & ~0111);
714 	assertEqualString("x86exe", archive_entry_pathname(ae));
715 	assertEqualInt(172802, archive_entry_mtime(ae));
716 	assertEqualInt(27328, archive_entry_size(ae));
717 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
718 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
719 	remaining = (size_t)archive_entry_size(ae);
720 	while (remaining) {
721 		if (remaining < sizeof(buff))
722 			assertEqualInt(remaining,
723 			    bytes = archive_read_data(a, buff, sizeof(buff)));
724 		else
725 			assertEqualInt(sizeof(buff),
726 			    bytes = archive_read_data(a, buff, sizeof(buff)));
727 		if (bytes > 0)
728 			remaining -= bytes;
729 		else
730 			break;
731 	}
732 	assertEqualInt(0, remaining);
733 
734 	assertEqualInt(1, archive_file_count(a));
735 
736 	/* End of archive. */
737 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
738 
739 	/* Verify archive format. */
740 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
741 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
742 
743 	/* Close the archive. */
744 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
745 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
746 }
747 
748 /*
749  * Extract a file compressed with PPMd.
750  */
751 static void
test_ppmd(void)752 test_ppmd(void)
753 {
754 	const char *refname = "test_read_format_7zip_ppmd.7z";
755 	struct archive_entry *ae;
756 	struct archive *a;
757 	size_t remaining;
758 	ssize_t bytes;
759 	char buff[1024];
760 
761 	extract_reference_file(refname);
762 	assert((a = archive_read_new()) != NULL);
763 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
764 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
765 	assertEqualIntA(a, ARCHIVE_OK,
766 	    archive_read_open_filename(a, refname, 10240));
767 
768 	/* Verify regular file1. */
769 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
770 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
771 	assertEqualString("ppmd_test.txt", archive_entry_pathname(ae));
772 	assertEqualInt(1322464589, archive_entry_mtime(ae));
773 	assertEqualInt(102400, archive_entry_size(ae));
774 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
775 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
776 	remaining = (size_t)archive_entry_size(ae);
777 	while (remaining) {
778 		if (remaining < sizeof(buff))
779 			assertEqualInt(remaining,
780 			    bytes = archive_read_data(a, buff, sizeof(buff)));
781 		else
782 			assertEqualInt(sizeof(buff),
783 			    bytes = archive_read_data(a, buff, sizeof(buff)));
784 		if (bytes > 0)
785 			remaining -= bytes;
786 		else
787 			break;
788 	}
789 	assertEqualInt(0, remaining);
790 
791 	assertEqualInt(1, archive_file_count(a));
792 
793 	/* End of archive. */
794 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
795 
796 	/* Verify archive format. */
797 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
798 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
799 
800 	/* Close the archive. */
801 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
802 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
803 }
804 
805 static void
test_symname(void)806 test_symname(void)
807 {
808 	const char *refname = "test_read_format_7zip_symbolic_name.7z";
809 	struct archive_entry *ae;
810 	struct archive *a;
811 	char buff[128];
812 
813 	extract_reference_file(refname);
814 	assert((a = archive_read_new()) != NULL);
815 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
816 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
817 	assertEqualIntA(a, ARCHIVE_OK,
818 	    archive_read_open_filename(a, refname, 10240));
819 
820 	/* Verify regular file1. */
821 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
822 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
823 	assertEqualString("file1", archive_entry_pathname(ae));
824 	assertEqualInt(86401, archive_entry_mtime(ae));
825 	assertEqualInt(32, archive_entry_size(ae));
826 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
827 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
828 	assertEqualInt(32, archive_read_data(a, buff, sizeof(buff)));
829 	assertEqualMem(buff, "hellohellohello\nhellohellohello\n", 32);
830 
831 	/* Verify symbolic-link symlinkfile. */
832 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
833 	assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
834 	assertEqualString("symlinkfile", archive_entry_pathname(ae));
835 	assertEqualString("file1", archive_entry_symlink(ae));
836 	assertEqualInt(86401, archive_entry_mtime(ae));
837 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
838 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
839 
840 	assertEqualInt(2, archive_file_count(a));
841 
842 	/* End of archive. */
843 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
844 
845 	/* Verify archive format. */
846 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
847 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
848 
849 	/* Close the archive. */
850 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
851 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
852 }
853 
854 
DEFINE_TEST(test_read_format_7zip)855 DEFINE_TEST(test_read_format_7zip)
856 {
857 	struct archive *a;
858 
859 	assert((a = archive_read_new()) != NULL);
860 
861 	/* Extracting with liblzma */
862 	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
863 		skipping("7zip:lzma decoding is not supported on this "
864 		    "platform");
865 	} else {
866 		test_symname();
867 		test_extract_all_files("test_read_format_7zip_copy_2.7z");
868 		test_extract_last_file("test_read_format_7zip_copy_2.7z");
869 		test_extract_all_files2("test_read_format_7zip_lzma1_lzma2.7z");
870 		test_bcj("test_read_format_7zip_bcj2_copy_lzma.7z");
871 	}
872 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
873 }
874 
DEFINE_TEST(test_read_format_7zip_bzip2)875 DEFINE_TEST(test_read_format_7zip_bzip2)
876 {
877 	struct archive *a;
878 
879 	assert((a = archive_read_new()) != NULL);
880 
881 	/* Extracting with libbzip2 */
882 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
883 		skipping("7zip:bzip2 decoding is not supported on this platform");
884 	} else {
885 		test_plain_header("test_read_format_7zip_bzip2.7z");
886 		test_bcj("test_read_format_7zip_bcj_bzip2.7z");
887 		test_bcj("test_read_format_7zip_bcj2_bzip2.7z");
888 	}
889 
890 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
891 }
892 
DEFINE_TEST(test_read_format_7zip_from_fd)893 DEFINE_TEST(test_read_format_7zip_from_fd)
894 {
895 	test_copy(1);/* read a 7zip file from a file descriptor. */
896 }
897 
DEFINE_TEST(test_read_format_7zip_copy)898 DEFINE_TEST(test_read_format_7zip_copy)
899 {
900 	test_copy(0);
901 	test_bcj("test_read_format_7zip_bcj_copy.7z");
902 	test_bcj("test_read_format_7zip_bcj2_copy_1.7z");
903 	test_bcj("test_read_format_7zip_bcj2_copy_2.7z");
904 }
905 
DEFINE_TEST(test_read_format_7zip_deflate)906 DEFINE_TEST(test_read_format_7zip_deflate)
907 {
908 	struct archive *a;
909 
910 	assert((a = archive_read_new()) != NULL);
911 
912 	/* Extracting with libz */
913 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
914 		skipping(
915 		    "7zip:deflate decoding is not supported on this platform");
916 	} else {
917 		test_plain_header("test_read_format_7zip_deflate.7z");
918 		test_bcj("test_read_format_7zip_bcj_deflate.7z");
919 		test_bcj("test_read_format_7zip_bcj2_deflate.7z");
920 	}
921 
922 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
923 }
924 
DEFINE_TEST(test_read_format_7zip_zstd)925 DEFINE_TEST(test_read_format_7zip_zstd)
926 {
927 	struct archive *a;
928 
929 	assert((a = archive_read_new()) != NULL);
930 
931 	/* Extracting with libzstd */
932 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
933 		skipping(
934 		    "7zip:zstd decoding is not supported on this platform");
935 	} else if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
936 		// The directory header entries in the test file uses lzma.
937 		skipping(
938 		    "7zip:lzma decoding is not supported on this platform");
939 	} else {
940 		test_extract_all_files_zstd("test_read_format_7zip_zstd.7z");
941 	}
942 
943 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
944 }
945 
DEFINE_TEST(test_read_format_7zip_zstd_solid)946 DEFINE_TEST(test_read_format_7zip_zstd_solid)
947 {
948 	struct archive *a;
949 
950 	assert((a = archive_read_new()) != NULL);
951 
952 	/* Extracting with libzstd */
953 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
954 		skipping(
955 		    "7zip:zstd decoding is not supported on this platform");
956 	} else if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
957 		// The directory header entries in the test file uses lzma.
958 		skipping(
959 		    "7zip:lzma decoding is not supported on this platform");
960 	} else {
961 		test_extract_all_files_zstd("test_read_format_7zip_solid_zstd.7z");
962 	}
963 
964 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
965 }
966 
DEFINE_TEST(test_read_format_7zip_zstd_bcj)967 DEFINE_TEST(test_read_format_7zip_zstd_bcj)
968 {
969 	struct archive *a;
970 
971 	assert((a = archive_read_new()) != NULL);
972 
973 	/* Extracting with libzstd */
974 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
975 		skipping(
976 		    "7zip:zstd decoding is not supported on this platform");
977 	} else {
978 		test_extract_file_zstd_bcj_nobjc("test_read_format_7zip_zstd_bcj.7z");
979 	}
980 
981 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
982 }
983 
DEFINE_TEST(test_read_format_7zip_zstd_nobcj)984 DEFINE_TEST(test_read_format_7zip_zstd_nobcj)
985 {
986 	struct archive *a;
987 
988 	assert((a = archive_read_new()) != NULL);
989 
990 	/* Extracting with libzstd */
991 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
992 		skipping(
993 		    "7zip:zstd decoding is not supported on this platform");
994 	} else {
995 		test_extract_file_zstd_bcj_nobjc("test_read_format_7zip_zstd_nobcj.7z");
996 	}
997 
998 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
999 }
1000 
DEFINE_TEST(test_read_format_7zip_empty)1001 DEFINE_TEST(test_read_format_7zip_empty)
1002 {
1003 	test_empty_archive();
1004 	test_empty_file();
1005 }
1006 
DEFINE_TEST(test_read_format_7zip_lzma1)1007 DEFINE_TEST(test_read_format_7zip_lzma1)
1008 {
1009 	struct archive *a;
1010 
1011 	assert((a = archive_read_new()) != NULL);
1012 
1013 	/* Extracting with liblzma */
1014 	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
1015 		skipping("7zip:lzma decoding is not supported on this "
1016 		    "platform");
1017 	} else {
1018 		test_plain_header("test_read_format_7zip_lzma1.7z");
1019 		test_extract_all_files("test_read_format_7zip_lzma1_2.7z");
1020 		test_extract_last_file("test_read_format_7zip_lzma1_2.7z");
1021 		test_bcj("test_read_format_7zip_bcj_lzma1.7z");
1022 		test_bcj("test_read_format_7zip_bcj2_lzma1_1.7z");
1023 		test_bcj("test_read_format_7zip_bcj2_lzma1_2.7z");
1024 		test_delta_lzma("test_read_format_7zip_delta_lzma1.7z");
1025 		test_delta_lzma("test_read_format_7zip_delta4_lzma1.7z");
1026 	}
1027 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1028 }
1029 
DEFINE_TEST(test_read_format_7zip_lzma2)1030 DEFINE_TEST(test_read_format_7zip_lzma2)
1031 {
1032 	struct archive *a;
1033 
1034 	assert((a = archive_read_new()) != NULL);
1035 
1036 	/* Extracting with liblzma */
1037 	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
1038 		skipping("7zip:lzma decoding is not supported on this "
1039 		    "platform");
1040 	} else {
1041 		test_plain_header("test_read_format_7zip_lzma2.7z");
1042 		test_bcj("test_read_format_7zip_bcj_lzma2.7z");
1043 		test_bcj("test_read_format_7zip_bcj2_lzma2_1.7z");
1044 		test_bcj("test_read_format_7zip_bcj2_lzma2_2.7z");
1045 		test_delta_lzma("test_read_format_7zip_delta_lzma2.7z");
1046 		test_delta_lzma("test_read_format_7zip_delta4_lzma2.7z");
1047 	}
1048 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1049 }
1050 
1051 static void
test_arm_filter(const char * refname)1052 test_arm_filter(const char *refname)
1053 {
1054 	struct archive *a;
1055 	struct archive_entry *ae;
1056 	char buff[7804];
1057 	uint32_t computed_crc = 0;
1058 	uint32_t expected_crc = 0x355ec4e1;
1059 
1060 	assert((a = archive_read_new()) != NULL);
1061 
1062 	extract_reference_file(refname);
1063 
1064 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1065 	assert((a = archive_read_new()) != NULL);
1066 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1067 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1068 
1069 	assertEqualIntA(a, ARCHIVE_OK,
1070 		archive_read_open_filename(a, refname, 10240));
1071 
1072 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1073 	assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
1074 	assertEqualString("hw-gnueabihf", archive_entry_pathname(ae));
1075 	assertEqualInt(sizeof(buff), archive_entry_size(ae));
1076 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1077 	computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
1078 	assertEqualInt(computed_crc, expected_crc);
1079 
1080 	assertEqualInt(1, archive_file_count(a));
1081 
1082 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1083 
1084 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1085 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1086 }
1087 
DEFINE_TEST(test_read_format_7zip_zstd_arm)1088 DEFINE_TEST(test_read_format_7zip_zstd_arm)
1089 {
1090 	struct archive *a;
1091 
1092 	assert((a = archive_read_new()) != NULL);
1093 
1094 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1095 		skipping(
1096 		    "7zip:zstd decoding is not supported on this platform");
1097 	} else {
1098 		test_arm_filter("test_read_format_7zip_zstd_arm.7z");
1099 	}
1100 
1101 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1102 }
1103 
DEFINE_TEST(test_read_format_7zip_lzma2_arm)1104 DEFINE_TEST(test_read_format_7zip_lzma2_arm)
1105 {
1106 	struct archive *a;
1107 
1108 	assert((a = archive_read_new()) != NULL);
1109 
1110 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1111 		skipping(
1112 		    "7zip:lzma decoding is not supported on this platform");
1113 	} else {
1114 		test_arm_filter("test_read_format_7zip_lzma2_arm.7z");
1115 	}
1116 
1117 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1118 }
1119 
DEFINE_TEST(test_read_format_7zip_ppmd)1120 DEFINE_TEST(test_read_format_7zip_ppmd)
1121 {
1122 	test_ppmd();
1123 }
1124 
1125 static void
test_arm64_filter(const char * refname)1126 test_arm64_filter(const char *refname)
1127 {
1128 	struct archive *a;
1129 	struct archive_entry *ae;
1130 	char buff[70368];
1131 	uint32_t computed_crc = 0;
1132 	uint32_t expected_crc = 0xde97d594;
1133 
1134 	assert((a = archive_read_new()) != NULL);
1135 
1136 	extract_reference_file(refname);
1137 
1138 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1139 	assert((a = archive_read_new()) != NULL);
1140 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1141 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1142 
1143 	assertEqualIntA(a, ARCHIVE_OK,
1144 		archive_read_open_filename(a, refname, 10240));
1145 
1146 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1147 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1148 	assertEqualString("hw-arm64", archive_entry_pathname(ae));
1149 	assertEqualInt(sizeof(buff), archive_entry_size(ae));
1150 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1151 	computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
1152 	assertEqualInt(computed_crc, expected_crc);
1153 
1154 	assertEqualInt(1, archive_file_count(a));
1155 
1156 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1157 
1158 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1159 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1160 }
1161 
DEFINE_TEST(test_read_format_7zip_lzma2_arm64)1162 DEFINE_TEST(test_read_format_7zip_lzma2_arm64)
1163 {
1164 #ifdef LZMA_FILTER_ARM64
1165 	struct archive *a;
1166 
1167 	assert((a = archive_read_new()) != NULL);
1168 
1169 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1170 		skipping(
1171 		    "7zip:lzma decoding is not supported on this platform");
1172 	} else {
1173 		test_arm64_filter("test_read_format_7zip_lzma2_arm64.7z");
1174 	}
1175 
1176 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1177 #else
1178 	skipping("This version of liblzma does not support LZMA_FILTER_ARM64");
1179 #endif
1180 }
1181 
DEFINE_TEST(test_read_format_7zip_deflate_arm64)1182 DEFINE_TEST(test_read_format_7zip_deflate_arm64)
1183 {
1184 	struct archive *a;
1185 
1186 	assert((a = archive_read_new()) != NULL);
1187 
1188 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
1189 		skipping(
1190 		    "7zip:deflate decoding is not supported on this platform");
1191 	} else {
1192 		test_arm64_filter("test_read_format_7zip_deflate_arm64.7z");
1193 	}
1194 
1195 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1196 }
1197 
DEFINE_TEST(test_read_format_7zip_win_attrib)1198 DEFINE_TEST(test_read_format_7zip_win_attrib)
1199 {
1200 	struct archive *a;
1201 
1202 	assert((a = archive_read_new()) != NULL);
1203 
1204 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1205 		skipping(
1206 		    "7zip:lzma decoding is not supported on this platform");
1207 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1208 		return;
1209 	}
1210 
1211 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1212 
1213 	// This archive has four files and four directories:
1214 	// * hidden directory
1215 	// * readonly directory
1216 	// * regular directory
1217 	// * system directory
1218 	// * regular "archive" file
1219 	// * hidden file
1220 	// * readonly file
1221 	// * system file
1222 	const char *refname = "test_read_format_7zip_win_attrib.7z";
1223 	extract_reference_file(refname);
1224 
1225 	assertEqualIntA(a, ARCHIVE_OK,
1226 		archive_read_open_filename(a, refname, 10240));
1227 
1228 	struct archive_entry *ae;
1229 
1230 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1231 	assertEqualString("hidden_dir/", archive_entry_pathname(ae));
1232 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1233 	assertEqualString("hidden", archive_entry_fflags_text(ae));
1234 
1235 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1236 	assertEqualString("readonly_dir/", archive_entry_pathname(ae));
1237 	assertEqualInt((AE_IFDIR | 0555), archive_entry_mode(ae));
1238 	assertEqualString("rdonly", archive_entry_fflags_text(ae));
1239 
1240 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1241 	assertEqualString("regular_dir/", archive_entry_pathname(ae));
1242 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1243 	assertEqualString(NULL, archive_entry_fflags_text(ae));
1244 
1245 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1246 	assertEqualString("system_dir/", archive_entry_pathname(ae));
1247 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1248 	assertEqualString("system", archive_entry_fflags_text(ae));
1249 
1250 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1251 	assertEqualString("archive_file.txt", archive_entry_pathname(ae));
1252 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1253 	assertEqualString(NULL, archive_entry_fflags_text(ae));
1254 
1255 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1256 	assertEqualString("hidden_file.txt", archive_entry_pathname(ae));
1257 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1258 	assertEqualString("hidden", archive_entry_fflags_text(ae));
1259 
1260 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1261 	assertEqualString("readonly_file.txt", archive_entry_pathname(ae));
1262 	assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae));
1263 	assertEqualString("rdonly", archive_entry_fflags_text(ae));
1264 
1265 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1266 	assertEqualString("system_file.txt", archive_entry_pathname(ae));
1267 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1268 	assertEqualString("system", archive_entry_fflags_text(ae));
1269 
1270 
1271 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1272 }
1273 
DEFINE_TEST(test_read_format_7zip_sfx_pe)1274 DEFINE_TEST(test_read_format_7zip_sfx_pe)
1275 {
1276 	/*
1277 	 * This is a regular 7z SFX PE file
1278 	 * created by 7z tool v22.01 on Windows 64-bit
1279 	 */
1280 	struct archive *a;
1281 	struct archive_entry *ae;
1282 	int bs = 10240;
1283 	char buff[32];
1284 	const char reffile[] = "test_read_format_7zip_sfx_pe.exe";
1285 	const char test_txt[] = "123";
1286 	int size = sizeof(test_txt) - 1;
1287 
1288 	extract_reference_file(reffile);
1289 	assert((a = archive_read_new()) != NULL);
1290 	assertA(0 == archive_read_support_filter_all(a));
1291 	assertA(0 == archive_read_support_format_all(a));
1292 	assertA(0 == archive_read_open_filename(a, reffile, bs));
1293 
1294 	assertA(0 == archive_read_next_header(a, &ae));
1295 	assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1296 
1297 	assertA(size == archive_read_data(a, buff, size));
1298 	assertEqualMem(buff, test_txt, size);
1299 
1300 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1301 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1302 }
1303 
DEFINE_TEST(test_read_format_7zip_sfx_modified_pe)1304 DEFINE_TEST(test_read_format_7zip_sfx_modified_pe)
1305 {
1306 	/*
1307 	 * This test simulates a modified 7z SFX PE
1308 	 * the compressed data in the SFX file is still stored as PE overlay
1309 	 * but the decompressor code is replaced
1310 	 */
1311 	struct archive *a;
1312 	struct archive_entry *ae;
1313 	int bs = 10240;
1314 	char buff[32];
1315 	const char reffile[] = "test_read_format_7zip_sfx_modified_pe.exe";
1316 	const char test_txt[] = "123";
1317 	int size = sizeof(test_txt) - 1;
1318 
1319 	extract_reference_file(reffile);
1320 	assert((a = archive_read_new()) != NULL);
1321 	assertA(0 == archive_read_support_filter_all(a));
1322 	assertA(0 == archive_read_support_format_all(a));
1323 	assertA(0 == archive_read_open_filename(a, reffile, bs));
1324 
1325 	assertA(0 == archive_read_next_header(a, &ae));
1326 	assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1327 
1328 	assertA(size == archive_read_data(a, buff, size));
1329 	assertEqualMem(buff, test_txt, size);
1330 
1331 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1332 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1333 }
1334 
DEFINE_TEST(test_read_format_7zip_sfx_elf)1335 DEFINE_TEST(test_read_format_7zip_sfx_elf)
1336 {
1337 	/*
1338 	 * This is a regular 7z SFX ELF file
1339 	 * created by 7z tool v16.02 on Ubuntu
1340 	 */
1341 	struct archive *a;
1342 	struct archive_entry *ae;
1343 	int bs = 10240;
1344 	char buff[32];
1345 	const char reffile[] = "test_read_format_7zip_sfx_elf.elf";
1346 	const char test_txt[] = "123";
1347 	int size = sizeof(test_txt) - 1;
1348 
1349 	extract_reference_file(reffile);
1350 	assert((a = archive_read_new()) != NULL);
1351 	assertA(0 == archive_read_support_filter_all(a));
1352 	assertA(0 == archive_read_support_format_all(a));
1353 	assertA(0 == archive_read_open_filename(a, reffile, bs));
1354 
1355 	assertA(0 == archive_read_next_header(a, &ae));
1356 	assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1357 
1358 	assertA(size == archive_read_data(a, buff, size));
1359 	assertEqualMem(buff, test_txt, size);
1360 
1361 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1362   assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1363 }
1364 
DEFINE_TEST(test_read_format_7zip_extract_second)1365 DEFINE_TEST(test_read_format_7zip_extract_second)
1366 {
1367 	struct archive *a;
1368 	char buffer[256];
1369 
1370 	assert((a = archive_read_new()) != NULL);
1371 
1372 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1373 		skipping(
1374 		    "7zip:lzma decoding is not supported on this platform");
1375 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1376 		return;
1377 	}
1378 
1379 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1380 
1381 	/*
1382 	 * The test archive has two files: first.txt which is a 65,536 file (the
1383 	 * size of the uncompressed buffer), and second.txt which has contents
1384 	 * we will validate. This test ensures we can skip first.txt and still
1385 	 * be able to read the contents of second.txt
1386 	 */
1387 	const char *refname = "test_read_format_7zip_extract_second.7z";
1388 	extract_reference_file(refname);
1389 
1390 	assertEqualIntA(a, ARCHIVE_OK,
1391 		archive_read_open_filename(a, refname, 10240));
1392 
1393 	struct archive_entry *ae;
1394 
1395 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1396 	assertEqualString("first.txt", archive_entry_pathname(ae));
1397 
1398 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1399 	assertEqualString("second.txt", archive_entry_pathname(ae));
1400 
1401 	assertEqualInt(23, archive_read_data(a, buffer, sizeof(buffer)));
1402 	assertEqualMem("This is from second.txt", buffer, 23);
1403 
1404 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1405 }
1406 
1407 #ifdef LZMA_FILTER_RISCV
1408 static void
test_riscv_filter(const char * refname)1409 test_riscv_filter(const char *refname)
1410 {
1411 	struct archive *a;
1412 	struct archive_entry *ae;
1413 	char buff[8488];
1414 	uint32_t computed_crc = 0;
1415 	uint32_t expected_crc = 0xf7ed24e7;
1416 
1417 	assert((a = archive_read_new()) != NULL);
1418 
1419 	extract_reference_file(refname);
1420 
1421 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1422 	assert((a = archive_read_new()) != NULL);
1423 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1424 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1425 
1426 	assertEqualIntA(a, ARCHIVE_OK,
1427 		archive_read_open_filename(a, refname, 10240));
1428 
1429 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1430 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1431 	assertEqualString("hw-riscv64", archive_entry_pathname(ae));
1432 	assertEqualInt(sizeof(buff), archive_entry_size(ae));
1433 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1434 
1435 	computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
1436 	assertEqualInt(computed_crc, expected_crc);
1437 
1438 	assertEqualInt(1, archive_file_count(a));
1439 
1440 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1441 
1442 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1443 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1444 }
1445 #endif
1446 
DEFINE_TEST(test_read_format_7zip_lzma2_riscv)1447 DEFINE_TEST(test_read_format_7zip_lzma2_riscv)
1448 {
1449 #ifdef LZMA_FILTER_RISCV
1450 	struct archive *a;
1451 
1452 	assert((a = archive_read_new()) != NULL);
1453 
1454 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1455 		skipping("7zip:lzma decoding is not supported on this platform");
1456 	} else {
1457 		test_riscv_filter("test_read_format_7zip_lzma2_riscv.7z");
1458 	}
1459 #else
1460 	skipping("This version of liblzma does not support LZMA_FILTER_RISCV");
1461 #endif
1462 }
1463 
1464 static void
test_sparc_filter(const char * refname)1465 test_sparc_filter(const char *refname)
1466 {
1467 	struct archive *a;
1468 	struct archive_entry *ae;
1469 	size_t expected_entry_size = 1053016;
1470 	char *buff = malloc(expected_entry_size);
1471 	uint32_t computed_crc = 0;
1472 	uint32_t expected_crc = 0x6b5b364d;
1473 
1474 	assert((a = archive_read_new()) != NULL);
1475 
1476 	extract_reference_file(refname);
1477 
1478 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1479 	assert((a = archive_read_new()) != NULL);
1480 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1481 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1482 
1483 	assertEqualIntA(a, ARCHIVE_OK,
1484 		archive_read_open_filename(a, refname, 10240));
1485 
1486 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1487 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1488 	assertEqualString("hw-sparc64", archive_entry_pathname(ae));
1489 	assertEqualInt(expected_entry_size, archive_entry_size(ae));
1490 	assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size));
1491 
1492 	computed_crc = bitcrc32(computed_crc, buff, expected_entry_size);
1493 	assertEqualInt(computed_crc, expected_crc);
1494 
1495 	assertEqualInt(1, archive_file_count(a));
1496 
1497 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1498 
1499 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1500 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1501 
1502 	free(buff);
1503 }
1504 
DEFINE_TEST(test_read_format_7zip_lzma2_sparc)1505 DEFINE_TEST(test_read_format_7zip_lzma2_sparc)
1506 {
1507 	struct archive *a;
1508 
1509 	assert((a = archive_read_new()) != NULL);
1510 
1511 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1512 		skipping(
1513 		    "7zip:lzma decoding is not supported on this platform");
1514 	} else {
1515 		test_sparc_filter("test_read_format_7zip_lzma2_sparc.7z");
1516 	}
1517 
1518 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1519 }
1520 
DEFINE_TEST(test_read_format_7zip_zstd_sparc)1521 DEFINE_TEST(test_read_format_7zip_zstd_sparc)
1522 {
1523 	struct archive *a;
1524 
1525 	assert((a = archive_read_new()) != NULL);
1526 
1527 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1528 		skipping(
1529 		    "7zip:zstd decoding is not supported on this platform");
1530 	} else {
1531 		test_sparc_filter("test_read_format_7zip_zstd_sparc.7z");
1532 	}
1533 
1534 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1535 }
1536 
1537 static void
test_powerpc_filter(const char * refname)1538 test_powerpc_filter(const char *refname)
1539 {
1540 	struct archive *a;
1541 	struct archive_entry *ae;
1542 	size_t expected_entry_size = 68340;
1543 	char *buff = malloc(expected_entry_size);
1544 	uint32_t computed_crc = 0;
1545 	uint32_t expected_crc = 0x71fb03c9;
1546 
1547 	assert((a = archive_read_new()) != NULL);
1548 
1549 	extract_reference_file(refname);
1550 
1551 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1552 	assert((a = archive_read_new()) != NULL);
1553 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1554 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1555 
1556 	assertEqualIntA(a, ARCHIVE_OK,
1557 		archive_read_open_filename(a, refname, 10240));
1558 
1559 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1560 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1561 	assertEqualString("hw-powerpc", archive_entry_pathname(ae));
1562 	assertEqualInt(expected_entry_size, archive_entry_size(ae));
1563 	assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size));
1564 
1565 	computed_crc = bitcrc32(computed_crc, buff, expected_entry_size);
1566 	assertEqualInt(computed_crc, expected_crc);
1567 
1568 	assertEqualInt(1, archive_file_count(a));
1569 
1570 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1571 
1572 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1573 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1574 
1575 	free(buff);
1576 }
1577 
DEFINE_TEST(test_read_format_7zip_deflate_powerpc)1578 DEFINE_TEST(test_read_format_7zip_deflate_powerpc)
1579 {
1580 	struct archive *a;
1581 
1582 	assert((a = archive_read_new()) != NULL);
1583 
1584 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
1585 		skipping(
1586 		    "7zip:deflate decoding is not supported on this platform");
1587 	} else {
1588 		test_powerpc_filter("test_read_format_7zip_deflate_powerpc.7z");
1589 	}
1590 
1591 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1592 }
1593 
DEFINE_TEST(test_read_format_7zip_lzma2_powerpc)1594 DEFINE_TEST(test_read_format_7zip_lzma2_powerpc)
1595 {
1596 	struct archive *a;
1597 
1598 	assert((a = archive_read_new()) != NULL);
1599 
1600 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
1601 		skipping(
1602 		    "7zip:deflate decoding is not supported on this platform");
1603 	} else {
1604 		test_powerpc_filter("test_read_format_7zip_lzma2_powerpc.7z");
1605 	}
1606 
1607 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1608 }
1609