163dab8eeSAdrian Chadd /*
263dab8eeSAdrian Chadd * Test application for xz_boot.c
363dab8eeSAdrian Chadd *
463dab8eeSAdrian Chadd * Author: Lasse Collin <lasse.collin@tukaani.org>
563dab8eeSAdrian Chadd *
663dab8eeSAdrian Chadd * This file has been put into the public domain.
763dab8eeSAdrian Chadd * You can do whatever you want with this file.
863dab8eeSAdrian Chadd */
963dab8eeSAdrian Chadd
1063dab8eeSAdrian Chadd #include <stdlib.h>
1163dab8eeSAdrian Chadd #include <string.h>
1263dab8eeSAdrian Chadd #include <stdio.h>
1363dab8eeSAdrian Chadd
1463dab8eeSAdrian Chadd #define STATIC static
1563dab8eeSAdrian Chadd #define INIT
1663dab8eeSAdrian Chadd
error(char * msg)1763dab8eeSAdrian Chadd static void error(/*const*/ char *msg)
1863dab8eeSAdrian Chadd {
1963dab8eeSAdrian Chadd fprintf(stderr, "%s\n", msg);
2063dab8eeSAdrian Chadd }
2163dab8eeSAdrian Chadd
22*f0bd5302SXin LI /* Disable the CRC64 support even if it was enabled in the Makefile. */
23*f0bd5302SXin LI #undef XZ_USE_CRC64
24*f0bd5302SXin LI
2563dab8eeSAdrian Chadd #include "../linux/lib/decompress_unxz.c"
2663dab8eeSAdrian Chadd
2763dab8eeSAdrian Chadd static uint8_t in[1024 * 1024];
2863dab8eeSAdrian Chadd static uint8_t out[1024 * 1024];
2963dab8eeSAdrian Chadd
fill(void * buf,unsigned int size)3063dab8eeSAdrian Chadd static int fill(void *buf, unsigned int size)
3163dab8eeSAdrian Chadd {
3263dab8eeSAdrian Chadd return fread(buf, 1, size, stdin);
3363dab8eeSAdrian Chadd }
3463dab8eeSAdrian Chadd
flush(void * buf,unsigned int size)3563dab8eeSAdrian Chadd static int flush(/*const*/ void *buf, unsigned int size)
3663dab8eeSAdrian Chadd {
3763dab8eeSAdrian Chadd return fwrite(buf, 1, size, stdout);
3863dab8eeSAdrian Chadd }
3963dab8eeSAdrian Chadd
test_buf_to_buf(void)4063dab8eeSAdrian Chadd static void test_buf_to_buf(void)
4163dab8eeSAdrian Chadd {
4263dab8eeSAdrian Chadd size_t in_size;
4363dab8eeSAdrian Chadd int ret;
4463dab8eeSAdrian Chadd in_size = fread(in, 1, sizeof(in), stdin);
4563dab8eeSAdrian Chadd ret = decompress(in, in_size, NULL, NULL, out, NULL, &error);
4663dab8eeSAdrian Chadd /* fwrite(out, 1, FIXME, stdout); */
4763dab8eeSAdrian Chadd fprintf(stderr, "ret = %d\n", ret);
4863dab8eeSAdrian Chadd }
4963dab8eeSAdrian Chadd
test_buf_to_cb(void)5063dab8eeSAdrian Chadd static void test_buf_to_cb(void)
5163dab8eeSAdrian Chadd {
5263dab8eeSAdrian Chadd size_t in_size;
5363dab8eeSAdrian Chadd int in_used;
5463dab8eeSAdrian Chadd int ret;
5563dab8eeSAdrian Chadd in_size = fread(in, 1, sizeof(in), stdin);
5663dab8eeSAdrian Chadd ret = decompress(in, in_size, NULL, &flush, NULL, &in_used, &error);
5763dab8eeSAdrian Chadd fprintf(stderr, "ret = %d; in_used = %d\n", ret, in_used);
5863dab8eeSAdrian Chadd }
5963dab8eeSAdrian Chadd
test_cb_to_cb(void)6063dab8eeSAdrian Chadd static void test_cb_to_cb(void)
6163dab8eeSAdrian Chadd {
6263dab8eeSAdrian Chadd int ret;
6363dab8eeSAdrian Chadd ret = decompress(NULL, 0, &fill, &flush, NULL, NULL, &error);
6463dab8eeSAdrian Chadd fprintf(stderr, "ret = %d\n", ret);
6563dab8eeSAdrian Chadd }
6663dab8eeSAdrian Chadd
6763dab8eeSAdrian Chadd /*
6863dab8eeSAdrian Chadd * Not used by Linux <= 2.6.37-rc4 and newer probably won't use it either,
6963dab8eeSAdrian Chadd * but this kind of use case is still required to be supported by the API.
7063dab8eeSAdrian Chadd */
test_cb_to_buf(void)7163dab8eeSAdrian Chadd static void test_cb_to_buf(void)
7263dab8eeSAdrian Chadd {
7363dab8eeSAdrian Chadd int in_used;
7463dab8eeSAdrian Chadd int ret;
7563dab8eeSAdrian Chadd ret = decompress(in, 0, &fill, NULL, out, &in_used, &error);
7663dab8eeSAdrian Chadd /* fwrite(out, 1, FIXME, stdout); */
7763dab8eeSAdrian Chadd fprintf(stderr, "ret = %d; in_used = %d\n", ret, in_used);
7863dab8eeSAdrian Chadd }
7963dab8eeSAdrian Chadd
main(int argc,char ** argv)8063dab8eeSAdrian Chadd int main(int argc, char **argv)
8163dab8eeSAdrian Chadd {
8263dab8eeSAdrian Chadd if (argc != 2)
8363dab8eeSAdrian Chadd fprintf(stderr, "Usage: %s [bb|bc|cc|cb]\n", argv[0]);
8463dab8eeSAdrian Chadd else if (strcmp(argv[1], "bb") == 0)
8563dab8eeSAdrian Chadd test_buf_to_buf();
8663dab8eeSAdrian Chadd else if (strcmp(argv[1], "bc") == 0)
8763dab8eeSAdrian Chadd test_buf_to_cb();
8863dab8eeSAdrian Chadd else if (strcmp(argv[1], "cc") == 0)
8963dab8eeSAdrian Chadd test_cb_to_cb();
9063dab8eeSAdrian Chadd else if (strcmp(argv[1], "cb") == 0)
9163dab8eeSAdrian Chadd test_cb_to_buf();
9263dab8eeSAdrian Chadd else
9363dab8eeSAdrian Chadd fprintf(stderr, "Usage: %s [bb|bc|cc|cb]\n", argv[0]);
9463dab8eeSAdrian Chadd
9563dab8eeSAdrian Chadd return 0;
9663dab8eeSAdrian Chadd }
97