diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2009-03-22 13:12:47 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2009-03-22 13:12:47 +0200 |
commit | bcc4b36559e38e30afe6030c42ea4f369df94989 (patch) | |
tree | 8a7297a96e5baa52df62b0ab7bdc2b5399879c45 /userspace | |
download | xz-embedded-bcc4b36559e38e30afe6030c42ea4f369df94989.tar.gz |
Initial commit
Diffstat (limited to 'userspace')
-rw-r--r-- | userspace/Makefile | 38 | ||||
-rw-r--r-- | userspace/boottest.c | 37 | ||||
-rw-r--r-- | userspace/buftest.c | 48 | ||||
-rw-r--r-- | userspace/xzminidec.c | 113 |
4 files changed, 236 insertions, 0 deletions
diff --git a/userspace/Makefile b/userspace/Makefile new file mode 100644 index 0000000..61ed75c --- /dev/null +++ b/userspace/Makefile @@ -0,0 +1,38 @@ +# +# Makefile +# +# Author: Lasse Collin <lasse.collin@tukaani.org> +# +# This file has been put into the public domain. +# You can do whatever you want with this file. +# + +CC = gcc -std=gnu89 +CFLAGS = -ggdb3 -O2 -pedantic -Wall -Wextra +RM = rm -f +VPATH = ../linux/include/linux ../linux/lib/xz +COMMON_OBJS = xz_crc32.o xz_dec_stream.o xz_dec_lzma2.o +XZMINIDEC_OBJS = xzminidec.o +BUFTEST_OBJS = buftest.o +BOOTTEST_OBJS = boottest.o +XZ_HEADERS = xz.h xz_private.h xz_stream.h xz_lzma2.h +PROGRAMS = xzminidec buftest boottest + +all: $(PROGRAMS) + +%.o: %.c $(XZ_HEADERS) + $(CC) -I../linux/include/linux $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + +xzminidec: $(COMMON_OBJS) $(XZMINIDEC_OBJS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) $(XZMINIDEC_OBJS) + +buftest: $(COMMON_OBJS) $(BUFTEST_OBJS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(COMMON_OBJS) $(BUFTEST_OBJS) + +boottest: $(BOOTTEST_OBJS) xz_crc32.c xz_dec_stream.c xz_dec_lzma2.c + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTTEST_OBJS) + +.PHONY: clean +clean: + -$(RM) $(COMMON_OBJS) $(XZMINIDEC_OBJS) $(BUFTEST_OBJS) \ + $(BOOTTEST_OBJS) $(PROGRAMS) diff --git a/userspace/boottest.c b/userspace/boottest.c new file mode 100644 index 0000000..971194e --- /dev/null +++ b/userspace/boottest.c @@ -0,0 +1,37 @@ +/* + * Test application for xz_boot.c + * + * Author: Lasse Collin <lasse.collin@tukaani.org> + * + * This file has been put into the public domain. + * You can do whatever you want with this file. + */ + +#include <stdio.h> + +static void error(const char *msg) +{ + fprintf(stderr, "%s\n", msg); +} + +#define XZ_MEM_FUNCS +#include "../linux/lib/xz/xz_boot.c" + +static uint8_t in[1024 * 1024]; +static uint8_t out[1024 * 1024]; +static uint8_t heap[40 * 1024]; + +int main(void) +{ + int ret; + struct xz_buf b = { in, 0, 0, out, 0, sizeof(out) }; + b.in_size = fread(in, 1, sizeof(in), stdin); + + malloc_buf = heap; + malloc_avail = sizeof(heap); + ret = xz_dec_buf(&b); + + fwrite(out, 1, b.out_pos, stdout); + fprintf(stderr, "%d\n", ret); + return 0; +} diff --git a/userspace/buftest.c b/userspace/buftest.c new file mode 100644 index 0000000..5fbf5dd --- /dev/null +++ b/userspace/buftest.c @@ -0,0 +1,48 @@ +/* + * Test application to test buffer-to-buffer decoding + * + * Author: Lasse Collin <lasse.collin@tukaani.org> + * + * This file has been put into the public domain. + * You can do whatever you want with this file. + */ + +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include "xz.h" + +#define BUFFER_SIZE (1024 * 1024) + +static uint8_t in[BUFFER_SIZE]; +static uint8_t out[BUFFER_SIZE]; + +int main(void) +{ + struct xz_buf b; + struct xz_dec *s; + enum xz_ret ret; + + xz_crc32_init(); + + s = xz_dec_init(0); + if (s == NULL) { + fputs("Initialization failed", stderr); + return 1; + } + + b.in = in; + b.in_pos = 0; + b.in_size = fread(in, 1, sizeof(in), stdin); + b.out = out; + b.out_pos = 0; + b.out_size = sizeof(out); + + ret = xz_dec_run(s, &b); + xz_dec_end(s); + + fwrite(out, 1, b.out_pos, stdout); + fprintf(stderr, "%d\n", ret); + + return 0; +} diff --git a/userspace/xzminidec.c b/userspace/xzminidec.c new file mode 100644 index 0000000..93b3095 --- /dev/null +++ b/userspace/xzminidec.c @@ -0,0 +1,113 @@ +/* + * Simple XZ decoder command line tool + * + * Author: Lasse Collin <lasse.collin@tukaani.org> + * + * This file has been put into the public domain. + * You can do whatever you want with this file. + */ + +/* + * This is really limited: The dictionary has to be preallocated, only CRC32 + * is supported as the integrity check, and decoding of concatenated streams + * is not supported. Thus, you may want to look at xzdec from XZ Utils if + * a few KiB bigger tool is not a problem. + */ + +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include "xz.h" + +static uint8_t in[BUFSIZ]; +static uint8_t out[BUFSIZ]; + +int main(int argc, char **argv) +{ + struct xz_buf b; + struct xz_dec *s; + enum xz_ret ret; + const char *msg; + + if (argc >= 2 && strcmp(argv[1], "--help") == 0) { + fputs("Uncompress a .xz file from stdin to stdout.\n" + "Arguments other than `--help' are ignored.\n", + stdout); + return 0; + } + + xz_crc32_init(); + + /* Support up to 16 MiB dictionary. */ + s = xz_dec_init(1 << 24); + if (s == NULL) { + msg = "Memory allocation failed\n"; + goto error; + } + + b.in = in; + b.in_pos = 0; + b.in_size = 0; + b.out = out; + b.out_pos = 0; + b.out_size = BUFSIZ; + + while (true) { + if (b.in_pos == b.in_size) { + b.in_size = fread(in, 1, sizeof(in), stdin); + b.in_pos = 0; + } + + ret = xz_dec_run(s, &b); + + if (b.out_pos == sizeof(out)) { + if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos) { + msg = "Write error\n"; + goto error; + } + + b.out_pos = 0; + } + + if (ret == XZ_OK) + continue; + + if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos + || fclose(stdout)) { + msg = "Write error\n"; + goto error; + } + + switch (ret) { + case XZ_STREAM_END: + return 0; + + case XZ_MEMLIMIT_ERROR: + msg = "Preallocated dictionary was too small\n"; + goto error; + + case XZ_FORMAT_ERROR: + msg = "Not a .xz file\n"; + goto error; + + case XZ_OPTIONS_ERROR: + msg = "Unsupported options in the .xz headers\n"; + goto error; + + case XZ_DATA_ERROR: + case XZ_BUF_ERROR: + msg = "File is corrupt\n"; + goto error; + + default: + msg = "Bug!\n"; + goto error; + } + } + +error: + fputs(argv[0], stderr); + fputs(": ", stderr); + fputs(msg, stderr); + return 1; +} |