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