Home | History | Annotate | Line # | Download | only in gzip
unxz.c revision 1.3
      1  1.1  christos 
      2  1.1  christos #include <stdarg.h>
      3  1.1  christos #include <errno.h>
      4  1.1  christos #include <stdio.h>
      5  1.1  christos #include <unistd.h>
      6  1.1  christos #include <lzma.h>
      7  1.1  christos 
      8  1.1  christos static off_t
      9  1.1  christos unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
     10  1.1  christos {
     11  1.1  christos 	lzma_stream strm = LZMA_STREAM_INIT;
     12  1.2  christos 	static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
     13  1.1  christos 	lzma_ret ret;
     14  1.3  christos 	lzma_action action = LZMA_RUN;
     15  1.3  christos 	off_t bytes_out, bp;
     16  1.1  christos 	uint8_t ibuf[BUFSIZ];
     17  1.1  christos 	uint8_t obuf[BUFSIZ];
     18  1.1  christos 
     19  1.3  christos 	if (bytes_in == NULL)
     20  1.3  christos 		bytes_in = &bp;
     21  1.3  christos 
     22  1.1  christos 	strm.next_in = ibuf;
     23  1.2  christos 	memcpy(ibuf, pre, prelen);
     24  1.1  christos 	strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
     25  1.1  christos 	if (strm.avail_in == (size_t)-1)
     26  1.3  christos 		maybe_err("read failed");
     27  1.3  christos 	strm.avail_in += prelen;
     28  1.3  christos 	*bytes_in = strm.avail_in;
     29  1.1  christos 
     30  1.2  christos 	if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
     31  1.2  christos 		maybe_errx("Can't initialize decoder (%d)", ret);
     32  1.2  christos 
     33  1.2  christos 	strm.next_out = NULL;
     34  1.2  christos 	strm.avail_out = 0;
     35  1.2  christos 	if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
     36  1.2  christos 		maybe_errx("Can't read headers (%d)", ret);
     37  1.1  christos 
     38  1.3  christos 	bytes_out = 0;
     39  1.1  christos 	strm.next_out = obuf;
     40  1.1  christos 	strm.avail_out = sizeof(obuf);
     41  1.1  christos 
     42  1.1  christos 	for (;;) {
     43  1.1  christos 		if (strm.avail_in == 0) {
     44  1.1  christos 			strm.next_in = ibuf;
     45  1.1  christos 			strm.avail_in = read(i, ibuf, sizeof(ibuf));
     46  1.3  christos 			switch (strm.avail_in) {
     47  1.3  christos 			case (size_t)-1:
     48  1.3  christos 				maybe_err("read failed");
     49  1.3  christos 				/*NOTREACHED*/
     50  1.3  christos 			case 0:
     51  1.3  christos 				action = LZMA_FINISH;
     52  1.3  christos 				break;
     53  1.3  christos 			default:
     54  1.3  christos 				*bytes_in += strm.avail_in;
     55  1.3  christos 				break;
     56  1.3  christos 			}
     57  1.1  christos 		}
     58  1.1  christos 
     59  1.3  christos 		ret = lzma_code(&strm, action);
     60  1.1  christos 
     61  1.1  christos 		// Write and check write error before checking decoder error.
     62  1.1  christos 		// This way as much data as possible gets written to output
     63  1.1  christos 		// even if decoder detected an error.
     64  1.1  christos 		if (strm.avail_out == 0 || ret != LZMA_OK) {
     65  1.1  christos 			const size_t write_size = sizeof(obuf) - strm.avail_out;
     66  1.1  christos 
     67  1.1  christos 			if (write(o, obuf, write_size) != (ssize_t)write_size)
     68  1.1  christos 				maybe_err("write failed");
     69  1.1  christos 
     70  1.1  christos 			strm.next_out = obuf;
     71  1.1  christos 			strm.avail_out = sizeof(obuf);
     72  1.3  christos 			bytes_out += write_size;
     73  1.1  christos 		}
     74  1.1  christos 
     75  1.1  christos 		if (ret != LZMA_OK) {
     76  1.1  christos 			if (ret == LZMA_STREAM_END) {
     77  1.1  christos 				// Check that there's no trailing garbage.
     78  1.1  christos 				if (strm.avail_in != 0 || read(i, ibuf, 1))
     79  1.1  christos 					ret = LZMA_DATA_ERROR;
     80  1.1  christos 				else {
     81  1.1  christos 					lzma_end(&strm);
     82  1.3  christos 					return bytes_out;
     83  1.1  christos 				}
     84  1.1  christos 			}
     85  1.1  christos 
     86  1.1  christos 			const char *msg;
     87  1.1  christos 			switch (ret) {
     88  1.1  christos 			case LZMA_MEM_ERROR:
     89  1.1  christos 				msg = strerror(ENOMEM);
     90  1.1  christos 				break;
     91  1.1  christos 
     92  1.1  christos 			case LZMA_FORMAT_ERROR:
     93  1.1  christos 				msg = "File format not recognized";
     94  1.1  christos 				break;
     95  1.1  christos 
     96  1.1  christos 			case LZMA_OPTIONS_ERROR:
     97  1.1  christos 				// FIXME: Better message?
     98  1.1  christos 				msg = "Unsupported compression options";
     99  1.1  christos 				break;
    100  1.1  christos 
    101  1.1  christos 			case LZMA_DATA_ERROR:
    102  1.1  christos 				msg = "File is corrupt";
    103  1.1  christos 				break;
    104  1.1  christos 
    105  1.1  christos 			case LZMA_BUF_ERROR:
    106  1.1  christos 				msg = "Unexpected end of input";
    107  1.1  christos 				break;
    108  1.1  christos 
    109  1.1  christos 			case LZMA_MEMLIMIT_ERROR:
    110  1.1  christos 				msg = "Reached memory limit";
    111  1.1  christos 				break;
    112  1.1  christos 
    113  1.1  christos 			default:
    114  1.2  christos 				msg = "Unknown error (%d)";
    115  1.1  christos 				break;
    116  1.1  christos 			}
    117  1.1  christos 
    118  1.2  christos 			maybe_errx(msg, ret);
    119  1.1  christos 		}
    120  1.1  christos 	}
    121  1.1  christos }
    122