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