Home | History | Annotate | Line # | Download | only in gzip
unbzip2.c revision 1.10.16.1
      1  1.10.16.1    yamt /*	$NetBSD: unbzip2.c,v 1.10.16.1 2008/05/18 12:36:06 yamt Exp $	*/
      2       1.10  simonb 
      3       1.10  simonb /*-
      4       1.10  simonb  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5       1.10  simonb  * All rights reserved.
      6       1.10  simonb  *
      7       1.10  simonb  * This code is derived from software contributed to The NetBSD Foundation
      8       1.10  simonb  * by Simon Burge.
      9       1.10  simonb  *
     10       1.10  simonb  * Redistribution and use in source and binary forms, with or without
     11       1.10  simonb  * modification, are permitted provided that the following conditions
     12       1.10  simonb  * are met:
     13       1.10  simonb  * 1. Redistributions of source code must retain the above copyright
     14       1.10  simonb  *    notice, this list of conditions and the following disclaimer.
     15       1.10  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.10  simonb  *    notice, this list of conditions and the following disclaimer in the
     17       1.10  simonb  *    documentation and/or other materials provided with the distribution.
     18       1.10  simonb  *
     19       1.10  simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.10  simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.10  simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.10  simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.10  simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.10  simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.10  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.10  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.10  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.10  simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.10  simonb  * POSSIBILITY OF SUCH DAMAGE.
     30       1.10  simonb  */
     31        1.1     mrg 
     32        1.1     mrg /* This file is #included by gzip.c */
     33        1.1     mrg 
     34        1.1     mrg static off_t
     35        1.4     mrg unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
     36        1.1     mrg {
     37        1.4     mrg 	int		ret, end_of_file;
     38        1.1     mrg 	off_t		bytes_out = 0;
     39        1.3     mrg 	bz_stream	bzs;
     40        1.3     mrg 	static char	*inbuf, *outbuf;
     41        1.1     mrg 
     42        1.6     dsl 	if (inbuf == NULL)
     43        1.6     dsl 		inbuf = malloc(BUFLEN);
     44        1.6     dsl 	if (outbuf == NULL)
     45        1.6     dsl 		outbuf = malloc(BUFLEN);
     46        1.6     dsl 	if (inbuf == NULL || outbuf == NULL)
     47        1.5     mrg 	        maybe_err("malloc");
     48        1.3     mrg 
     49        1.3     mrg 	bzs.bzalloc = NULL;
     50        1.3     mrg 	bzs.bzfree = NULL;
     51        1.3     mrg 	bzs.opaque = NULL;
     52        1.3     mrg 
     53        1.3     mrg 	end_of_file = 0;
     54        1.3     mrg 	ret = BZ2_bzDecompressInit(&bzs, 0, 0);
     55        1.3     mrg 	if (ret != BZ_OK)
     56        1.5     mrg 	        maybe_errx("bzip2 init");
     57        1.3     mrg 
     58        1.4     mrg 	/* Prepend. */
     59        1.4     mrg 	bzs.avail_in = prelen;
     60        1.4     mrg 	bzs.next_in = pre;
     61        1.4     mrg 
     62        1.4     mrg 	if (bytes_in)
     63        1.4     mrg 		*bytes_in = prelen;
     64        1.3     mrg 
     65        1.9     mrg 	while (ret >= BZ_OK && ret != BZ_STREAM_END) {
     66        1.3     mrg 	        if (bzs.avail_in == 0 && !end_of_file) {
     67        1.8     mrg 			ssize_t	n;
     68        1.8     mrg 
     69        1.4     mrg 	                n = read(in, inbuf, BUFLEN);
     70        1.3     mrg 	                if (n < 0)
     71        1.5     mrg 	                        maybe_err("read");
     72        1.3     mrg 	                if (n == 0)
     73        1.3     mrg 	                        end_of_file = 1;
     74        1.3     mrg 	                bzs.next_in = inbuf;
     75        1.3     mrg 	                bzs.avail_in = n;
     76        1.4     mrg 			if (bytes_in)
     77        1.4     mrg 				*bytes_in += n;
     78        1.4     mrg 	        }
     79        1.3     mrg 
     80        1.3     mrg 	        bzs.next_out = outbuf;
     81        1.4     mrg 	        bzs.avail_out = BUFLEN;
     82        1.3     mrg 	        ret = BZ2_bzDecompress(&bzs);
     83        1.3     mrg 
     84        1.3     mrg 	        switch (ret) {
     85        1.3     mrg 	        case BZ_STREAM_END:
     86        1.3     mrg 	        case BZ_OK:
     87        1.3     mrg 	                if (ret == BZ_OK && end_of_file)
     88        1.5     mrg 	                        maybe_err("read");
     89        1.3     mrg 	                if (!tflag) {
     90        1.8     mrg 				ssize_t	n;
     91        1.8     mrg 
     92        1.4     mrg 	                        n = write(out, outbuf, BUFLEN - bzs.avail_out);
     93        1.3     mrg 	                        if (n < 0)
     94        1.5     mrg 	                                maybe_err("write");
     95        1.8     mrg 	                	bytes_out += n;
     96        1.3     mrg 	                }
     97        1.3     mrg 	                break;
     98        1.3     mrg 
     99        1.3     mrg 	        case BZ_DATA_ERROR:
    100        1.6     dsl 	                maybe_warnx("bzip2 data integrity error");
    101        1.5     mrg 			break;
    102        1.5     mrg 
    103        1.3     mrg 	        case BZ_DATA_ERROR_MAGIC:
    104        1.6     dsl 	                maybe_warnx("bzip2 magic number error");
    105        1.5     mrg 			break;
    106        1.5     mrg 
    107        1.3     mrg 	        case BZ_MEM_ERROR:
    108        1.6     dsl 	                maybe_warnx("bzip2 out of memory");
    109        1.5     mrg 			break;
    110        1.5     mrg 
    111        1.3     mrg 	        }
    112        1.3     mrg 	}
    113        1.1     mrg 
    114        1.5     mrg 	if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK)
    115        1.5     mrg 	        return (-1);
    116        1.1     mrg 
    117        1.1     mrg 	return (bytes_out);
    118        1.1     mrg }
    119