Home | History | Annotate | Line # | Download | only in gzip
      1  1.1  simonb /*-
      2  1.1  simonb  * SPDX-License-Identifier: BSD-2-Clause
      3  1.1  simonb  *
      4  1.1  simonb  * Copyright (c) 2022 Klara, Inc.
      5  1.1  simonb  *
      6  1.1  simonb  * Redistribution and use in source and binary forms, with or without
      7  1.1  simonb  * modification, are permitted provided that the following conditions
      8  1.1  simonb  * are met:
      9  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     10  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     11  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     14  1.1  simonb  *
     15  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1  simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1  simonb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1  simonb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  simonb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  simonb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1  simonb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  simonb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  simonb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  simonb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  simonb  * SUCH DAMAGE.
     26  1.1  simonb  */
     27  1.1  simonb 
     28  1.1  simonb /* This file is #included by gzip.c */
     29  1.1  simonb 
     30  1.1  simonb static off_t
     31  1.1  simonb unzstd(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
     32  1.1  simonb {
     33  1.1  simonb 	static char *ibuf, *obuf;
     34  1.1  simonb 	ZSTD_inBuffer zib;
     35  1.1  simonb 	ZSTD_outBuffer zob;
     36  1.1  simonb 	ZSTD_DCtx *zds;
     37  1.1  simonb 	ssize_t res;
     38  1.1  simonb 	size_t zres;
     39  1.1  simonb 	size_t bytes_out = 0;
     40  1.1  simonb 	int eof = 0;
     41  1.1  simonb 
     42  1.1  simonb 	if (ibuf == NULL)
     43  1.1  simonb 		ibuf = malloc(BUFLEN);
     44  1.1  simonb 	if (obuf == NULL)
     45  1.1  simonb 		obuf = malloc(BUFLEN);
     46  1.1  simonb 	if (ibuf == NULL || obuf == NULL)
     47  1.1  simonb 		maybe_err("malloc");
     48  1.1  simonb 
     49  1.1  simonb 	zds = ZSTD_createDStream();
     50  1.1  simonb 	ZSTD_initDStream(zds);
     51  1.1  simonb 
     52  1.1  simonb 	zib.src = pre;
     53  1.1  simonb 	zib.size = prelen;
     54  1.1  simonb 	zib.pos = 0;
     55  1.1  simonb 	if (bytes_in != NULL)
     56  1.1  simonb 		*bytes_in = prelen;
     57  1.1  simonb 	zob.dst = obuf;
     58  1.1  simonb 	zob.size = BUFLEN;
     59  1.1  simonb 	zob.pos = 0;
     60  1.1  simonb 
     61  1.1  simonb 	while (!eof) {
     62  1.1  simonb 		if (zib.pos >= zib.size) {
     63  1.1  simonb 			res = read(in, ibuf, BUFLEN);
     64  1.1  simonb 			if (res < 0)
     65  1.1  simonb 				maybe_err("read");
     66  1.1  simonb 			if (res == 0)
     67  1.1  simonb 				eof = 1;
     68  1.1  simonb 			infile_newdata(res);
     69  1.1  simonb 			zib.src = ibuf;
     70  1.1  simonb 			zib.size = res;
     71  1.1  simonb 			zib.pos = 0;
     72  1.1  simonb 			if (bytes_in != NULL)
     73  1.1  simonb 				*bytes_in += res;
     74  1.1  simonb 		}
     75  1.1  simonb 		zres = ZSTD_decompressStream(zds, &zob, &zib);
     76  1.1  simonb 		if (ZSTD_isError(zres)) {
     77  1.1  simonb 			maybe_errx("%s", ZSTD_getErrorName(zres));
     78  1.1  simonb 		}
     79  1.1  simonb 		if (zob.pos > 0) {
     80  1.1  simonb 			res = write(out, obuf, zob.pos);
     81  1.1  simonb 			if (res < 0)
     82  1.1  simonb 				maybe_err("write");
     83  1.1  simonb 			zob.pos = 0;
     84  1.1  simonb 			bytes_out += res;
     85  1.1  simonb 		}
     86  1.1  simonb 	}
     87  1.1  simonb 	ZSTD_freeDStream(zds);
     88  1.1  simonb 	return (bytes_out);
     89  1.1  simonb }
     90