xref: /aosp_15_r20/external/xz-embedded/userspace/boottest.c (revision d2c16535d139cb185e89120452531bba6b36d3c6)
1 // SPDX-License-Identifier: 0BSD
2 
3 /*
4  * Test application for xz_boot.c
5  *
6  * Author: Lasse Collin <[email protected]>
7  */
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 
13 #define STATIC static
14 #define INIT
15 
error(char * msg)16 static void error(/*const*/ char *msg)
17 {
18 	fprintf(stderr, "%s\n", msg);
19 }
20 
21 /*
22  * Disable XZ_UNSUPPORTED_CHECK as it's not used in Linux and thus
23  * decompress_unxz.c doesn't handle it either (it thinks it's a bug).
24  */
25 #undef XZ_DEC_ANY_CHECK
26 
27 /* Disable the CRC64 support even if it was enabled in the Makefile. */
28 #undef XZ_USE_CRC64
29 
30 #include "../linux/lib/decompress_unxz.c"
31 
32 static uint8_t in[1024 * 1024];
33 static uint8_t out[1024 * 1024];
34 
fill(void * buf,unsigned long size)35 static long fill(void *buf, unsigned long size)
36 {
37 	return fread(buf, 1, size, stdin);
38 }
39 
flush(void * buf,unsigned long size)40 static long flush(/*const*/ void *buf, unsigned long size)
41 {
42 	return fwrite(buf, 1, size, stdout);
43 }
44 
test_buf_to_buf(void)45 static void test_buf_to_buf(void)
46 {
47 	size_t in_size;
48 	int ret;
49 	in_size = fread(in, 1, sizeof(in), stdin);
50 	ret = __decompress(in, in_size, NULL, NULL, out, sizeof(out),
51 			NULL, &error);
52 	/* fwrite(out, 1, FIXME, stdout); */
53 	fprintf(stderr, "ret = %d\n", ret);
54 }
55 
test_buf_to_cb(void)56 static void test_buf_to_cb(void)
57 {
58 	size_t in_size;
59 	long in_used;
60 	int ret;
61 	in_size = fread(in, 1, sizeof(in), stdin);
62 	ret = __decompress(in, in_size, NULL, &flush, NULL, sizeof(out),
63 			&in_used, &error);
64 	fprintf(stderr, "ret = %d; in_used = %ld\n", ret, in_used);
65 }
66 
test_cb_to_cb(void)67 static void test_cb_to_cb(void)
68 {
69 	int ret;
70 	ret = __decompress(NULL, 0, &fill, &flush, NULL, sizeof(out),
71 			NULL, &error);
72 	fprintf(stderr, "ret = %d\n", ret);
73 }
74 
75 /*
76  * Not used by Linux <= 2.6.37-rc4 and newer probably won't use it either,
77  * but this kind of use case is still required to be supported by the API.
78  */
test_cb_to_buf(void)79 static void test_cb_to_buf(void)
80 {
81 	long in_used;
82 	int ret;
83 	ret = __decompress(in, 0, &fill, NULL, out, sizeof(out),
84 			&in_used, &error);
85 	/* fwrite(out, 1, FIXME, stdout); */
86 	fprintf(stderr, "ret = %d; in_used = %ld\n", ret, in_used);
87 }
88 
main(int argc,char ** argv)89 int main(int argc, char **argv)
90 {
91 	if (argc != 2)
92 		fprintf(stderr, "Usage: %s [bb|bc|cc|cb]\n", argv[0]);
93 	else if (strcmp(argv[1], "bb") == 0)
94 		test_buf_to_buf();
95 	else if (strcmp(argv[1], "bc") == 0)
96 		test_buf_to_cb();
97 	else if (strcmp(argv[1], "cc") == 0)
98 		test_cb_to_cb();
99 	else if (strcmp(argv[1], "cb") == 0)
100 		test_cb_to_buf();
101 	else
102 		fprintf(stderr, "Usage: %s [bb|bc|cc|cb]\n", argv[0]);
103 
104 	return 0;
105 }
106