1129202Scognet/*
2129202Scognet * Test application for xz_boot.c
3129202Scognet *
4129202Scognet * Author: Lasse Collin <lasse.collin@tukaani.org>
5129202Scognet *
6129202Scognet * This file has been put into the public domain.
7129202Scognet * You can do whatever you want with this file.
8129202Scognet */
9129202Scognet
10129202Scognet#include <stdlib.h>
11129202Scognet#include <string.h>
12129202Scognet#include <stdio.h>
13129202Scognet
14129202Scognet#define STATIC static
15129202Scognet#define INIT
16129202Scognet
17129202Scognetstatic void error(/*const*/ char *msg)
18129202Scognet{
19129202Scognet	fprintf(stderr, "%s\n", msg);
20129202Scognet}
21129202Scognet
22129202Scognet/* Disable the CRC64 support even if it was enabled in the Makefile. */
23129202Scognet#undef XZ_USE_CRC64
24129202Scognet
25129202Scognet#include "../linux/lib/decompress_unxz.c"
26129202Scognet
27129202Scognetstatic uint8_t in[1024 * 1024];
28129202Scognetstatic uint8_t out[1024 * 1024];
29129202Scognet
30129202Scognetstatic int fill(void *buf, unsigned int size)
31129202Scognet{
32129202Scognet	return fread(buf, 1, size, stdin);
33129202Scognet}
34129202Scognet
35129202Scognetstatic int flush(/*const*/ void *buf, unsigned int size)
36129202Scognet{
37129202Scognet	return fwrite(buf, 1, size, stdout);
38129202Scognet}
39129202Scognet
40129202Scognetstatic void test_buf_to_buf(void)
41129202Scognet{
42129202Scognet	size_t in_size;
43129202Scognet	int ret;
44129202Scognet	in_size = fread(in, 1, sizeof(in), stdin);
45129202Scognet	ret = decompress(in, in_size, NULL, NULL, out, NULL, &error);
46129202Scognet	/* fwrite(out, 1, FIXME, stdout); */
47129202Scognet	fprintf(stderr, "ret = %d\n", ret);
48129202Scognet}
49129202Scognet
50static 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
60static 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 */
71static 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
80int 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