diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2012-02-22 12:30:08 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2012-02-22 12:30:08 +0200 |
commit | df8539e5818cd66236eb05778f2abb3788c858a1 (patch) | |
tree | 2a483bcc04e9acdc4bde4d556decff90aa3a179d | |
parent | 48f4588342f4a4e0182a6740e25675fd8e6c6295 (diff) | |
download | xz-embedded-df8539e5818cd66236eb05778f2abb3788c858a1.tar.gz |
Add the test program that was supposed to be in the previous commit.
Note: The previous commit is five months old.
-rw-r--r-- | userspace/bytetest.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/userspace/bytetest.c b/userspace/bytetest.c new file mode 100644 index 0000000..aa48b9b --- /dev/null +++ b/userspace/bytetest.c @@ -0,0 +1,135 @@ +/* + * Lazy test for the case when the output size is known + * + * 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 <stdlib.h> +#include "xz.h" + +static uint8_t in[1]; +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; + size_t uncomp_size; + + if (argc != 2) { + fputs("Give uncompressed size as the argument", stderr); + return 1; + } + + uncomp_size = atoi(argv[1]); + + xz_crc32_init(); + + /* + * Support up to 64 MiB dictionary. The actually needed memory + * is allocated once the headers have been parsed. + */ + s = xz_dec_init(XZ_DYNALLOC, 1 << 26); + 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 = uncomp_size < BUFSIZ ? uncomp_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; + } + + uncomp_size -= b.out_pos; + b.out_pos = 0; + b.out_size = uncomp_size < BUFSIZ + ? uncomp_size : BUFSIZ; + } + + if (ret == XZ_OK) + continue; + +#ifdef XZ_DEC_ANY_CHECK + if (ret == XZ_UNSUPPORTED_CHECK) { + fputs(argv[0], stderr); + fputs(": ", stderr); + fputs("Unsupported check; not verifying " + "file integrity\n", stderr); + continue; + } +#endif + + if (uncomp_size != b.out_pos) { + msg = "Uncompressed size doesn't match\n"; + goto error; + } + + 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: + xz_dec_end(s); + return 0; + + case XZ_MEM_ERROR: + msg = "Memory allocation failed\n"; + goto error; + + case XZ_MEMLIMIT_ERROR: + msg = "Memory usage limit reached\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: + xz_dec_end(s); + fputs(argv[0], stderr); + fputs(": ", stderr); + fputs(msg, stderr); + return 1; +} |