Path: blob/main/sys/contrib/xz-embedded/userspace/bytetest.c
48254 views
/*1* Lazy test for the case when the output size is known2*3* Author: Lasse Collin <[email protected]>4*5* This file has been put into the public domain.6* You can do whatever you want with this file.7*/89#include <stdbool.h>10#include <stdio.h>11#include <string.h>12#include <stdlib.h>13#include "xz.h"1415static uint8_t in[1];16static uint8_t out[BUFSIZ];1718int main(int argc, char **argv)19{20struct xz_buf b;21struct xz_dec *s;22enum xz_ret ret;23const char *msg;24size_t uncomp_size;2526if (argc != 2) {27fputs("Give uncompressed size as the argument\n", stderr);28return 1;29}3031uncomp_size = atoi(argv[1]);3233xz_crc32_init();3435/*36* Support up to 64 MiB dictionary. The actually needed memory37* is allocated once the headers have been parsed.38*/39s = xz_dec_init(XZ_DYNALLOC, 1 << 26);40if (s == NULL) {41msg = "Memory allocation failed\n";42goto error;43}4445b.in = in;46b.in_pos = 0;47b.in_size = 0;48b.out = out;49b.out_pos = 0;50b.out_size = uncomp_size < BUFSIZ ? uncomp_size : BUFSIZ;5152while (true) {53if (b.in_pos == b.in_size) {54b.in_size = fread(in, 1, sizeof(in), stdin);55b.in_pos = 0;56}5758ret = xz_dec_run(s, &b);5960if (b.out_pos == sizeof(out)) {61if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos) {62msg = "Write error\n";63goto error;64}6566uncomp_size -= b.out_pos;67b.out_pos = 0;68b.out_size = uncomp_size < BUFSIZ69? uncomp_size : BUFSIZ;70}7172if (ret == XZ_OK)73continue;7475#ifdef XZ_DEC_ANY_CHECK76if (ret == XZ_UNSUPPORTED_CHECK) {77fputs(argv[0], stderr);78fputs(": ", stderr);79fputs("Unsupported check; not verifying "80"file integrity\n", stderr);81continue;82}83#endif8485if (uncomp_size != b.out_pos) {86msg = "Uncompressed size doesn't match\n";87goto error;88}8990if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos91|| fclose(stdout)) {92msg = "Write error\n";93goto error;94}9596switch (ret) {97case XZ_STREAM_END:98xz_dec_end(s);99return 0;100101case XZ_MEM_ERROR:102msg = "Memory allocation failed\n";103goto error;104105case XZ_MEMLIMIT_ERROR:106msg = "Memory usage limit reached\n";107goto error;108109case XZ_FORMAT_ERROR:110msg = "Not a .xz file\n";111goto error;112113case XZ_OPTIONS_ERROR:114msg = "Unsupported options in the .xz headers\n";115goto error;116117case XZ_DATA_ERROR:118case XZ_BUF_ERROR:119msg = "File is corrupt\n";120goto error;121122default:123msg = "Bug!\n";124goto error;125}126}127128error:129xz_dec_end(s);130fputs(argv[0], stderr);131fputs(": ", stderr);132fputs(msg, stderr);133return 1;134}135136137